【问题标题】:Incompatible pointer types assigning to 'TableViewCell' from 'UITableViewCell'从“UITableViewCell”分配给“TableViewCell”的不兼容指针类型
【发布时间】:2015-11-22 21:31:04
【问题描述】:

我正在尝试此代码并收到以下警告

从“UITableViewCell”分配给“GuideTableViewCell”的指针类型不兼容

排队

cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"BusinessTableViewCell"];

完整代码:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{

   BusinessTableViewCell * cell = [self.tableView dequeueReusableCellWithIdentifier:@"BusinessTableViewCell"];
   if (!cell) {
    cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"BusinessTableViewCell"];
   }

   BusinessInfo * business = self.businesses[indexPath.row];
   cell.business = business;
   return cell;
}

也试过了

BusinessTableViewCell *cell = [[UITableViewCell alloc]initWithStyle: UITableViewCellStyleDefault reuseIdentifier:@"BusinessTableViewCell"];

仍然出现错误,任何人都可以给我一些帮助。

谢谢

【问题讨论】:

    标签: ios objective-c uitableview


    【解决方案1】:

    您的代码中有两个问题。应该是:

    - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
        BusinessTableViewCell * cell = (BusinessTableViewCell *)[self.tableView dequeueReusableCellWithIdentifier:@"BusinessTableViewCell"];
    
        if(!cell)
        {
            cell = [[BusinessTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"BusinessTableViewCell"];
        }
    
        BusinessInfo * business = self.businesses[indexPath.row];
        cell.business = business;
    
        return cell;
    }
    
    1. 您需要将dequeueReusableCellWithIdentifier 转换为正确的类。
    2. 创建新单元格时,其类型必须正确。

    【讨论】:

    • 为什么需要 self.tableView dequeueReusableCellWithIdentifier。没有那个代码就可以工作。它只是发出警告。
    • 它促进了单元格的重用,使表格视图更加高效。
    • 谢谢,是的,每一位代码中的一点点性能改进都会大大提高整个应用程序的性能。
    【解决方案2】:

    您收到错误消息,因为您的代码可能无法运行。

    您的代码需要一个 BusinessTableViewCell。你创建一个 UITableViewCell。您应该创建一个 BusinessTableViewCell。

    【讨论】:

    • 代码有效但只是给出警告!
    猜你喜欢
    • 1970-01-01
    • 2014-04-29
    • 2018-11-17
    • 2014-06-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多