1

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

2 回答 2

0

You only show one use of imageView, but perhaps this modification will fix that and be instructive as well. THe idea is for any property in your cell subclass, you cannot assume what any given property might be showing. So for each one either set it or clear it:

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;
于 2012-08-27T16:00:36.937 回答
0

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

帮助我调试的是:

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

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

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

于 2012-08-27T22:22:36.540 回答