【问题标题】:How to use UITextAlignment to align text in UITableViewCell to the right and left in Swift如何使用 UITextAlignment 将 UITableViewCell 中的文本在 Swift 中左右对齐
【发布时间】:2023-03-29 14:55:02
【问题描述】:

我必须将表格单元格中的文本左右对齐。我在我的应用程序中使用了两个 plist 根据 plist 选择,我必须在表格单元格中对齐文本。

我尝试以下代码

if ([app.currentPlist isEqualToString:@"Accounting.plist"]) {
    cell.textAlignment=UITextAlignmentLeft;            
} else {
    cell.textAlignment=UITextAlignmentRight;           
}

【问题讨论】:

    标签: iphone swift xcode ipad


    【解决方案1】:

    UITableViewCell 没有 textAlignment 属性。您必须在单元格的文本标签上设置它:

    - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
    {
        NSUInteger row = [indexPath row];
    
        static NSString *CellIdentifier = @"Cell";
    
        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
        if (cell == nil) {
            cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
        }
    
        cell.textLabel.text = @"Something";
    
        if([app.currentPlist isEqualToString:@"Accounting.plist"])
        {
            cell.textLabel.textAlignment=UITextAlignmentLeft;             
        }
        else
        {
            cell.textLabel.textAlignment=UITextAlignmentRight;           
        }
    
        return cell;
    }
    

    【讨论】:

    • @Vishal P - 我用了你的代码! - 但是你把 cellForRowAtIndexPath... 看我更新的答案
    【解决方案2】:

    UITextAlignmentRight 已弃用,请改用NSTextAlignmentLeft

    所以,代码将是 - cell.textLabel.textAlignment = NSTextAlignmentLeft;

    【讨论】:

      【解决方案3】:

      很快就可以了

      cell.textLabel.textAlignment = NSTextAlignment.Right
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2023-03-31
        • 2014-12-12
        • 2022-07-20
        • 2012-11-26
        • 2015-04-24
        相关资源
        最近更新 更多