2012-04-13 72 views
1

我发现这个代码,一个自定义的UITableViewCell:与自定义的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)的实例...我怎么能这样做?

回答

1

有时人们会使用Interface Builder创建自定义UITableViewCell。此人正在加载其自定义的UITableViewCell子类并将其分配给单元格。该行:cell = (MyCustomCell *)view;假设可行,因为MyCustomCellUITableViewCell的一个子类。

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

0

首先,它遍历一组UIView s或UIView子类。它将每个迭代的变量存储到一个名为view的指针中。

然后简单地将当前的view变量转换为MyCustomCell类型。据推测MyCustomCell延伸UIView,所以这是合法的。

如果您想使用特定于MyCustomCell的方法,那么Xcode将不知道它们存在,如果您没有明确Type Cast您的对象,这非常有用。

+0

而我怎么能把它转换成我的MyCustomCell实例的类型呢? – 2012-04-13 20:12:46

+0

@Lucase Pereira:我不知道我跟着你。现在它将'cell'强制转换为'MyCustomCell',如果它确实是'MyCustomCell'的一个实例。 – Josh 2012-04-13 20:14:33