不兼容的指针类型的警告

问题描述:

我仍然在iOS开发有点绿,不断得到那我不知道有关的警告。不兼容的指针类型的警告

我使用一个tableview中自定义单元格,并设置其类是的UITableViewCell的子类,称为SiteCell。在我的“didSelectRowAtIndexPath方法”的方法,当我声明所选择的小区作为SiteCell类型,收到以下警告:

不相容指针类型初始化SiteCell与类型的 表达__strong的UITableViewCell

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{ 
    SiteCell *cell = [tableView cellForRowAtIndexPath:indexPath]; 

    NSString *rowIdentity = [NSString stringWithFormat:@"%d", indexPath.row]; 
    [selectedRows setObject:[cell.siteTitleLabel text] forKey:rowIdentity]; 

    if(cell.accessoryType == UITableViewCellAccessoryCheckmark){ 
     cell.accessoryType = UITableViewCellAccessoryNone; 
    }else{ 
     cell.accessoryType = UITableViewCellAccessoryCheckmark; 
    } 
} 

任何人都可以阐明如何摆脱这一警告的任何轻?

假设的cellForRowAtIndexPath正确编码,被称为总是返回SiteCell,你只需要简单地转换为SiteCell*

SiteCell *cell = (SiteCell*)[tableView cellForRowAtIndexPath:indexPath]; 

你的代码应该是这样的,因为你必须将UITableViewCell投射到您的子类SiteCell

SiteCell *cell = (SiteCell *)[tableView cellForRowAtIndexPath:indexPath]; 
+0

当然!谢谢 – Pheepster 2013-04-04 18:26:06

正确的做法。你需要NSIndexPath

SiteCell *cell = (SiteCell *) [self.tableView cellForRowAtIndexPath:[NSIndexPath indexPathForRow:row inSection:section]];