首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >取消accessoryType在UITableView中的选择需要两次访问

取消accessoryType在UITableView中的选择需要两次访问
EN

Stack Overflow用户
提问于 2013-10-08 09:05:56
回答 2查看 143关注 0票数 0

我需要允许用户为某些项目选择多个选项。为此,我使用了以下方法。

唯一的问题是,我有两次选择行来删除accessoryView。我可以在第一个选择中选择行。然而,在选择行后,如果触摸它取消它,首先只有高亮颜色是去,然后我必须再次单击取消选择该行。

知道为什么会这样吗?我想第一次取消这一行。

代码语言:javascript
复制
- (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];
}
}

编辑:

代码语言:javascript
复制
- (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;
}
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2013-10-17 05:12:32

因为没有人回答这个问题,所以我必须自己回答。

通过使用[tableView reloadData],我终于找到了解决这种情况的方法。

在对表的数据进行任何更改之后,我可以为所有ios用户添加一个建议,即ALways重新加载表。

票数 0
EN

Stack Overflow用户

发布于 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替换为

代码语言:javascript
复制
UITableViewRowAnimationFade,
UITableViewRowAnimationRight,
UITableViewRowAnimationLeft,
UITableViewRowAnimationTop,
UITableViewRowAnimationBottom,
UITableViewRowAnimationNone,
UITableViewRowAnimationMiddle,
UITableViewRowAnimationAutomatic = 100
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/19243438

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档