2013-01-06 51 views
0

我有一個表格,我想爲每個被單擊的單元格分配一個整數。如果ios 6將整數分配給tableview中的多個單元格

- (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次工作要做,我想知道是否有在這我可以簡化我的代碼的方式。順便說一句,我有一個財產清單,其中包含所有玩家的名字。

回答

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"]]; 
+0

嘿謝謝你的答覆。我知道如何創建一個字典數組,但是({@「player」:@「John」,@「num」:@ 0},{@「player」:@「Mark」,@「num」 :@ 1},{@「player」:@「Fred」,@「num」:@ 3}等等} –

+0

@AdrianFarfan,考慮到你的問題,我想你想把這些特定的數字與那些特定的名字聯繫起來。這是不是你想要的? – rdelmar

0

如果指定的整數等於小區索引。爲什麼你不這樣做:

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

嘿謝謝..這是非常聰明的。還有,說我已經實施了一個搜索到該表視圖。我有一個過濾項目數組..當項目被過濾,細胞索引和info int會改變,我想爲過濾搜索中的每個項目分配一個名爲extraInt的新整數,我應該如何處理? –

相關問題