ios - UITableView 单元格在高度变化后显示错误的内容

标签 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;
}

但是,当我添加该代码时,第三部分的第一行(“仅警报来自”)向它的 View 添加了一个不应该存在的 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;
}

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

最佳答案

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

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

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

否则,RowOneRowTwo 以外的行中的“回收”单元格,以前是 RowOneRowTwo,将包含回收单元格之前存在的旧文本和附件 View 。

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

关于ios - UITableView 单元格在高度变化后显示错误的内容,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28678747/

相关文章:

ios - NSURLSession block 中的方法延迟

ios - 无法将特定的CGColor应用于CGContextSetFillColor

ios - iOS 6 和 iOS 8 之间的 UITableView 渲染差异

ios - 迭代 UITableView 中的自定义单元格

ios - UITableView 是否小于 HIG 的全宽?

ios - iOS应用程序同一工作区内多个项目的xcode 4静态库链接

ios - URL 解码特殊字符 - äåö

iphone - 表单与 UITableView

ios - 如何重新创建 UITableViewController 的初始化行为?

ios - 在 iOS 中处理后台任务的正确方法是什么