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--;
}
}
}
When i select a player in 'didSelectRowAtIndexPath' and add a checkmark on the selected row it adds an additional checkmark.
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--;
}
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我使用带有动态原型单元的故事板来显示状态列表 在找到此解决方案之前我使用了上面的一些想法
步骤 1
步骤 2
步骤 3
步骤 4
I am using storyboards with Dynamic Prototype Cells to display a list of states I used some of the ideas above before I found this solution
Step 1
Step 2
Step 3
Step 4
您面临的问题是由细胞重复使用引起的。
基本上,如果你的 UITableView 有 50 个单元格要显示,它只会创建 10 个单元格,然后在向下滚动/向上滚动时重用它们。因此,无论您对第 0 行的单元格做了什么更改,它都会在第 11 行重新显示,因为 TableView 使用相同的单元格等。
您要做的是跟踪独立于单元格选择了哪些玩家。您可以通过创建一个集合轻松实现这一点,比如说 NSMutableArray 或 NSMutableDictionary,它们将在 NSNumber 对象中存储 BOOL 值,例如。
然后在 didSelectRowAtIndexPath:(NSIndexPath *)indexPath 中,您不需要对单元格进行操作,而只需更改相应 NSNumber 对象的值。
然后在 cellForRowAtIndexPath:(NSIndexPath *)indexPath 中,通过检查玩家集合中的相应条目来配置单元附件。
或者,如果您真的非常顽固,您可以将 cellForRowAtIndexPath:(NSIndexPath *)indexPath: 中的以下行替换(不推荐)
:
The problem you are facing is caused by cell reusing.
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.
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.
Then in didSelectRowAtIndexPath:(NSIndexPath *)indexPath you do instead of operating on cell, you will simply change the value of a corresponding NSNumber object.
Then in cellForRowAtIndexPath:(NSIndexPath *)indexPath you configure cell accessory by checking the corresponding entry in players collection.
Or if you are really, really stubborn you could replace (THIS IS NOT RECOMENDED) the following line from the cellForRowAtIndexPath:(NSIndexPath *)indexPath:
with:
对于其他来到这里的人(像我一样)要了解为什么他们的表选择随机单元格,您必须在 cellForRowAtIndexPath 中添加类似以下内容:
我保留一个名为checkedIndexPaths 的 NSMutableArray,以便我知道检查了哪些indexPath。保留这样的数组可以让您轻松限制用户可以检查的单元格数量。这是我的 didSelectRowAtIndexPath 的示例:
For others coming here (like I did) to see why their table selects random cells, you have to add something like the following to your cellForRowAtIndexPath:
I keep a NSMutableArray called checkedIndexPaths so that I know which indexPaths are checked. Keeping such an array allows you to easily limit the number of cells a user can check. Here's an example of my didSelectRowAtIndexPath: