滚动时,自定义UITableViewCells会相互覆盖

时间:2014-04-30 21:25:15

标签: ios uitableview custom-cell

当我向动态单元格添加标签时,如何使每个单元格都是唯一的,因为当它滚动时会相互覆盖`

`
     -(UITableViewCell *) tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath   *)indexPath
    {
        UITableViewCell *cell = [super tableView:tableView cellForRowAtIndexPath:indexPath];

                if ([cell isKindOfClass:[MessageCellInStream class]])
                {
                MessageCellInStream *mcell = (MessageCellInStream *) cell;

                MOC2CallEvent *event = [self.fetchedResultsController objectAtIndexPath:indexPath];


                    timeStampLabel = (UILabel*)[mcell viewWithTag:550411];

                        if(!timeStampLabel)
                        {

                            // If the label does not exist, create it
                            CGRect timeStampLabelRect = CGRectMake(200, 8, 100, 20);
                            timeStampLabel = [[UILabel alloc] initWithFrame:timeStampLabelRect];
                            timeStampLabel.textAlignment = NSTextAlignmentCenter;
                            timeStampLabel.font = [UIFont italicSystemFontOfSize:12];
                            [timeStampLabel setTextColor:[UIColor whiteColor]];

                            NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
                            [dateFormatter setDateFormat:@"H:mm"];
                            NSString *timeStampString = [dateFormatter stringFromDate:event.timeStamp];

                            timeStampLabel.text = timeStampString;
                            [mcell.contentView addSubview: timeStampLabel];
                        }

                }

            if ([cell isKindOfClass:[MessageCellOutStream class]])
            {
                MessageCellOutStream *mcella = (MessageCellOutStream *) cell;
                MOC2CallEvent *event = [self.fetchedResultsController objectAtIndexPath:indexPath];

                timeStampLabel = (UILabel*)[mcella viewWithTag:550410];

                if(!timeStampLabel)
                {
                    // If the label does not exist, create it
                    CGRect timeStampLabelRect = CGRectMake(0, 8, 100, 20);
                    timeStampLabel = [[UILabel alloc] initWithFrame:timeStampLabelRect];
                    timeStampLabel.textAlignment = NSTextAlignmentCenter;
                    timeStampLabel.font = [UIFont italicSystemFontOfSize:12];
                    [timeStampLabel setTextColor:[UIColor whiteColor]];
                    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
                    [dateFormatter setDateFormat:@"H:mm"];
                    NSString *timeStampString = [dateFormatter stringFromDate:event.timeStamp];

                    timeStampLabel.text = timeStampString;

                    [mcella.contentView addSubview: timeStampLabel];
                }
                //timeStampLabel = nil;
            }





        return cell;
    }

因此,每个单元格的任何重用标识符都可能有所帮助,但某些方法不起作用,每个单元格都有自己的cellIdentifier。

提前致谢

1 个答案:

答案 0 :(得分:1)

看起来只有在创建标签时才设置标签文本。一旦单元格被重用,就会找到标签(通过viewWithTag:并且没有进一步的工作。

更改表格:

id nthPartOfMyModel = [self.fetchedResultsController objectAtIndexPath:indexPath];

UILabel *someLabel = (UILabel *)[cell viewWithTag:someTag];
if (!someLabel) {
    someLabel = // alloc init, set model-invariant properties
    someLabel.tag = someTag;
    [cell addSubview:someLabel];

    NSString *text = // string representation of some aspect of nthPartOfMyModel
    someLabel.text = text;
}

以下内容:

UILabel *someLabel = (UILabel *)[cell viewWithTag:someTag];
if (!someLabel) {
    someLabel = // alloc init, set model-invariant properties
    someLabel.tag = someTag;
    [cell addSubview:someLabel];
}

id nthPartOfMyModel = [self.fetchedResultsController objectAtIndexPath:indexPath];
NSString *text = // string representation of some aspect of nthPartOfMyModel
someLabel.text = text;

看到区别?我们总是在给定索引路径的情况下取消引用我们的模型,并且我们总是更新单元格的子视图来表示我们的模型,但是当我们在单元格中找不到它们时,我们只构建这些子视图。

上述重点是“模型不变”属性。在标签中,除了label.text之外的所有内容通常都是模型不变的 - 无论我们谈论的是哪个modelItem都是一样的。但是如果框架或标签的存在取决于模型呢?没问题,只需将其移出创建条件,如下所示:

UILabel *someLabel = (UILabel *)[cell viewWithTag:someTag];
if (!someLabel) {
    someLabel = // alloc init, set model-invariant properties
    someLabel.tag = someTag;
    [cell addSubview:someLabel];
}

id nthPartOfMyModel = [self.fetchedResultsController objectAtIndexPath:indexPath];
NSString *text = // string representation of some aspect of nthPartOfMyModel
someLabel.text = text;

// this label is only visible for some rows, say based on some BOOL in the model
someLabel.alpha = (nthPartOfMyModel.doWeNeedTheLabel)? 1.0 : 0.0;