3

我在 iOS 开发中仍然有点绿色,并且不断收到我不确定的警告。

我在表格视图中使用自定义单元格,并将其类设置为 UITableViewCell 的子类,称为 SiteCell。在我的“didSelectRowAtIndexPath”方法中,当我将所选单元格声明为 SiteCell 类型时,我收到以下警告:

使用 UITableViewCell 类型的表达式初始化 SiteCell __strong 的不兼容指针类型

-(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;
    }
}

任何人都可以阐明如何摆脱这个警告吗?

4

3 回答 3

16

假设 cellForRowAtIndexPath 编码正确,并且已知总是返回 a SiteCell,您只需转换为SiteCell*

SiteCell *cell = (SiteCell*)[tableView cellForRowAtIndexPath:indexPath];
于 2013-04-04T18:21:15.237 回答
3

您的代码应该是这样,因为您必须将其UITableViewCell转换为您的子类SiteCell

SiteCell *cell = (SiteCell *)[tableView cellForRowAtIndexPath:indexPath];
于 2013-04-04T18:16:45.380 回答
1

正确的方法。你需要NSIndexPath

SiteCell *cell = (SiteCell *) [self.tableView cellForRowAtIndexPath:[NSIndexPath indexPathForRow:row inSection:section]];
于 2013-05-27T15:54:36.493 回答