1

我找到了自定义 UITableViewCell 的代码:

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) 
{
    NSArray* views = [[NSBundle mainBundle] loadNibNamed:@"MyCustomCell" owner:nil options:nil];

    MyCustomCell *customCell = [[MyCustomCell alloc]init];

    MyCustomCell.cellImage = [arrayImages objectAtIndex:indexPath.row];

    for (UIView *view in views) 
    {
        if([view isKindOfClass:[UITableViewCell class]])
        {                
            cell = (MyCustomCell *)view;

        }
    }
}

我无法弄清楚这个特定部分是如何工作的:cell = (MyCustomCell *)view;

我想为我之前创建的 MyCustomCell (customCell) 实例更改它...我该怎么做?

4

2 回答 2

1

UITableViewCell有时人们会使用 Interface Builder创建自定义。这个人只是加载他们的自定义UITableViewCell子类并将其分配给单元格。这行:cell = (MyCustomCell *)view;presumable 有效,因为MyCustomCell它是UITableViewCell.

这只是另一种创建自定义单元格的技术,有时您会看到使用标签完成类似的事情。

于 2012-04-13T20:16:05.540 回答
0

首先,它遍历一个UIViews 或UIView子类的集合。它将每次迭代的变量存储到一个名为view.

然后它只是将当前view变量转换为MyCustomCell. 大概是MyCustomCellextends UIView,所以这是合法的。

如果您想使用特定于 的方法,这很有用MyCustomCell,因为 Xcode 不会知道它们存在,如果您没有显式类型转换您的对象。

于 2012-04-13T20:09:16.370 回答