gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-03 18:50:55 25 4
gpt4 key购买 nike

当我在“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--;
}
}
}

最佳答案

我正在使用带有动态原型(prototype)单元的 Storyboard来显示状态列表,在找到此解决方案之前我使用了上面的一些想法

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

关于iphone - UITableView didSelectRowAtIndexPath 在点击时添加额外的复选标记,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5370311/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com