除了调用indexPath之外,还可以通过其他方法以编程方式selecttableView中的行

如何使用[NSIndexPath indexPathForRow:0 inSection:0]方法旁边的其他方法调用tableView中的行?

例如,tableView中的每一行都有自己的“操作键”,可以从XML中检测。 所以,我想通过使用这个“动作键”,而不是使用indexPath方法,以编程方式selecttableVIew中的行。

我已经search过,但所有的方法都调用它们的indexPath。 任何人有任何想法?

这些是我目前用于我的应用程序在tableView中select行的方法:

 -(void)viewDidAppear:(BOOL)animated;{ NSLog(@"viewDidAppear"); [super viewDidAppear:YES]; // saving an NSString NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults]; NSString *action_app = [defaults objectForKey:@"app"]; [defaults setObject:@"closed_NewVC" forKey:@"closed_NewVC"]; //will select row in tableView when receive key action from Push Notification if (([[[NSUserDefaults standardUserDefaults]objectForKey:@"app"] isEqualToString:@"a1"]) && ([AryStoreInboxItem count]>0)){ NSIndexPath *indexPath = [NSIndexPath indexPathForRow:0 inSection:0]; [self->tableView selectRowAtIndexPath:indexPath animated:YES scrollPosition:UITableViewScrollPositionMiddle]; [self tableView:self->tableView didSelectRowAtIndexPath:indexPath]; }else if (([[[NSUserDefaults standardUserDefaults]objectForKey:@"app"] isEqualToString:@"a2"]) && ([AryStoreInboxItem count]>1)){ NSIndexPath *indexPath = [NSIndexPath indexPathForRow:1 inSection:0]; [self->tableView selectRowAtIndexPath:indexPath animated:YES scrollPosition:UITableViewScrollPositionMiddle]; [self tableView:self->tableView didSelectRowAtIndexPath:indexPath]; }else if (([[[NSUserDefaults standardUserDefaults]objectForKey:@"app"] isEqualToString:@"a3"]) && ([AryStoreInboxItem count]>2)){ NSIndexPath *indexPath = [NSIndexPath indexPathForRow:2 inSection:0]; [self->tableView selectRowAtIndexPath:indexPath animated:YES scrollPosition:UITableViewScrollPositionMiddle]; [self tableView:self->tableView didSelectRowAtIndexPath:indexPath]; }else if (([[[NSUserDefaults standardUserDefaults]objectForKey:@"app"] isEqualToString:@"a4"]) && ([AryStoreInboxItem count]>3)){ NSIndexPath *indexPath = [NSIndexPath indexPathForRow:3 inSection:0]; [self->tableView selectRowAtIndexPath:indexPath animated:YES scrollPosition:UITableViewScrollPositionMiddle]; [self tableView:self->tableView didSelectRowAtIndexPath:indexPath]; }else if (([[[NSUserDefaults standardUserDefaults]objectForKey:@"app"] isEqualToString:@"a5"]) && ([AryStoreInboxItem count]>4)){ NSIndexPath *indexPath = [NSIndexPath indexPathForRow:4 inSection:0]; [self->tableView selectRowAtIndexPath:indexPath animated:YES scrollPosition:UITableViewScrollPositionMiddle]; [self tableView:self->tableView didSelectRowAtIndexPath:indexPath]; }else if (([[[NSUserDefaults standardUserDefaults]objectForKey:@"app"] isEqualToString:@"a6"]) && ([AryStoreInboxItem count]>5)){ NSIndexPath *indexPath = [NSIndexPath indexPathForRow:5 inSection:0]; [self->tableView selectRowAtIndexPath:indexPath animated:YES scrollPosition:UITableViewScrollPositionMiddle]; [self tableView:self->tableView didSelectRowAtIndexPath:indexPath]; }else if (([[[NSUserDefaults standardUserDefaults]objectForKey:@"app"] isEqualToString:@"a7"]) && ([AryStoreInboxItem count]>6)){ NSIndexPath *indexPath = [NSIndexPath indexPathForRow:6 inSection:0]; [self->tableView selectRowAtIndexPath:indexPath animated:YES scrollPosition:UITableViewScrollPositionMiddle]; [self tableView:self->tableView didSelectRowAtIndexPath:indexPath]; }else if (([[[NSUserDefaults standardUserDefaults]objectForKey:@"app"] isEqualToString:@"a8"]) && ([AryStoreInboxItem count]>7)){ NSIndexPath *indexPath = [NSIndexPath indexPathForRow:7 inSection:0]; [self->tableView selectRowAtIndexPath:indexPath animated:YES scrollPosition:UITableViewScrollPositionMiddle]; [self tableView:self->tableView didSelectRowAtIndexPath:indexPath]; }else if (([[[NSUserDefaults standardUserDefaults]objectForKey:@"app"] isEqualToString:@"d1"]) && ([AryStoreInboxItem count]>8)){ NSIndexPath *indexPath = [NSIndexPath indexPathForRow:8 inSection:0]; [self->tableView selectRowAtIndexPath:indexPath animated:YES scrollPosition:UITableViewScrollPositionMiddle]; [self tableView:self->tableView didSelectRowAtIndexPath:indexPath]; }else if (([[[NSUserDefaults standardUserDefaults]objectForKey:@"app"] isEqualToString:@"e1"]) && ([AryStoreInboxItem count]>9)){ NSIndexPath *indexPath = [NSIndexPath indexPathForRow:9 inSection:0]; [self->tableView selectRowAtIndexPath:indexPath animated:YES scrollPosition:UITableViewScrollPositionMiddle]; [self tableView:self->tableView didSelectRowAtIndexPath:indexPath]; }else if (([[[NSUserDefaults standardUserDefaults]objectForKey:@"app"] isEqualToString:@"m11"]) && ([AryStoreInboxItem count]>10)){ NSIndexPath *indexPath = [NSIndexPath indexPathForRow:10 inSection:0]; [self->tableView selectRowAtIndexPath:indexPath animated:YES scrollPosition:UITableViewScrollPositionMiddle]; [self tableView:self->tableView didSelectRowAtIndexPath:indexPath]; }else if (([[[NSUserDefaults standardUserDefaults]objectForKey:@"app"] isEqualToString:@"m12"]) && ([AryStoreInboxItem count]>11)){ NSIndexPath *indexPath = [NSIndexPath indexPathForRow:11 inSection:0]; [self->tableView selectRowAtIndexPath:indexPath animated:YES scrollPosition:UITableViewScrollPositionMiddle]; [self tableView:self->tableView didSelectRowAtIndexPath:indexPath]; }else if (([[[NSUserDefaults standardUserDefaults]objectForKey:@"app"] isEqualToString:@"b1"]) && ([AryStoreInboxItem count]>12)){ NSIndexPath *indexPath = [NSIndexPath indexPathForRow:12 inSection:0]; [self->tableView selectRowAtIndexPath:indexPath animated:YES scrollPosition:UITableViewScrollPositionMiddle]; [self tableView:self->tableView didSelectRowAtIndexPath:indexPath]; }else if (([[[NSUserDefaults standardUserDefaults]objectForKey:@"app"] isEqualToString:@"b2"]) && ([AryStoreInboxItem count]>13)){ NSIndexPath *indexPath = [NSIndexPath indexPathForRow:13 inSection:0]; [self->tableView selectRowAtIndexPath:indexPath animated:YES scrollPosition:UITableViewScrollPositionMiddle]; [self tableView:self->tableView didSelectRowAtIndexPath:indexPath]; }else if (([[[NSUserDefaults standardUserDefaults]objectForKey:@"app"] isEqualToString:@"b3"]) && ([AryStoreInboxItem count]>14)){ NSIndexPath *indexPath = [NSIndexPath indexPathForRow:14 inSection:0]; [self->tableView selectRowAtIndexPath:indexPath animated:YES scrollPosition:UITableViewScrollPositionMiddle]; [self tableView:self->tableView didSelectRowAtIndexPath:indexPath]; }else if (([[[NSUserDefaults standardUserDefaults]objectForKey:@"app"] isEqualToString:@"b4"]) && ([AryStoreInboxItem count]>15)){ NSIndexPath *indexPath = [NSIndexPath indexPathForRow:15 inSection:0]; [self->tableView selectRowAtIndexPath:indexPath animated:YES scrollPosition:UITableViewScrollPositionMiddle]; [self tableView:self->tableView didSelectRowAtIndexPath:indexPath]; }else if (([[[NSUserDefaults standardUserDefaults]objectForKey:@"app"] isEqualToString:@"b5"]) && ([AryStoreInboxItem count]>16)){ NSIndexPath *indexPath = [NSIndexPath indexPathForRow:16 inSection:0]; [self->tableView selectRowAtIndexPath:indexPath animated:YES scrollPosition:UITableViewScrollPositionMiddle]; [self tableView:self->tableView didSelectRowAtIndexPath:indexPath]; }else if (([[[NSUserDefaults standardUserDefaults]objectForKey:@"app"] isEqualToString:@"b6"]) && ([AryStoreInboxItem count]>17)){ NSIndexPath *indexPath = [NSIndexPath indexPathForRow:17 inSection:0]; [self->tableView selectRowAtIndexPath:indexPath animated:YES scrollPosition:UITableViewScrollPositionMiddle]; [self tableView:self->tableView didSelectRowAtIndexPath:indexPath]; }else if (([[[NSUserDefaults standardUserDefaults]objectForKey:@"app"] isEqualToString:@"b7"]) && ([AryStoreInboxItem count]>18)){ NSIndexPath *indexPath = [NSIndexPath indexPathForRow:18 inSection:0]; [self->tableView selectRowAtIndexPath:indexPath animated:YES scrollPosition:UITableViewScrollPositionMiddle]; [self tableView:self->tableView didSelectRowAtIndexPath:indexPath]; }else if (([[[NSUserDefaults standardUserDefaults]objectForKey:@"app"] isEqualToString:@"b8"]) && ([AryStoreInboxItem count]>19)){ NSIndexPath *indexPath = [NSIndexPath indexPathForRow:19 inSection:0]; [self->tableView selectRowAtIndexPath:indexPath animated:YES scrollPosition:UITableViewScrollPositionMiddle]; [self tableView:self->tableView didSelectRowAtIndexPath:indexPath]; }else if (([[[NSUserDefaults standardUserDefaults]objectForKey:@"app"] isEqualToString:@"b9"]) && ([AryStoreInboxItem count]>20)){ NSIndexPath *indexPath = [NSIndexPath indexPathForRow:20 inSection:0]; [self->tableView selectRowAtIndexPath:indexPath animated:YES scrollPosition:UITableViewScrollPositionMiddle]; [self tableView:self->tableView didSelectRowAtIndexPath:indexPath]; }else if (([[[NSUserDefaults standardUserDefaults]objectForKey:@"app"] isEqualToString:@"b10"]) && ([AryStoreInboxItem count]>21)){ NSIndexPath *indexPath = [NSIndexPath indexPathForRow:21 inSection:0]; [self->tableView selectRowAtIndexPath:indexPath animated:YES scrollPosition:UITableViewScrollPositionMiddle]; [self tableView:self->tableView didSelectRowAtIndexPath:indexPath]; }else if (([[[NSUserDefaults standardUserDefaults]objectForKey:@"app"] isEqualToString:@"b11"]) && ([AryStoreInboxItem count]>22)){ NSIndexPath *indexPath = [NSIndexPath indexPathForRow:22 inSection:0]; [self->tableView selectRowAtIndexPath:indexPath animated:YES scrollPosition:UITableViewScrollPositionMiddle]; [self tableView:self->tableView didSelectRowAtIndexPath:indexPath]; }else{ NSLog(@"Others key sent from Push Notification OR key/value not exist in tableView since user not update"); [[NSUserDefaults standardUserDefaults] removeObjectForKey:@"app"]; NSLog(@" key form push has been cleared! ==> %@ ! ",[[NSUserDefaults standardUserDefaults]objectForKey:@"app"] ); } } 

在处理UITableView ,最后没有build议的方式来访问单元格而不使用NSIndexPath 。 所以我build议你创build一个可靠的数据源来为单个数据/模型对象确定正确的NSIndexPath

您的数据源应该在您的操作键和索引path之间进行转换。

也就是说,您的数据源可以调入由您的XML类广播的通知。