【问题标题】:Why doesn't my UITableView use prototypes for cells?为什么我的 UITableView 不使用单元格原型?
【发布时间】:2014-03-17 17:42:36
【问题描述】:

所以这是我的代码,它不起作用

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier;
    if (indexPath.row == 0)
    {
        // buttonCell
        CellIdentifier = @"buttonCell";
        buttonCell *cell = (buttonCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
        cell.startAddressLabel.text = @"something";

        //cell config
        //debug
        NSLog(@"indexPath.row: %d", indexPath.row);
        NSLog(@"Cell Identifier: %@", CellIdentifier);

        return cell;
    } else if (indexPath.row == 1)
    {
        //mutableCaptionCell date&time
        CellIdentifier = @"mutableCaptionCell";
        mutableCaptionCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];

        //cell config
        cell.infoLabel.text = @"Время и дата";
        cell.contentLabel.text = @"";

        //debug
        NSLog(@"indexPath.row: %d", indexPath.row);

        return cell;

    } else if (indexPath.row == 2)
    {
        //mutableCaptionCell tax
        CellIdentifier = @"mutableCaptionCell";
        mutableCaptionCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];

        //cell config
        cell.infoLabel.text = @"Тариф";
        cell.contentLabel.text = @"";

        //debug
        NSLog(@"indexPath.row: %d", indexPath.row);

      return cell;
    } else if (indexPath.row == 3)
    {
        //mutableCaptionCell car
        CellIdentifier = @"mutableCaptionCell";
        mutableCaptionCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];

        //cell config
        cell.infoLabel.text = @"Выбор машины на карте";
        cell.contentLabel.text = @"";

        //debug
        NSLog(@"indexPath.row: %d", indexPath.row);

        return cell;
    } else if (indexPath.row == 4)
    {
        //wishCell wishlist
        CellIdentifier = @"wishCell";
        WishCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];

        //cell config
        cell.infoLabel.text = @"Пожелания";
        cell.contentLabel.text = @"";

        //debug
        NSLog(@"indexPath.row: %d", indexPath.row);

        return cell;
    } else
    {
        //debug
        NSLog(@"indexPath.row: %d", indexPath.row);
        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"mutableCaptionCell" forIndexPath:indexPath];

        // Configure the cell...
        cell.textLabel.text = @"ERROR CELL";

        //debug
        //NSLog(@"indexPath.row: %d", indexPath.row);

        return cell;

        ;
    }

}

我已经为每个原型说过 UITableViewCell 的子类,重用标识符与每个原型匹配,但我在运行时看到的 UITableView 似乎没有使用这些原型。例如,第一个单元格必须很大,有两个按钮,但它显示为空。我 NSLoged 一切,并记录显示:

2014-02-18 10:13:51.587 app[1624:70b] indexPath.row: 0
2014-02-18 10:13:51.590 app[1624:70b] Cell Identifier: buttonCell
2014-02-18 10:13:51.594 app[1624:70b] cell: <buttonCell: 0x8b95e00; baseClass = UITableViewCell; frame = (0 0; 320 44); autoresize = W; layer = <CALayer: 0x8b96000>>
2014-02-18 10:13:51.600 app[1624:70b] indexPath.row: 1
2014-02-18 10:13:51.603 app[1624:70b] indexPath.row: 2
2014-02-18 10:13:51.606 app[1624:70b] indexPath.row: 3
2014-02-18 10:13:51.610 app[1624:70b] indexPath.row: 4

但 TableView 仍然没有使用正确的原型。

我正在尝试使用动态原型,因为我需要在运行时更改一些单元格高度,可能有一种方法可以使用静态单元格吗?

旁注:我在 iOS 7 上使用 Xcode 5

【问题讨论】:

    标签: ios uitableview


    【解决方案1】:

    我的猜测是,你没有初始化你的单元格,dequeueReusableCellWithIdentifier 还不够。它重用了已经创建的单元格,因此如果出队导致nil,则应首先分配cell

    可能:

    if (indexPath.row == 0)
    {
        // buttonCell
        CellIdentifier = @"buttonCell";
        buttonCell *cell = (buttonCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
    
    
       if(!cell)
       {
          cell = [[UITableViewCell alloc] initWithStyle:UITableViewStylePlain reuseIdentifier:CellIdentifier];
       }
    
        cell.startAddressLabel.text = @"something";
    
        //cell config
        //debug
        NSLog(@"indexPath.row: %d", indexPath.row);
        NSLog(@"Cell Identifier: %@", CellIdentifier);
    
        return cell;
    }
    

    或使用 IB 设计的称重传感器:

    if (indexPath.row == 0)
    {
        // buttonCell
        CellIdentifier = @"buttonCell";
        buttonCell *cell = (buttonCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
    
    
       if(!cell)
       {
            cell = [[[NSBundle mainBundle] loadNibNamed:@"MyCustomCell" owner:nil options:nil] objectAtIndex:0];   // Assign reuseIdentifier from IB
       }
    
        cell.startAddressLabel.text = @"something";
    
        //cell config
        //debug
        NSLog(@"indexPath.row: %d", indexPath.row);
        NSLog(@"Cell Identifier: %@", CellIdentifier);
    
        return cell;
    }
    

    希望对你有帮助!

    【讨论】:

    • 许多消息来源说不再需要检查单元格是否实际创建,无论如何,您的答案不起作用,对不起
    • 你不再需要这样做了 ;) 因为 iOS 5 或 6 它保证返回一个有效的(即初始化的)单元格。 As per doc:Return Value A UITableViewCell object with the associated reuse identifier. This method always returns a valid cell.
    • 好的,所以它返回一个有效的单元格,但是应该先调用registerNib:forCellReuseIdentifierdeveloper.apple.com/library/ios/documentation/uikit/reference/…:然后试试看。
    • 如果您使用的是故事板,则不是 ;)
    • 我用的是Storyboard,所以没有这些.nib文件,还用这个方法吗?
    【解决方案2】:

    如果单元格数量有限,最好使用静态单元格。当我们滚动表格视图时,单元格将一次又一次地放置。所花费的时间会非常少,无论它拾取哪个单元格,它都会对所有单元格显示相同的高度......我建议尽可能使用静态单元格或不同的表格视图......

    【讨论】:

    • 我在之前的迭代中使用了静态单元格,但现在我需要在运行时更改一些单元格的高度,如上所述。有没有办法这样做?其中一个单元格将导致带有选项列表的不同场景,我应该在该单元格上显示选择的选项,因此必须更改高度
    • 这可能对你有帮助....stackoverflow.com/questions/3737218/…
    • 也感谢您的帮助,我回到静态单元格解决方案,因为在使用情节提要时使用 .xibs 的解决方案对我来说似乎是错误的,这不应该是:因为他们实施情节提要,所有工作都必须完成带故事板
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-05-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-08-15
    • 1970-01-01
    相关资源
    最近更新 更多