if语句中的UITableViewCell

时间:2014-04-01 19:47:11

标签: ios iphone uitableview

我使用此代码而无法返回调用(错误,因为它在if语句中)。我需要这些陈述。我怎么能在这里解决它?

 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];

}

2 个答案:

答案 0 :(得分:0)

由于您在if的两个分支中声明两个相互排斥的单元格,因此您可以在两个中放置return个语句,其中相应的cell变量在范围内:

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

答案 1 :(得分:0)

您可以通过在外面声明来修复它:

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;