在UITableView中对齐文本 [英] Aligning text in UITableView

查看:119
本文介绍了在UITableView中对齐文本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个牢房。每当单元格行中的文本等于(null)时,我希望标签位于单元格的右侧。

I have a cell. Whenever the text in cell row is equal to "(null)" I want the label to be on the right hand side of the the cell.

这是我的代码那一刻,但它没有做任何事情。没有错误,它只是没有对齐到单元格的右侧。任何想法?

Here is my code at the moment, but it isn't doing anything. No errors, it just doesn't align to the right hand side of the cell. Any ideas?

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    static NSString *CellIdentifier = @"ChatListItem";
     NSDictionary *itemAtIndex = (NSDictionary *)[messages objectAtIndex:indexPath.row];
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
    }

    if([[itemAtIndex objectForKey:@"user"] isEqualToString:@"(null)"]){
        cell.textLabel.textAlignment=UITextAlignmentRight;
        cell.detailTextLabel.textAlignment=UITextAlignmentRight;
    }
    cell.textLabel.text = [itemAtIndex objectForKey:@"text"];
    cell.detailTextLabel.text = [itemAtIndex objectForKey:@"user"];

   return cell;
}


推荐答案

首先,你是否已经完成了代码并检查键user和text的值的内容?

First, did you step through the code and check the contents of the value for keys "user" and "text"?

如果全部符合预期,则应执行以下操作:

If all is as expected, you should do the following:


  1. NSTextAlignmentRight 替换 UITextAlignmentRight 以沉默编译器警告。

  2. 明确设置 NSTextAlignmentRight NSTextAlignmentLeft ,否则您将无法正确设置再生细胞更新。

  3. 最后,确保标签的宽度是固定的。否则,标签的宽度将基于其内容,以便对齐(在标签内)失去其效果。

  1. Replace UITextAlignmentRight with NSTextAlignmentRight to silence compiler warnings.
  2. Explicitly set NSTextAlignmentRight and NSTextAlignmentLeft, otherwise you will not get the correct update in recycled cells.
  3. Finally, make sure the label's width is fixed. Otherwise, the width of the label will be based on its content, so that the alignment (within the label) loses its effect.

这篇关于在UITableView中对齐文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆