0

我有一个表格,我想在其中为每个单击的单元格分配一个整数。

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



UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"MainStoryboard" bundle:nil];

Information *detailViewController = [storyboard instantiateViewControllerWithIdentifier:@"Information1"];

这将检查单元格文本是否等于“player1”并为其分配一个整数,并将标题传递给下一个视图控制器。

if([[playersearch objectAtIndex:indexPath.row]isEqual:@"John"]){

    detailViewController.infoInt=0;

    [detailViewController setTitle:[playersearch objectAtIndex:indexPath.row]];

}



if([[playersearch objectAtIndex:indexPath.row]isEqual:@"Mark"]){

    detailViewController.infoInt=1;

    [detailViewController setTitle:[playersearch objectAtIndex:indexPath.row]];



}



if([[playersearch objectAtIndex:indexPath.row]isEqual:@"Fred"]){

    detailViewController.infoInt=2;

    [detailViewController setTitle:[playersearch objectAtIndex:indexPath.row]];



}


if([[playersearch objectAtIndex:indexPath.row]isEqual:@"Matt"]){

    detailViewController.infoInt=3;

    [detailViewController setTitle:[playersearch objectAtIndex:indexPath.row]];



}

if([[filteredSearch objectAtIndex:indexPath.row]isEqual:@"Javier"]){

    detailViewController.infoInt=3;

    [detailViewController setTitle:@"player 4"];



}

if([[playersearch objectAtIndex:indexPath.row]isEqual:@"Andreas"]){

    detailViewController.infoInt=4;

    [detailViewController setTitle:[playersearch objectAtIndex:indexPath.row]];



}

if([[playersearch objectAtIndex:indexPath.row]isEqual:@"Lionel"]){

    detailViewController.infoInt=5;

    [detailViewController setTitle:[playersearch objectAtIndex:indexPath.row]];



}





[self.navigationController pushViewController:detailViewController animated:YES];

对于我的应用程序,这必须完成大约 200 次,我想知道是否有一种方法可以简化我的代码。顺便说一句,我有一个包含所有玩家姓名的财产清单。

4

2 回答 2

0

从您的数组创建一个字典数组会更有效,因此它看起来像这样:

({@"player":@"John", @"num":@0},{@"player":@"Mark", @"num":@1},{@"player":@"Fred", @"num":@3}, etc}

然后你只需要 2 行而不需要 if 语句:

   detailViewController.infoInt= playersearch[indexPath.row][@"num"];
   [detailViewController setTitle:playersearch[indexPath.row][@"player"]];
于 2013-01-06T05:09:55.010 回答
0

如果分配的整数等于单元格索引。你为什么不这样做:

[detailViewController setInfoInt:indexPath.row];
[detailViewController setTitle:[playersearch objectAtIndex:indexPath.row]];
[self.navigationController pushViewController:detailViewController animated:YES];
于 2013-01-06T15:37:22.460 回答