UITableView单元格在高度更改后显示错误的内容

时间:2015-02-23 16:43:44

标签: ios objective-c uitableview nsindexpath heightforrowatindexpath

我正在努力实现一个包含三个部分的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添加到它的视图中,该视图不应该存在,如下所示:

enter image description here

以下是我为第三部分实施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;
}

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

1 个答案:

答案 0 :(得分:2)

由于与tableView:cellForSubscribedNotificationsRowAtIndexPath:方法中的回收单元格相关的错误,存在问题。更改其中一个单元格的高度会使此错误可见。

您需要在default案例中添加代码,以删除您为RowOneRowTwo添加的附件和单元格文字:

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

否则,RowOneRowTwo以外的行中的“已回收”单元格(以前为RowOneRowTwo)将包含旧文本和附件视图在细胞被回收之前就存在了。

解决此问题的另一种方法是overriding prepareForReuse