0

我正在实现一个包含三个部分的 UITableView。第二部分的最后一行应该显示一个 UIPicker 的实例。

因此,我已更改此特定行的高度,如下所示:

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    CGFloat rowHeight = self.tableView.rowHeight;
    if (indexPath.section == RedZoneSection && indexPath.row == MonitorConfigRow){
        rowHeight = 162;
        return rowHeight;
    }
    return rowHeight;
}

但是,当我添加该代码时,第三部分的第一行(“Only Alert From”)正在向它的视图添加一个 UISwitch,它不应该存在,如下所示:

在此处输入图像描述

下面是我cellForRowAtIndexPath为第三部分实施的地方:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForTimeOfDayRestrictionsRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"SettingsRowCell";

    UITableViewCell *cell = (UITableViewCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:CellIdentifier];
    }

    cell.backgroundColor = [UIColor colorWithRed:0.922 green:0.937 blue:0.949 alpha:1];

    switch (indexPath.row) {
        case HourTimeZoneRow:
            cell.textLabel.text = NSLocalizedString(@"Only Alert From", @"Only Alert Row");
            break;
        default:
            break;
    }
    return cell;
}

编辑在错误位置再次显示的特定 UISwitch 最初位于我的表格第一部分的第二个单元格中。以下是该cellForRowAtIndexPath部分的实现:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForSubscribedNotificationsRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"SettingsRowCell";

    UITableViewCell *cell = (UITableViewCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:CellIdentifier];
    }

    cell.backgroundColor = [UIColor colorWithRed:0.922 green:0.937 blue:0.949 alpha:1];

    UISwitch *cellSwitch = nil;

    switch (indexPath.row) {
        case RowOne:
            cell.textLabel.text = NSLocalizedString(@"Row One", @"Row One");
            cellSwitch = [[UISwitch alloc] init];
            cell.accessoryView = cellSwitch;
            break;

        case RowTwo:
            cell.textLabel.text = NSLocalizedString(@"Row Two", @"Row Two");
                cell.accessoryView = cellSwitch;
                break;
 //            cell.textLabel.text = nil ;
            cell.accessoryView = UITableViewCellAccessoryNone;
            accessoryViewIsShowing = FALSE;
            break;
    }
    if (cell.accessoryView == nil) {
        NSLog(@"Cell accessoryView is nil");
    }
    else if (cell.accessoryView != nil){
        NSLog(@"Cell accessoryView is not nil");
    }

    NSLog(@"Section: %ld, Row: %ld", (long)indexPath.section, (long)indexPath.row);

    return cell;
}

有谁知道为什么更改特定单元格的高度会导致在另一部分的单元格中显示不正确的内容?

4

1 回答 1

2

问题的存在是因为该方法中与回收单元有关的错误tableView:cellForSubscribedNotificationsRowAtIndexPath:。更改其中一个单元格的高度会使此错误可见。

您需要在default案例中添加代码以删除您为 和 添加的附件和单元格RowOne文本RowTwo

default:
    cell.textLabel.text = nil;
    cell.accessoryView = nil;
    break;

否则,除RowOneand之外的行中的“回收”单元格(RowTwo以前是RowOneor RowTwo)将包含在回收单元格之前存在的旧文本和附件视图。

解决此问题的另一种方法是覆盖prepareForReuse.

于 2015-02-23T17:04:34.003 回答