自定义UITableViewCell在TableView中滚动时隐藏内容



我有一个UITableView,其中有几个自定义单元格,其中两个包含图像视图。当它们被图像填充时,它们是可以的,但如果我向下滚动表视图,使图像视图不在视图中,然后返回,图像就会消失,留下空白的白色。

CellForRowAtIndexPath Method
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = @"Cell";
static NSString *GECIdentifier = @"GECell";
static NSString *TECIdentifier = @"TECell";
static NSString *AMCIdentifier = @"AMCell";
if (indexPath.section == 0) {
    //Create generic GEC for use by all rows in section
    GeneralEditingCell *GEC = [self.tableView dequeueReusableCellWithIdentifier:GECIdentifier];
    if (GEC == nil) {
        GEC = [[GeneralEditingCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
        GEC.accessoryType = UITableViewCellAccessoryNone;
        GEC.selectionStyle = UITableViewCellSelectionStyleNone;
        GEC.master = self;
    }
    //Create and setup datepicker and dateformatter for later use
    UIDatePicker *datePicker = [[UIDatePicker alloc]init];
    datePicker.datePickerMode = UIDatePickerModeDate;
    datePicker.tag = indexPath.row;
    [datePicker addTarget:self action:@selector(pickerChanged:) forControlEvents:UIControlEventValueChanged];
    NSDateFormatter *formatter = [[NSDateFormatter alloc]init];
    [formatter setDateFormat:@"LLL dd, yyyy"];
    //Configure each cell individually
    switch (indexPath.row) {
        case 0:
            if (editing) {
                GEC.titleField.text = [item objectForKey:@"Title"];
                GEC.dateField.text = [item objectForKey:@"Date"];
                GEC.imageView.image = [self getThumbnailforVideoAtPath:[NSURL fileURLWithPath:[item objectForKey:@"Video Path"]]];
            } else {
                GEC.dateField.text = [formatter stringFromDate:[NSDate date]];
                GEC.dateField.inputView = datePicker;
            }
            break;
        default:
            break;
    }
    return GEC;
}
if (indexPath.section == 1) {
    //Create generic TEC for use by all rows in section
    TextEditingCell *TEC = [self.tableView dequeueReusableCellWithIdentifier:TECIdentifier];
    if (TEC == nil) {
        TEC = [[TextEditingCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
        TEC.accessoryType = UITableViewCellAccessoryNone;
        TEC.selectionStyle = UITableViewCellSelectionStyleNone;
    }
    //Configure each cell individually
    switch (indexPath.section) {
        case 0:
            if (editing) {
                TEC.textField.text = @"Editing Mode";
            } else {
                TEC.label.text = @"TextEdit Cell";
            }
            break;
        default:
            break;
    }
    return TEC;
}
if (indexPath.section == 2) {
    //Create generic AMC for use by all rows in section
    AddMediaCell *AMC = [self.tableView dequeueReusableCellWithIdentifier:AMCIdentifier];
    if (AMC == nil) {
        AMC = [[AddMediaCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
        AMC.accessoryType = UITableViewCellAccessoryNone;
        AMC.selectionStyle = UITableViewCellSelectionStyleNone;
    }
    //Configure each cell individually
    switch (indexPath.row) {
        case 0:
            AMC.label.text = @"Add Video";
            break;
        case 1:
            AMC.label.text = @"Add Photos";
            break;
        case 2:
            AMC.label.text = @"Record Audio";
            break;
        case 3:
            AMC.label.text = @"Add Text Note";
            break;
        case 4:
            AMC.label.text = @"Attach Location";
            break;
        case 5:
            AMC.label.text = @"Attach Weather Data";
            break;
        case 6:
            AMC.label.text = @"Add More Tags";
            break;
        default:
            break;
    }
    return AMC;
}
//Catch any previously unhandled cell
UITableViewCell *errorCell = [self.tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (errorCell == nil) {
    errorCell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
errorCell.textLabel.text = @"ERROR! UNHANDLED CELL!!!";
return errorCell;
}

您只展示了imageView的一次使用,但这次修改可能会解决这个问题,并具有指导意义。这个想法是,对于单元格子类中的任何属性,您不能假设任何给定的属性可能显示什么。因此,对于每一个,要么设置它,要么清除它:

switch (indexPath.row) {
    case 0:
        if (editing) {
            GEC.titleField.text = [item objectForKey:@"Title"];
            GEC.dateField.text = [item objectForKey:@"Date"];
            GEC.dateField.inputView = nil;
            GEC.imageView.image = [self getThumbnailforVideoAtPath:[NSURL fileURLWithPath:[item objectForKey:@"Video Path"]]];
        } else {
            GEC.titleField.text = @"";
            GEC.dateField.text = [formatter stringFromDate:[NSDate date]];
            GEC.dateField.inputView = datePicker;
            GEC.imageView.image = nil; 
        }
        break;
    default:
        break;

我也遇到过类似的问题(滚动时数据无法保留),但我的问题是索引计数错误,单元格没有显示任何内容。

帮助我调试它的是:

  • 在"cellForRowAtIndexPath"中设置中断
  • 禁用它
  • 运行应用程序并向下滚动
  • 再次启用断点
  • 向上滚动

UITableView会自动释放屏幕外UITableViewCell行的内存,并在向上滚动时再次调用cellForRowAtIndexPath

您可能错过了这样一种情况,即您只为每行初始化UIImageView一次,但当您再次调用它时,会出现问题。

最新更新