質問

how can i make each cell unique when i add a label to the dynamic cells because when it is scrolled they overwrite each other`

`
     -(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;
    }

So any reuse identifier for each cell may help but some methods didnt work each cell has its own cellIdentifier.

Thanks in advance

役に立ちましたか?

解決

It looks like the label text is being set only when the label is created. Once the cell gets reused, the label is found (via viewWithTag: and no further work is done.

Change the form:

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;
}

To the following:

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;

See the difference? We always dereference our model given the index path, and we always update our cell's subviews to represent our model, but we only build those subviews when we can't find them in the cell.

An important point in the above is "model-invariant" properties. In labels, often everything but the label.text is model-invariant -- the same no matter which modelItem we're talking about. But what if the frame or the very presence of a label depends on the model? No problem, just move that out of the create condition, like this:

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;
ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top