简体   繁体   中英

Incompatible pointer types assigning to 'TableViewCell' from 'UITableViewCell'

I am trying this code and getting below warning

Incompatible pointer types assigning to 'GuideTableViewCell' from 'UITableViewCell'

in line

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

Full Code:

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

Also tried

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

still getting the error can any one kindly give me some help.

thanks

You have two issues in your code. It should be:

- (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. You need to cast dequeueReusableCellWithIdentifier to the proper class.
  2. When you create a new cell, it needs to be of the proper type.

You get an error because your code cannot possibly work.

Your code expects a BusinessTableViewCell. You create a UITableViewCell. You should be creating a BusinessTableViewCell.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM