4

关于刷新自定义表格单元格有很多问题,但这个问题很奇怪。我有一个 tableView,每个单元格现在都有一个自定义UILabelUIImageView.

目前只有2个细胞。第一个显示日期。当表格首次加载时,它将当前日期显示为第一个单元格中的字符串UILabel

当我选择第一个单元格时,我会展示一个处理所有日期选择的自定义类。一旦选择了日期,就会弹出这个视图,然后我会返回到表格视图。

在 上-(void)viewDidAppear,tableviews 数据被重新加载,并且应该出现新的选定日期。

但是,第一个单元格中的标签不会更新。

令人困惑的是,如果我有多个单元格都显示相同的数据,那么这些单元格都会按预期刷新并显示新日期。似乎 index: 0 处的单元格行不会刷新。

更令人困惑的是,当我查询UILabel单元格的字符串值时,它返回正确的日期。

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{

    static NSString *CellIdentifier = @"Cell";

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];

        // 
        // clears the grouped style border from each table cell
        //

        UIView *clearBgView = [[UIView alloc]initWithFrame:CGRectZero];
        [cell setBackgroundView:clearBgView];

        // label
        UILabel *dl = [[UILabel alloc]initWithFrame:CGRectMake(70.0f, 10.0f, screenWidth-100, 50)];
        [self setDetailsLabel:dl];
        dl = nil;

        [[self detailsLabel] setBackgroundColor:[UIColor colorWithRed:.1 green:.1 blue:.1 alpha:.1  ]];
        [[self detailsLabel] setTextColor:[UIColor colorWithRed:1.0f green:1.0f blue:1.0f alpha:.3f]];

        //icon for each cell
        UIImageView *ci = [[UIImageView alloc]initWithFrame:CGRectMake(10.0f, 10.0f, 50.0f, 50.0f)];
        [ci setBackgroundColor:[UIColor colorWithRed:.2 green:.2 blue:.2 alpha:.2]];
        [self setCellIcon:ci];
        ci = nil;

        //
        // set up views
        //

        [cell addSubview:[self cellIcon]];
        [cell addSubview:[self detailsLabel]];
    }

    // Configure the cell...
    [cell setSelectionStyle:UITableViewCellSelectionStyleNone];



    //populate each by row.
    NSString *dateDisplay = [self formatDate:[self dateCaught]];
    NSLog (@"date is %@", dateDisplay);
   [[self detailsLabel] setText:[self formatDate:[self dateCaught]]];

    switch (indexPath.row) { //this needs to be an integer, so return the row of the indexPath.
        case 0:
            NSLog (@"text for the cell is %@",[[self detailsLabel]text]);
            break;

        default:
            break;
    }


return cell;

}

4

2 回答 2

4

问题与您通过detailsLabel周围的方式有关。你把它保存在一个属性或一个 ivar 上self

[self setDetailsLabel:dl];

但仅当单元格不可重用时才设置它。当您重复使用单元格时,detailsLabelonself会设置为之前运行的标签,从而导致您出现各种问题。

The cleanest solution is to create your own class deriving from UITableViewCell, move the initialization code that creates labels, icons, background colors, and so on into the designated initializer, and create properties for setting label texts. With this class in place, you would be able to simplify your code as follows:

UIMyCustomTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
    cell = [[UIMyCustomTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
[cell setSelectionStyle:UITableViewCellSelectionStyleNone];
[[cell detailsLabel] setText:[self formatDate:[self dateCaught]]];
// ^--- detailsLabel can be moved to UIMyCustomTableViewCell now
于 2012-09-01T22:33:00.363 回答
0

The solution is, to not use a member variable like @property detailsLabel, but instead tags for all custom subviews. Change your code like this:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
    {
        static NSString *CellIdentifier = @"Cell";
        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
        if (cell == nil) {
            [...]

            // label
            UILabel *dl = [[UILabel alloc]initWithFrame:...];
            dl.tag = 99;
            [cell.contentView addSubview: dl];

            [...]
        }

        [...]

       UILabel *dl = [cell.contentView viewWithTag: 99];
       [dl setText:[self formatDate:[self dateCaught]]];

       [...]
       return cell;
}
于 2012-09-01T22:38:08.997 回答