【问题标题】:Incompatible pointer types warning (in Objective-C)不兼容的指针类型警告(在 Objective-C 中)
【发布时间】:2013-04-04 18:15:42
【问题描述】:

我在 iOS 开发方面还不太成熟,并且不断收到我不确定的警告。

我在表格视图中使用自定义单元格,并将其类设置为 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;
    }
}

任何人都可以阐明如何消除此警告吗?

【问题讨论】:

    标签: objective-c types variable-assignment compiler-warnings


    【解决方案1】:

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

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

    【讨论】:

      【解决方案2】:

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

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

      【讨论】:

        【解决方案3】:

        正确的方法。你需要NSIndexPath

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

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2011-04-14
          • 2015-10-10
          • 1970-01-01
          • 1970-01-01
          • 2021-08-29
          • 1970-01-01
          相关资源
          最近更新 更多