2011-03-20 54 views
5

當我在'didSelectRowAtIndexPath'中選擇播放器並在所選行上添加複選標記時,它會添加一個額外複選標記。UITableView didSelectRowAtIndexPath在水龍頭處添加額外複選標記

如果我點擊row = 0,它會爲行= 0和行= 11添加一個複選標記。這意味着兩行由一個水龍頭標記。如果我點擊row = 1,它會爲row = 10添加一個額外的複選標記,因此它會向前添加複選標記10行。它似乎只是添加了複選標記,因爲玩家沒有進入真正的玩家列表。

任何幫助將非常感激。

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

UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath]; 

NSLog(@"indexPath: %i", indexPath.row); 

// To many players selected 
if (nrOfSelectedPlayers == 6) { //This is max players allowed 
    UIAlertView *alertPlayer = [[UIAlertView alloc] initWithTitle:@"VARNING" 
                  message:@"Du kan maximalt spela \n med sex spelare!" 
                 delegate:self 
               cancelButtonTitle:@"Tillbaka" 
               otherButtonTitles:nil]; 

    [alertPlayer show]; 
    [alertPlayer release]; 
    nrOfSelectedPlayers--; 
    checkDeletePlayer = YES; 
} 
else { 

    // Handle the number of selected players to be able to delete player +6 
    if (checkDeletePlayer == YES) { 
     checkDeletePlayer = NO; 
     nrOfSelectedPlayers++; 
    } 


    if (cell.accessoryType == UITableViewCellAccessoryNone) { 
     cell.accessoryType = UITableViewCellAccessoryCheckmark; 
     [selectedPlayersArray addObject:cell.textLabel.text]; 
     nrOfSelectedPlayers++; 
    } 
    else { 
     cell.accessoryType = UITableViewCellAccessoryNone; 
     selectedPlayer = cell.textLabel.text; 

     for (int oo = 0; oo < nrOfSelectedPlayers; oo++) { 
      if ([selectedPlayersArray objectAtIndex:oo] == cell.textLabel.text) { 
       [selectedPlayersArray removeObjectAtIndex:oo]; 
       nrOfSelectedPlayers--; 
      } 
     } 
     //nrOfSelectedPlayers--; 
    } 
} 
} 
+1

THX夥計你的問題,你的代碼片段保存我問另一個重複的問題! – codejunkie 2012-03-20 20:21:34

+0

我們歡迎您的傢伙 – PeterK 2012-03-20 21:55:21

回答

6

您面臨的問題是由單元重複使用造成的。

基本上,如果你的UITableView有,比方說50個單元格顯示,它只創​​建10個,然後在你向下滾動/向上滾動時重用它們。因此,無論您對第0行的單元格所做的更改如何,都將重新顯示第11行,因爲TableView使用同一個單元格等。

您想要做的是跟蹤哪些玩家被獨立選中從細胞。你可以通過創建一個集合來輕鬆實現,例如NSMutableArray或NSMutableDictionary,它將在NSNumber對象中存儲BOOL值,例如。

NSMutableArray *players = [NSMutableArray arrayWithCapacity:50]; 
for (int i = 0; i < 50; i++) { 
    [players addObject:[NSNumber numberWithBool:NO]]; 
} 

然後在didSelectRowAtIndexPath方法:(NSIndexPath *)indexPath你做的,而不是在小區中操作,您只需更改相應的NSNumber對象的值。

然後在cellForRowAtIndexPath:(NSIndexPath *)indexPath您可以通過檢查播放器集合中的相應條目來配置單元附件。

或者如果你是真的,真的很固執,你可以取代(這是不推薦)從的cellForRowAtIndexPath以下行:(NSIndexPath *)indexPath

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 

有:

UITableViewCell *cell = nil; 
+0

您能否在'cellForRowAtIndexPath'中顯示示例? – PeterK 2011-03-20 20:34:11

+0

修正了,謝謝ALOT :-) – PeterK 2011-03-20 20:58:55

+0

不客氣。 – 2011-03-20 23:09:34

2

對於其他人來到這裏(像我這樣),看看他們的表爲什麼選擇隨機單元格,你必須添加類似以下內容到您的cellForRowAtIndexPath:

// Assume cell not checked; 
[cell setAccessoryType:UITableViewCellAccessoryNone]; 
for (int i = 0; i < checkedIndexPaths.count; i++) { 
    NSUInteger num = [[checkedIndexPaths objectAtIndex:i] row]; 

    if (num == indexPath.row) { 
     [cell setAccessoryType:UITableViewCellAccessoryCheckmark]; 
    } 
} 

我保留一個名爲checkedIndexPaths的NSMutableArray,以便知道檢查了哪些indexPaths。保持這樣一個數組可以讓您輕鬆限制用戶可以檢查的單元格數量。這裏是我的didSelectRowAtIndexPath方法的一個例子:

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath]; 

    // uncheck if already checked 
    if (cell.accessoryType == UITableViewCellAccessoryCheckmark) { 
     cell.accessoryType = UITableViewCellAccessoryNone; 
     [checkedIndexPaths removeObject:indexPath]; 
    } 
    else { 
     // make sure no more than 3 are selected 
     if ([checkedIndexPaths count] < 3) { 

      // check row 
      cell.accessoryType = UITableViewCellAccessoryCheckmark; 
      // add it to our list of checked indexes 
      [checkedIndexPaths addObject:indexPath]; 
     } else { 
      UIAlertView* alert = [[UIAlertView alloc] initWithTitle:@"Note" 
                  message:@"You can only select 3 rows." 
                  delegate:nil 
                cancelButtonTitle:@"OK" otherButtonTitles:nil]; 
      [alert show]; 
     } 
    } 
    [tableView deselectRowAtIndexPath:indexPath animated:YES]; 
} 
7

我使用與動態原型細胞情節串連圖板,以顯示我用的一些想法以上之前,我發現這個解決方案的狀態列表

步驟1

@interface StateViewController : UITableViewController 
{ 
    NSMutableArray *checkedIndexPaths; 
} 

步驟2

(void)viewDidLoad 
{ 
    [super viewDidLoad]; 
    self.states = [[GAIGStateStore sharedInstance]allStates]; 

    //Setup default array to keep track of the checkmarks 
    checkedIndexPaths = [NSMutableArray arrayWithCapacity:self.states.count]; 
    for (int i = 0; i < self.states.count; i++) { 
     [checkedIndexPaths addObject:[NSNumber numberWithBool:NO]]; 
    } 
} 

步驟3

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

    //This toggles the checkmark 
    UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath]; 

    if (cell.accessoryType == UITableViewCellAccessoryNone) 
    { 
     cell.accessoryType = UITableViewCellAccessoryCheckmark; 
     //This sets the array 
     [checkedIndexPaths replaceObjectAtIndex:indexPath.row withObject:[NSNumber numberWithBool:YES]]; 

    } else 
    { 
     cell.accessoryType = UITableViewCellAccessoryNone; 
     //This sets the array 
     [checkedIndexPaths replaceObjectAtIndex:indexPath.row withObject:[NSNumber numberWithBool:NO]]; 

    } 


} 

步驟4

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:stateCell forIndexPath:indexPath]; 

    UILabel *stateLabel = (UILabel *)[cell viewWithTag:1000]; 

    StateProvince *myState = [self.states objectAtIndex:indexPath.row]; 

    stateLabel.text = myState.label; 

    //Now set the check marks 
    // Assume cell not checked; 
    [cell setAccessoryType:UITableViewCellAccessoryNone]; 

    NSNumber *num = [checkedIndexPaths objectAtIndex:indexPath.row]; 


    if (num == [NSNumber numberWithBool:YES]) { 
      [cell setAccessoryType:UITableViewCellAccessoryCheckmark]; 
    } 


    return cell; 
} 
+0

代碼模型是完美的,但從我的最後一個建議是,你需要註釋行dequeueReusableCellWithIdentifier,因爲它將使用相同的單元格顯示其他單元格滾動,並將顯示在那裏的複選標記選項。 – Radix 2012-12-04 05:13:48

相關問題