【问题标题】:UITableViewCell initWithStyle:UITableViewCellStyleSubtitle is not workingUITableViewCell initWithStyle:UITableViewCellStyleSubtitle 不工作
【发布时间】:2017-04-07 08:10:17
【问题描述】:

我在尝试在单元格中显示信息时遇到问题,一个在左侧,一个在右侧。我知道使用initWithStyleUITableViewCellStyleSubtitle。我用这个,但它似乎不起作用。

这里是一些示例代码:

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

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

    Accounts *account = [self.fetchedResultsController objectAtIndexPath];
    cell.textLabel.text = account.name;

    cell.detailTextLabel.text = @"Price";

    return cell;
}

我可以很好地显示 cell.textLabel.text,但是我无法显示简单的“价格”。我尝试了不同的方法,例如设置cell.detailTextLabel 的字体大小。

我也尝试过UITableViewCellStyleValue1,正如一些人在旧帖子中所建议的那样。 设置为“Price”后抛出 NSLog,显示 cell.detailTextLabel 为 null。

不知道我做错了什么。

编辑:我发现了这个:cell.detailTextLabel.text is NULL

如果我删除 if (cell == nil) 它会起作用... 该检查应该到位,那么在使用不同样式时如何使其工作?

【问题讨论】:

  • 你在其他地方使用CellIdentifier吗?也许您实际上是在使单元格出列。
  • 不管怎样,你有没有通过调试器来查看cell在每一行指向什么?
  • 不,CellIdentifier 没有在其他地方使用。
  • 我的意思不是要让它成为如何使用 XCode...我对此很陌生,还没有使用断点和调试器。刚刚想通了怎么做。那么,您是说我应该查看每个断点处的单元格的值吗?如果是这样,我会在整个过程中设置一些断点。样式没有设置,因为它没有输入 if (cell == nil)。每个断点显示单元格相同的值,对于此运行 0X06aa3140
  • 尝试将CellIdentifier 更改为其他内容,例如“uniqueString”。

标签: ios uitableview


【解决方案1】:

使用情节提要和原型单元格时,总是从 dequeue 方法返回一个单元格(假设存在具有该标识符的原型)。这意味着您永远不会进入(cell == nil) 块。

在您的情况下,原型单元格未在情节提要中使用字幕样式定义,因此从不使用带字幕的单元格,并且不存在详细文本标签。将情节提要中的原型更改为具有字幕样式。

【讨论】:

  • 断点清楚地表明我没有进入 if (cell == nil),正如我上面所说的。正如你所说。将原型单元格样式更改为字幕有效!
  • @jrturton,这一切如何在代码中完成?我没有使用故事板或笔尖。
  • @scorpiozj 如果您在 viewDidLoad 中注册一个具有重用标识符的单元类,那么该单元将自动创建,就像您在情节提要中注册原型一样。你是这个意思吗?
  • @jrturton,是的。我在 viewDidLoad 中注册了 UITableViewCell 类,并在委托方法中找到了单元格 != nil 当我出队时。
  • 我不使用情节提要,但是dequeued其detailLabel为null的单元格。``UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"UITableViewCell" forIndexPath:indexPath]; if (cell == nil) { cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:@"UITableViewCell"]; // cell.selectionStyle = UITableViewCellSelectionStyleNone; }```
【解决方案2】:

仅在尝试这些行后删除所有代码并检查是否可行。

 -(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
 {
    static NSString *CellIdentifier = @"Cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
          cell = [[[UITableViewCell alloc]
             initWithStyle:UITableViewCellStyleSubtitle
             reuseIdentifier:CellIdentifier]
            autorelease];
 }


   cell.textLabel.text=[Array objectAtIndex:indexPath.row];
   cell.detailTextLabel.text=@"Price";


   return cell;
 }

【讨论】:

    【解决方案3】:

    我看到了问题:在您的方法名称中,UITableView 变量被命名为 ltableView,而不是 tableView。将其更改为tableView

    【讨论】:

    • 对不起,我不得不在我的工作机器上手动输入。这是一个错字,但不是在实际代码中。当我运行代码时,我不得不出错或崩溃。
    【解决方案4】:

    cell.detailTextLable.text 应该是 cell.detailTextLabel.text。它看起来像一个简单的标签拼写错误。

    【讨论】:

    • 抱歉,我是手动输入的,没有复制/粘贴...已修复。我没有错误并且运行没有崩溃。 “价格”将不会显示。
    【解决方案5】:

    这里提到的所有答案都是真正的解决方法,即使用故事板。 这是一种仅在代码中执行此操作的方法。

    基本上不是在 viewDidLoad 中注册单元格的标识符,而是在 cellForRowAtIndexPath: 方法中只注册一次。同时重置在 viewDidLoad 中注册的单元格 __sCellRegistered = 0;

        static int _sCellRegistered = 0;
    - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
      UITableViewCell *cell = nil;
    
    
    if (__sCellRegistered == 0) {
        __sCellRegistered = 1;
        NSLog(@"register cell");
    
        cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:@"CellIdentifier"];
        [self.tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"CellIdentifier"];
    };
    
    if (!cell) {
        NSLog(@"dequeue");
    
        cell = [tableView dequeueReusableCellWithIdentifier:@"CellIdentifier" forIndexPath:indexPath];
    }
    

    【讨论】:

      猜你喜欢
      • 2015-09-18
      • 1970-01-01
      • 1970-01-01
      • 2013-10-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-04-20
      相关资源
      最近更新 更多