ios 6将整数分配给tableview中的多个单元格

时间:2013-01-06 01:11:21

标签: ios integer tableview cells

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

- (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次,我想知道是否有一种方法可以简化我的代码。顺便说一下,我有一个属性列表,其中包含所有玩家的名字。

2 个答案:

答案 0 :(得分: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"]];

答案 1 :(得分:0)

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

[detailViewController setInfoInt:indexPath.row];
[detailViewController setTitle:[playersearch objectAtIndex:indexPath.row]];
[self.navigationController pushViewController:detailViewController animated:YES];