UITableView didSelectRowAtIndexPath在点击时添加额外的复选标记

Pet*_*erK 5 iphone uitableview didselectrowatindexpath

当我在'didSelectRowAtIndexPath'中选择一个玩家并在所选行上添加一个复选标记时,它会添加一个额外的复选标记.

如果我点击row = 0,它会在row = 0和row = 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--;
    }
}
}
Run Code Online (Sandbox Code Playgroud)

Dav*_*ski 7

我使用动态原型单元格的故事板显示我找到此解决方案之前使用上述一些想法的状态列表

步骤1

@interface StateViewController : UITableViewController
{
    NSMutableArray *checkedIndexPaths;
}
Run Code Online (Sandbox Code Playgroud)

第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]];
    }
}
Run Code Online (Sandbox Code Playgroud)

第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]];

    }


}
Run Code Online (Sandbox Code Playgroud)

第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;
}
Run Code Online (Sandbox Code Playgroud)


Mic*_* K. 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]];
}
Run Code Online (Sandbox Code Playgroud)

然后在didSelectRowAtIndexPath:(NSIndexPath*)indexPath中执行而不是在单元格上操作,只需更改相应NSNumber对象的值即可.

然后在cellForRowAtIndexPath:(NSIndexPath*)indexPath中,通过检查player集合中的相应条目来配置单元附件.

或者,如果你真的非常顽固,你可以从cellForRowAtIndexPath:(NSIndexPath*)indexPath替换(这不是推荐)以下行:

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
Run Code Online (Sandbox Code Playgroud)

有:

UITableViewCell *cell = nil;
Run Code Online (Sandbox Code Playgroud)