2016-11-17 84 views
0

我是新的Objective-C和iOS开发人员,我有一个奇怪的问题,只在一个特定的情况。这是我的开关部分代码:如何获取TableView的第一个单元格?

if(nowSolo>-1){ 
    [soloarray replaceObjectAtIndex:nowSolo withObject:[NSNumber numberWithBool:NO]]; 
    NSIndexPath *indexPath2 = [NSIndexPath indexPathForRow:nowSolo inSection:0]; 
    CustomCell *cell2=[_tableView cellForRowAtIndexPath:nowSolo]; 
    cell2.solo.backgroundColor=[UIColor colorWithRed:0.29 green:0.67 blue:0.62 alpha:1.0]; 
} 
nowSolo=row; 
[soloarray replaceObjectAtIndex:row withObject:[NSNumber numberWithBool:YES]]; 
cell.solo.backgroundColor=[UIColor colorWithRed:0.13 green:0.41 blue:0.37 alpha:1.0]; 

一切顺利,但是当可变nowSolo等于0,则cell2 = null。我不明白为什么,因为像1,2,3等其他值它工作正常。

+0

当你想要得到的细胞 ? – vaibhav

+0

如果条件之外使用'cell'。它是什么 ? – Mahesh

+0

用适当的代码更新你的问题(方法) –

回答

1

你可以得到的细胞这两种方法cellForRowAtIndexPath & didSelectRowAtIndexPath当你配置,或当你触摸内部环境。

获取特定的细胞,当你配置内cellForRowAtIndexPath委托方法如下面的代码:

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

    if (indexPath.row == 0) { 
     // configure cell here 
    } 
    return cell; 
} 

获取特定的细胞,当你触摸使用didSelectRowAtIndexPath方法请参见下面的代码:

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { 

    UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath]; 
    if (indexPath.row == 0) { 
      // do specific action on first cell 
    } 
} 
相关问题