gpt4 book ai didi

UITableView 多对勾选择

转载 作者:行者123 更新时间:2023-12-03 14:35:52 25 4
gpt4 key购买 nike

阅读了几个与此相关的问题后,我可以使该功能正常工作。然而,我的列表很长而且可以滚动,所以当我向上和向下滚动时,复选标记的选择到处都是。 selectedIndexes 是一个 NSMutableArray .有任何想法吗?

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

if ([selectedCell accessoryType] == UITableViewCellAccessoryNone) {
[selectedCell setAccessoryType:UITableViewCellAccessoryCheckmark];
[selectedIndexes addObject:[NSNumber numberWithInt:indexPath.row]];

}
else {
[selectedCell setAccessoryType:UITableViewCellAccessoryNone];
[selectedIndexes removeObject:[NSNumber numberWithInt:indexPath.row]];

}

[tableView deselectRowAtIndexPath:indexPath animated:NO];

}

在我的 cellForRowAtIndexPath:我有以下检查。
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

static NSString *CellIdentifier = @"CustomCell";

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
}

// Configure the cell...
NSDictionary *dictionary = [natures objectAtIndex:indexPath.section];
NSArray *array = [dictionary objectForKey:@"Name"];
NSString *cellValue = [array objectAtIndex:indexPath.row];
cell.textLabel.text = cellValue;

for (int i = 0; i < selectedIndexes.count; i++) {
NSUInteger num = [[selectedIndexes objectAtIndex:i] intValue];

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

}

return cell;
}

最佳答案

对于多选,您只需要在 viewDidLoad

self.tableView.allowsMultipleSelection = YES;


- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *tableViewCell = [tableView cellForRowAtIndexPath:indexPath];
tableViewCell.accessoryView.hidden = NO;
// if you don't use a custom image then tableViewCell.accessoryType = UITableViewCellAccessoryCheckmark;
}

- (void)tableView:(UITableView *)tableView didDeselectRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *tableViewCell = [tableView cellForRowAtIndexPath:indexPath];
tableViewCell.accessoryView.hidden = YES;
// if you don't use a custom image then tableViewCell.accessoryType = UITableViewCellAccessoryNone;
}

关于UITableView 多对勾选择,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6023883/

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