Ios 如何获取TableView的第一个单元格?
我是Objective-C和iOS开发的新手,我有一个奇怪的问题,只有在一个特定的情况下。这是我的开关代码的一部分:Ios 如何获取TableView的第一个单元格?,ios,objective-c,uitableview,cell,tableviewcell,Ios,Objective C,Uitableview,Cell,Tableviewcell,我是Objective-C和iOS开发的新手,我有一个奇怪的问题,只有在一个特定的情况下。这是我的开关代码的一部分: if(nowSolo>-1){ [soloarray replaceObjectAtIndex:nowSolo withObject:[NSNumber numberWithBool:NO]]; NSIndexPath *indexPath2 = [NSIndexPath indexPathForRow:nowSolo inSection:0]; C
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等,它工作正常 您可以在两种方法中获取单元格cellForRowAtIndexPath
&didSelectRowAtIndexPath
就像您在配置或触摸时一样
在配置内部cellforrowatinexpath
delegate方法时获取特定单元格,如下代码所示:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
if (indexPath.row == 0) {
// configure cell here
}
return cell;
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
if (indexPath.row == 0) {
// do specific action on first cell
}
}
使用didSelectRowAtIndexPath
方法触摸时获取特定单元格请参见下面的代码:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
if (indexPath.row == 0) {
// configure cell here
}
return cell;
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
if (indexPath.row == 0) {
// do specific action on first cell
}
}
当您想要获取单元格时?您使用的是if条件之外的
单元格
。这是什么?用适当的代码(方法)更新你的问题。你能解释一下你需要实现什么吗??细胞选择还是别的?