滚动浏览UITableView时,自定义UITableViewCell丢失内容

时间:2012-08-27 14:54:38

标签: objective-c ios uitableview

我有一个带有几个自定义单元格的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;
}

2 个答案:

答案 0 :(得分:0)

您只显示了一次使用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;

答案 1 :(得分:0)

我有一个类似的问题(滚动时数据不会保留)但我的是一个索引错误计数,单元格没有显示任何内容。

帮我调试的是:

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

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

你可能错过了一个案例,你只为每行初始化UIImageView但是当你再次调用它时出现问题。