dequeueReusableCellWithIdentifier和自定义单元格

时间:2015-03-20 19:39:21

标签: ios uitableview custom-cell

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

- 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。从理论上讲,应该发生的事情是,当首次通过Core Data对象填充单元格时,我想测试开关是“开”还是“关”并适当调整参数(如颜色)以便在没有任何其他交互的情况下,相应的行被适当地着色。

这是怎么回事 - 让我们假设我在iPhone 4S上进行模拟,屏幕显示4行,或总共12行(4行3种不同的原型)。并且我们还假设前2个被打开而第2个被关闭,再次直接从Core Data驱动。最初,屏幕看起来是正确的,前两个项目是红色的,接下来的两个项目是灰色的。然而,当我开始向上滚动我的桌子时,NEXT两个(屏幕外)被涂成红色,因此图案继续。奇怪的是,当NSLog返回Row标识符(在“问题区域”部分中看到)时,所有内容看起来都在识别正确的行,但显然不是,即:

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会正确识别行,但是颜色的变化没有达到正确的行?

1 个答案:

答案 0 :(得分:1)

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

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

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

但是cellForRowAtIndexPath不应该尝试调整另一个单元格的外观。您无法保证这些将被实例化的顺序(它可能会根据您的滚动,从哪个方向等而有所不同)。

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

相关问题