【发布时间】:2017-07-27 04:32:23
【问题描述】:
我想在 UITableview 中实现多行选择。但是当我选择带有复选标记的多行并向下滚动时,当我返回所选行部分时,复选标记会消失。我已经尝试了 Stackoverflow 的许多解决方案,但它没有为我工作。请任何人给我适合我的解决方案。
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *simpleTableIdentifier = @"SimpleTableCell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];
if (cell == nil)
{
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:simpleTableIdentifier];
}
if([_selectedstatearray containsObject:indexPath]) {
cell.accessoryType = UITableViewCellAccessoryCheckmark;
}
else {
cell.accessoryType = UITableViewCellAccessoryNone;
}
cell.textLabel.text = [statearray objectAtIndex:indexPath.row];
return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
NSString *cellText = cell.textLabel.text;
NSLog(@"cellText>>%@",cellText);
if (cell.accessoryType == UITableViewCellAccessoryCheckmark) {
[_selectedstatearray removeObject:cellText];
cell.accessoryType = UITableViewCellAccessoryNone;
} else {
[_selectedstatearray addObject:cellText];
cell.accessoryType=UITableViewCellAccessoryCheckmark;
}
NSLog(@"selectedarray>>%@",_selectedstatearray);
NSString *greeting = [_selectedstatearray componentsJoinedByString:@","];
NSLog(@"%@",greeting);
[tableView deselectRowAtIndexPath:indexPath animated:YES];
}
【问题讨论】:
-
你在分配你
_selectedstatearray的内存吗
标签: ios objective-c uitableview row multipleselection