【问题标题】:Objective C - ImageView (custom checkmark) in UITableViewCell stops toggling after multiple cell selectionsObjective C - UITableViewCell 中的 ImageView(自定义复选标记)在多个单元格选择后停止切换
【发布时间】:2015-09-30 23:00:56
【问题描述】:

在一个简单的列表应用程序 (iOS) 中,我的 tableViewCells 遇到了一些非常奇怪的行为。

这是我的工作应用程序的基本功能:
TableView 有 2 个部分。第一部分(例如,带有 2 个单元格)始终可见。第二部分可以使用部分标题中的自定义按钮显示/隐藏。我创建了两个类(即 FirstSectionItem 和 SecondSectionItem),它们都具有布尔属性“已完成”和其他一些属性。

编译后,应用程序按预期运行。点击单元格会显示复选标记,再次点击它们会隐藏复选标记(=自定义 imageView)。然而,在点击一些不同的单元格(随机顺序)或显示/隐藏第二部分之后,无论我点击多少单元格,复选标记(ImageViews)都会保持选中状态。一段时间后,每个单元格都被选中并且不能取消选中,但布尔值仍在不断变化。

下面是部分代码:

@property NSMutableArray *sectionTitleArray;
@property NSMutableDictionary *sectionContentDict;
@property NSMutableArray *firstSectionItems;
@property NSMutableArray *secondSectionItems;

ViewDidLoad:

- (void)viewDidLoad {

    [super viewDidLoad];

    if (!self.sectionTitleArray) {
        self.sectionTitleArray = [NSMutableArray arrayWithObjects:@"section1", @"section2", nil];
    }

    self.firstSectionItems = [[NSMutableArray alloc] init];
    self.secondSectionItems = [[NSMutableArray alloc] init];
    [self loadInitialData];

    self.sectionContentDict  = [[NSMutableDictionary alloc] init];

    self.arraySection1 = [[NSMutableArray alloc] init];

    for (int i = 0; i < [self.firstSectionItems count]; i++)
         {
             FirstSectionItem *firstSectionItem = [self.firstSectionItem objectAtIndex:i];
             [self.arraySection1 addObject:firstSectionItem.itemName];
         }

     self.arraySection2 = [[NSMutableArray alloc] init];
     for (int i = 0; i < [self.secondSectionItems count]; i++)
        {
             SecondSectionItem *secondSectionItem = [self.secondSectionItems objectAtIndex:i];
             [self.arrayFuture addObject:secondSectionItem.itemName];
        }

    [self.sectionContentDict setValue:self.arraySection1  forKey:[self.sectionTitleArray objectAtIndex:0]];
    [self.sectionContentDict setValue:self.arraySection2 forKey:[self.sectionTitleArray objectAtIndex:1]];
}

CellForRowAtIndexPath:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

     UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"ListPrototypeCell" forIndexPath:indexPath];

     // ... cell content ...

     NSArray *content = [self.sectionContentDict valueForKey:[self.sectionTitleArray objectAtIndex:indexPath.section]];

     //... constraints ...

     UIImage *checkmark = [UIImage imageNamed:@"checkmark.png"];
     UIImage *noCheckmark = [UIImage imageNamed:@"transparent.png"];
     UIImageView *imageView = [[UIImageView alloc] init];

     if (indexPath.section==0){

          FirstSectionItem *firstSectionItem = [self.firstSectionItems objectAtIndex:indexPath.row];
             imageView.image = firstSectionItem.completed ? checkmark : noCheckmark;

}


     if (indexPath.section==1){
        if(self.sec2isTapped == YES){
            SecondSectionItem *secondSectionItem = [self.secondSectionItems objectAtIndex:indexPath.row];
            imageView.image = secondSectionItem.completed ? checkmark : noCheckmark;
         }
      }

     [cell.contentView addSubview:imageView];

     // ... more constraints 


return cell;
}

didSelectRowAtIndexPath

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
     [tableView deselectRowAtIndexPath:indexPath animated:NO];

    if (indexPath.section ==0){
        FirstSectionItem *tappedItem = [self.firstSectionItems objectAtIndex:indexPath.row];
        tappedItem.completed = !tappedItem.completed;
    }
    if (indexPath.section ==1){
         SecondSectionItem *tappedItem = [self.secondSectionItems objectAtIndex:indexPath.row];
         tappedItem.completed = !tappedItem.completed;
    }

    [tableView beginUpdates];
    [tableView reloadRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationMiddle];
    [tableView endUpdates];

}

我尝试了多种方法,但似乎都没有奏效。我无法弄清楚问题是什么。

(我是 Objective C 的新手,所以有些部分可能看起来有点曲折)。

提前致谢。

【问题讨论】:

    标签: ios objective-c iphone uitableview uiimageview


    【解决方案1】:

    问题是每次 cellForRowAtIndexPath 运行时您都在初始化 UIImageView。但请记住,这些单元格会被重用,所以您正在做的是重用一个带有复选标记的单元格并在顶部添加一个新的清晰复选标记。

    你需要做的是:

    1. 在storyboard中添加imageView,给它打标签,用这个标签在代码中找到它
    2. 子类 UITableViewCell 并在其中分配正确的图像。

    有很多关于如何在网上同时进行的文章。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-11-09
      • 1970-01-01
      相关资源
      最近更新 更多