2014-04-01 46 views
0

我使用這段代碼,我不能返回調用(因爲它在if語句中出錯)。我需要這些陳述。我如何在這裏修復它?UITableViewCell 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]; 

} 
+0

外聲明它 –

+0

@MidhunMP MP,我該怎麼辦呢,如果他們有那麼同名輸精管典型è? – user3486428

+0

當你在'{}'括號內聲明一個變量時,它在這些括號之外是不可見的。在'if'語句之前聲明你的變量,並將它們設置爲nil。 **這是您嘗試使用Objective-C之前應該瞭解的非常基本的C語言內容。** –

回答

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

謝謝,非常簡單... – user3486428

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;