我需要允许用户为某些项目选择多个选项。为此,我使用了以下方法。
唯一的问题是,我有两次选择行来删除accessoryView。我可以在第一个选择中选择行。然而,在选择行后,如果触摸它取消它,首先只有高亮颜色是去,然后我必须再次单击取消选择该行。
知道为什么会这样吗?我想第一次取消这一行。
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
int i=indexPath.row;
if(self.multi == 1){
UITableViewCell *selectedCell = [tableView cellForRowAtIndexPath:indexPath];
NSLog(@"accessoryType:%d",selectedCell.accessoryType);
if (selectedCell.accessoryType == UITableViewCellAccessoryNone) {
selectedCell.accessoryType = UITableViewCellAccessoryCheckmark;
NSString *cellText = selectedCell.textLabel.text;
[self.selectedValues replaceObjectAtIndex:i withObject:cellText];
}else{
selectedCell.accessoryType = UITableViewCellAccessoryNone;
[self.selectedValues replaceObjectAtIndex:i withObject:@""];
}
}
else{
NSLog(@"not for multi select",nil);
UITableViewCell *selectedCell = [tableView cellForRowAtIndexPath:indexPath];
NSString *cellText = selectedCell.textLabel.text;
[self.delegate addItemViewController:self setPeculiarity:cellText setIndex:self.index];
[self dismissModalViewControllerAnimated:YES];
}
}编辑:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:nil];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
cell.textLabel.text = [pecs objectAtIndex:indexPath.row];
if(self.selectedValues.count > 0){
for(int k = 0 ; k < [selectedValues count];k++){
if ([[self.selectedValues objectAtIndex:k] isEqualToString:[pecs objectAtIndex:indexPath.row]]) {
cell.accessoryType = UITableViewCellAccessoryCheckmark;
NSLog(@"pecs:%@",[pecs objectAtIndex:indexPath.row]);
}
}
}
// for background color
UIView *bgColorView = [[UIView alloc] init];
bgColorView.backgroundColor = [UIColor colorWithRed:0.2823 green:0.4509 blue:0.8588 alpha:1.0];
[cell setSelectedBackgroundView:bgColorView];
cell.textLabel.highlightedTextColor = [UIColor whiteColor];
return cell;
}发布于 2013-10-17 05:12:32
因为没有人回答这个问题,所以我必须自己回答。
通过使用[tableView reloadData],我终于找到了解决这种情况的方法。
在对表的数据进行任何更改之后,我可以为所有ios用户添加一个建议,即ALways重新加载表。
发布于 2013-10-17 06:00:19
如果要使用[tableView reloadData],则将重新加载整个tableView。
在模拟器上不需要太多的内存和执行时间,但是在实际的iPhone / iPad / iPod上,需要更多的资源。
将[tableView reloadData]替换为[tableView reloadRowsAtIndexPaths: [NSArray arrayWithObjects: [NSIndexPath indexPathForRow: indexPath.row inSection: indexPath.section]] withRowAnimation:UITableViewRowAnimationNone];
您可以将UITableViewRowAnimationNone替换为
UITableViewRowAnimationFade,
UITableViewRowAnimationRight,
UITableViewRowAnimationLeft,
UITableViewRowAnimationTop,
UITableViewRowAnimationBottom,
UITableViewRowAnimationNone,
UITableViewRowAnimationMiddle,
UITableViewRowAnimationAutomatic = 100https://stackoverflow.com/questions/19243438
复制相似问题