2016-11-17 54 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 
    } 
} 
相關問題