UITableView didSelectRowAtIndexPath在点击时添加额外的复选标记 [英] UITableView didSelectRowAtIndexPath add additional checkmark at tap

查看:135
本文介绍了UITableView didSelectRowAtIndexPath在点击时添加额外的复选标记的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

When i select a player in 'didSelectRowAtIndexPath' and add a checkmark on the selected row it adds an additional checkmark.

如果我点击row = 0,它会添加一个checkmark to row = 0 and row = 11.这意味着两行标记为一次。如果我点击row = 1,它会为row = 10添加一个额外的复选标记,因此它会向前添加10行复选标记。看起来它只是添加了复选标记,因为玩家没有进入实际的玩家列表。

If i tap row = 0 it adds a checkmark to row = 0 and row = 11. This means that two row's are marked by one tap. If i tap row = 1 it adds an extra checkmark to row = 10 so it adds checkmark 10 rows forward. It seems like it only add the checkmark as the player does not get into the actual player-list.

任何帮助都会非常感激。

Any help would be very much appreciated.

- (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--;
    }
}
}


推荐答案

您遇到的问题是由细胞重复使用引起的。

The problem you are facing is caused by cell reusing.

基本上,如果您的UITableView有50个要显示的单元格,它只会创建10个,然后在向下滚动/向上滚动时重复使用它们。因此,无论您对第0行的单元格做了什么更改,它都会重新显示第11行,因为TableView使用相同的单元格等。

Basically, if your UITableView has, let say 50 cells to display, it creates only 10 and then reuse them as you scroll down / scroll up. So whatever changes you did to the cell at row 0, it will be re-displayed for the row 11 as TableView uses the same cell etc.

您想要做什么是跟踪哪些玩家是独立于小区选择的。你可以通过创建一个集合来轻松实现这一点,比如NSMutableArray或NSMutableDictionary,它将在NSNumber对象中存储BOOL值,例如。

What you want to do is to keep track of which players have been selected independently from cell. You can achieve that easily by creating a collection, let say NSMutableArray or NSMutableDictionary, which will store BOOL values in NSNumber objects, eg.

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

然后在 didSelectRowAtIndexPath:(NSIndexPath *)indexPath 如果不是在单元格上操作,只需更改相应NSNumber对象的值。

Then in didSelectRowAtIndexPath:(NSIndexPath *)indexPath you do instead of operating on cell, you will simply change the value of a corresponding NSNumber object.

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

Then in cellForRowAtIndexPath:(NSIndexPath *)indexPath you configure cell accessory by checking the corresponding entry in players collection.

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

Or if you are really, really stubborn you could replace (THIS IS NOT RECOMENDED) the following line from the cellForRowAtIndexPath:(NSIndexPath *)indexPath:

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

with:

UITableViewCell *cell = nil;

这篇关于UITableView didSelectRowAtIndexPath在点击时添加额外的复选标记的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆