ios - dequeueReusableCellWithIdentifier 和自定义单元格

标签 ios uitableview custom-cell

好的,我有一个表格,其中包含 3 个不同的原型(prototype)单元格(cellVaccination、cellAdmin、cellExpire)。在我的 cellForRowAtIndexPath 方法中,我将一个 Core Data 对象拆分到这 3 个单独的单元格中,这样表格在结构上将如下所示:

- Drug 1
  - Drug 1 Admin
  - Drug 1 Expire
- Drug 2
  - Drug 2 Admin
  - Drug 2 Expire
- Drug 3
  - Drug 3 Admin
  - Drug 3 Expire

此外,我以编程方式将 UISwitch 添加到“顶级”单元格(即药物 1)中,以便该开关可以控制辅助单元格的功能(即颜色、文本等)。这是我当前的 cellForRowAtIndexPath 的样子:

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

    // we need to adjust the indexPath because we split a single core data object into 3 different rows
    NSIndexPath *adjustedIndexPath = [NSIndexPath indexPathForRow:indexPath.row / 3 inSection:indexPath.section];
    Vaccine *vaccine = [self.fetchedResultsController objectAtIndexPath:adjustedIndexPath];

    // define the switch that will get added to the primary table rows
    UISwitch *switchview = [[UISwitch alloc] initWithFrame:CGRectZero];

    if (indexPath.row % 3 == 0) {
        static NSString *cellIdentifier = @"cellVaccination";
        VaccineTableViewCell *cell = [myTableView dequeueReusableCellWithIdentifier:cellIdentifier forIndexPath:indexPath];
        cell.vaccineName.text = vaccine.vaccineName;

        // add a switch into that table row
        cell.accessoryView = switchview;
        [switchview addTarget:self action:@selector(switchChanged:) forControlEvents:UIControlEventValueChanged];
        switchview.tag = indexPath.row;
        switchview.on = [vaccine.vaccineEnabled boolValue];

        // PROBLEM AREA BELOW
        if (switchview.on) {
            VaccineTableViewCell *cell1 = [myTableView dequeueReusableCellWithIdentifier:@"cellAdmin" forIndexPath:[NSIndexPath indexPathForItem:indexPath.row + 1 inSection:0]];
            cell1.vaccineAdmin.textColor = [UIColor redColor];
            cell1.vaccineAdminDate.textColor = [UIColor redColor];

            NSLog(@"Row %d is %@", indexPath.row, switchview.on ? @"ON" : @"OFF");
        } else {
            VaccineTableViewCell *cell1 = [myTableView dequeueReusableCellWithIdentifier:@"cellAdmin" forIndexPath:[NSIndexPath indexPathForItem:indexPath.row + 1 inSection:0]];
            cell1.vaccineAdmin.textColor = [UIColor lightGrayColor];
            cell1.vaccineAdminDate.textColor = [UIColor lightGrayColor];

            NSLog(@"Row %d is %@", indexPath.row, switchview.on ? @"ON" : @"OFF");
         }    
        //

        return cell;
    }

    else if (indexPath.row % 3 == 1) {
        VaccineTableViewCell *cell = [myTableView dequeueReusableCellWithIdentifier:@"cellAdmin" forIndexPath:indexPath];
        cell.vaccineAdminDate.text = vaccine.vaccineAdmin;
        return cell;
    }

    else if (indexPath.row % 3 == 2) {
        VaccineTableViewCell *cell = [myTableView dequeueReusableCellWithIdentifier:@"cellExpire" forIndexPath:indexPath];
        cell.vaccineExpireDate.text = vaccine.vaccineExpire;
        return cell;
    }

    else {
        // do nothing at the moment
    }
}

我遇到的问题似乎源于“问题区域下方”元素中标记的区域,更具体地说,我猜测是 dequeueReusableCellWithIdentifier。理论上,应该发生的是,当单元格首次通过核心数据对象填充时,我想测试开关是“打开”还是“关闭”并适当调整参数(例如颜色),以便在没有任何其他交互的情况下,相应的行被适本地着色。

这是怎么回事 - 假设我在 iPhone 4S 上进行模拟,屏幕显示 4 行集,或者总共 12 行(4 行,每行 3 个不同的原型(prototype))。我们还假设前 2 个打开,后 2 个关闭,同样直接从 Core Data 驱动。最初,屏幕看起来是正确的,前两项为红色,接下来的两项为灰色。然而,当我开始向上滚动我的表格时,接下来的两个(不在屏幕上)是红色的,所以模式继续。奇怪的是,当 NSLog 返回行标识符(在“问题区域”部分中看到)时,一切看起来都在识别正确的行,但显然不是,即:

vaccinations[10952:1486529] Row 0 is ON
vaccinations[10952:1486529] Row 3 is ON
vaccinations[10952:1486529] Row 6 is OFF
vaccinations[10952:1486529] Row 9 is OFF
vaccinations[10952:1486529] Row 12 is OFF
vaccinations[10952:1486529] Row 15 is OFF

我相信它与 dequeueReusableCellWithIdentifier 方法有关,但是为什么 NSLog 会正确识别行,但颜色的变化没有命中正确的行?

最佳答案

您有对 cell1 的引用,您在其中为不同的 NSIndexPath 出列一个单元格,配置该单元格的颜色,然后丢弃该单元格。我猜您正在尝试调整不同单元格(下一个单元格)的视觉外观。

这是不正确的。 cellForRowAtIndexPath 应该只调整当前单元格的状态。如果您想调整 cellAdmin 单元格的外观,您应该在 if (indexPath.row % 3 == 1) ... block 中进行调整。

因此 if (indexPath.row % 3 == 0) block 将在模型中查找以确定开关是打开还是关闭。 if (indexPath.row % 3 == 1) block 将在模型中查找以确定文本应该是什么颜色。

cellForRowAtIndexPath 不应尝试调整另一个单元格的外观。您无法保证这些将以何种顺序实例化(并且可能会有所不同,具体取决于您是否滚动,从哪个方向等)。

如果确实想要更新另一个可见的单元格,dequeueReusableCellWithIdentifier 无论如何都不是正确的方法。相反,人们会使用 [tableView cellForRowAtIndexPath:] 检索当前可见单元格的单元格(并且一定不要与类似命名的 UITableViewDataSource 方法混淆)。但是你永远不会在这种情况下这样做,因为你不知道是否已经加载了其他单元格。 (实际上,我认为在任何上下文中更新另一个单元格通常都是一种不好的做法,这违反了职责分离。)

关于ios - dequeueReusableCellWithIdentifier 和自定义单元格,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29174588/

相关文章:

ios - 从实用程序应用程序导航 Controller View 发送到实例错误的无法识别的选择器

ios - Game Center 分数同步适用于沙盒帐户吗?

objective-c - UITableView 滑动删除 : how to customize button and action?

uitableview - 无法展开 Optional.None。我无法弄清楚如何或什么

ios - Swift:按下按钮后,如何将来自三个独立文本字段的数据发布到自定义单元格中的标签上?

ios - 3 tableView Sections with custom Cell Segues swift

ios - 自定义 UITableview 单元格偶尔会在其他单元格之上重叠/复制

javascript - UIWebView 中的触摸

iOS App Distribution 作为私有(private)应用程序

ios - 从不同的类 Swift 访问 uibutton Action