22

所以我注册了我的手机:

[self.tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"Cell"];

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"Cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];

    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
    }
    // setting up the cell
}

问题是我无法设置cell.detailTextLabel.text属性。细胞永远不会nil

4

7 回答 7

39

如果首先调用,如果单元重用标识符匹配,表格视图registerClass将导致dequeueReusableCellWithIdentifier返回非零单元。

我相信 registerClass 通常用于将是从UITableViewCell. 您的自定义单元格可以覆盖 initWithStyle 并在那里设置样式。

创建自定义单元格并不总是必要的。

如果要设置单元格样式,请不要调用registerClass.

于 2013-05-22T02:35:00.087 回答
15

您需要遵循 3 项更改来实现您的目标:

  1. 删除 registerClass 语句。
  2. UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath]; => UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
  3. 使用 initWithStyle:UITableViewCellStyleSubtitle

通常有两种方法可以创建带有 subtile 的单元格,第一种是自定义 UITableViewCell,在 init 中设置样式。其次是后面的代码,这就是你想要的:

static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
    cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];
}
于 2014-01-20T10:02:10.707 回答
2

最简单的方法是使用故事板,并在 IB 中设置单元格样式。在这种情况下,您不应该注册任何东西,也不应该有 if (cell == nil) 子句。使用 dequeueReusableCellWithIdentifier: 还是 dequeueReusableCellWithIdentifier:forIndexPath 似乎并不重要。当在故事板中创建单元格时,它们都保证返回一个单元格。

于 2013-05-22T05:58:17.457 回答
2

创建一个自定义单元格。在界面生成器中更改其样式。使用表格视图从 nib 视图控制器注册单元格。

设置样式

和代码:

- (void)viewDidLoad {
    [super viewDidLoad];

    [self.tableView registerNib:[UINib nibWithNibName:@"YourCustomCell" bundle:nil] forCellReuseIdentifier:kReuseIdentifier];
}

- (UITableViewCell *)tableView:(UITableView *)tableView
     cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    YourCustomCell *cell = [tableView dequeueReusableCellWithIdentifier:kReuseIdentifier];

    // Do things with the cell. 
    // The cell has no chance to be nil because you've already registered it in viewDidLoad method. 
    // So there's not need to write any code like if(cell==nil).
    // Just use it.

    return cell;
}
于 2015-01-12T09:34:16.337 回答
0

您需要更改单元格样式:

if (cell == nil) {
                    cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}

对此

if (cell == nil) {
            cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];
        }

这对你有用。

于 2013-05-22T05:19:40.413 回答
0

尝试对单元格使用 UITableViewCellStyleSubtitle 样式。将 if 语句中的行更改为:

cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];
于 2013-05-22T02:36:09.283 回答
0

这是一个老问题。我只想提供一个替代解决方案。

注册单元后,为什么不在下面尝试:

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];

然后做:

[cell initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier ];

这个对我有用。

于 2016-11-25T16:18:16.423 回答