简体   繁体   中英

UITableViewCell in if statement

I use this code and I can not return call (error because it in if statement). I need this statements. How can I fix it here?

 static NSString *cellIdentifierCell = @"Cell";
static NSString *cellIdentifierCol = @"Col";
WallPost *wallPost = self.dataSource[indexPath.row];


if ([wallPost.images count] >= 2)
{
    AFTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifierCol];
    if (cell == nil)
    {
        cell = [[AFTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifierCol];
    }
}

       if ([wallPost.images count] < 2)
{
    FeedCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifierCell];
    if (cell == nil)
    {
        cell = [[FeedCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifierCell];

   //some code
    }
    cell.cellTextLabel.text = wallPost.text;
    cell.cellTextLabel.numberOfLines = 0;

    cell.cellLikeLabel.text = [NSString stringWithFormat:@"%@", wallPost.likesCount];
     cell.cellCommentsLabel.text = [NSString stringWithFormat:@"%@", wallPost.commentsCount];
    cell.cellDateLabel.text = [NSString stringWithFormat:@"%@", wallPost.pubDate];

}

Since you are declaring two cells in two branches of if that are mutually exclusive, you could place return statements in both, where the corresponding cell variable is in scope:

if ([wallPost.images count] >= 2) {
    AFTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifierCol];
    if (cell == nil) {
        cell = [[AFTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifierCol];
    }
    return cell;
} else { // [wallPost.images count] < 2
    FeedCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifierCell];
    if (cell == nil) {
        cell = [[FeedCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifierCell];
        //some code
    }
    cell.cellTextLabel.text = wallPost.text;
    cell.cellTextLabel.numberOfLines = 0;

    cell.cellLikeLabel.text = [NSString stringWithFormat:@"%@", wallPost.likesCount];
    cell.cellCommentsLabel.text = [NSString stringWithFormat:@"%@", wallPost.commentsCount];
    cell.cellDateLabel.text = [NSString stringWithFormat:@"%@", wallPost.pubDate];
    return cell;
}

You can fix it by declaring it outside:

static NSString *cellIdentifierCell = @"Cell";
static NSString *cellIdentifierCol = @"Col";
WallPost *wallPost = self.dataSource[indexPath.row];

// Base class pointer
UITableViewCell *returnCell = nil;

if ([wallPost.images count] >= 2)
{
    AFTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifierCol];
    if (cell == nil)
    {
        cell = [[AFTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifierCol];
        returnCell = cell;
    }
}

if ([wallPost.images count] < 2)
{
    FeedCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifierCell];
    if (cell == nil)
    {
        cell = [[FeedCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifierCell];
    }
    cell.cellTextLabel.text = wallPost.text;
    cell.cellTextLabel.numberOfLines = 0;

    cell.cellLikeLabel.text = [NSString stringWithFormat:@"%@", wallPost.likesCount];
    cell.cellCommentsLabel.text = [NSString stringWithFormat:@"%@", wallPost.commentsCount];
    cell.cellDateLabel.text = [NSString stringWithFormat:@"%@", wallPost.pubDate];
    returnCell = cell;

}

return returnCell;

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