开发者

uitableview loading the cells with previous cells' content

开发者 https://www.devze.com 2023-01-26 15:23 出处:网络
in my table view the cell size fits to the whole screen size. so it creating only two cells but from the 3rd cell its using old cell\'s content...so i am getting the incorrect data..

in my table view the cell size fits to the whole screen size. so it creating only two cells but from the 3rd cell its using old cell's content...so i am getting the incorrect data..

how to resolve this...?

thanks.

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

    static NSString *CellIdentifier = @"Cell";

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if(cell == nil) 
        cell = [[[UITableViewCell alloc] initWithFrame:CellFrame reuseIdentifier:CellIdentifier] autorelease];
    for(UIView *v in [cell.contentView subviews])
        [v removeFromSuperview];

        curCellNo = indexPath.row;
        if(curCellNo == 0){
            min = 0;
            max = 8;
            x=0;
            y=yForFirst;
            col = 1;
        }else{ 
            if(curCellNo < prevCellNo){
                min = min-12;
                max = max-12;

            }else{
                min = max;
                max = min+12;
            }
            x=0;
            y=0;
        }
        prevCellNo = curCellNo;
        NSLog(@"Max...%d",max);
        NSLog(@"Min...%d",min);
        NSLog(@"songsCount...%d",[songs count]);

        for(int i=min; i<max && i<[songs count]; i++){

            Song *thesong = [self.songs objectAtIndex:i];
            CGRect frame;
            frame.size.width=coverWidth; frame.size.height=coverHeight;
            frame.origin.x=x; frame.origin.y=y;

            LazyImageView* asyncImage = [[[LazyImageView alloc] initWithFrame:frame]autorelease];
            NSURL* url = [NSURL URLWithString:thesong.cover];
            [asyncImage loadImageFromURL:url];

            UILabel *label = [[[UILabel alloc]initWithFrame:CGRectMake(x, y+artistLabelYPos, artistLabelWidth, artistLabelHeight)]autorelease];
            label.text = [thesong.title stringByAppendingString:[@"\nby " stringByAppendingString:thesong.artist]];
            [label setTextAlignment:UITextAlignmentLeft];
            label.numberOfLines = 0;
            label.font = [UIFont systemFontOfSize:artistLabelFontSize];
            label.textColor = [UIColor whiteColor];
            label.backgroundColor = [UIColor darkTextColor];
            label.alpha = 0.75;

            UIButton *playBtn = [UIButton buttonWithType:UIButtonTypeCustom];
            playBtn.frame = CGRectMake(x+playBtnXPos, y+playBtnYPos, playBtnWidth, playBtnHeight); 
            [playBtn addTarget:self action:@selector(playBtnClicked:) forControlEvents:UIControlEventTouchUpInside];
            if(playingButton && streamer){
                if(playingButtonTag == i && [streamer isPlaying]){
                    [playBtn setBackgroundImage:[UIImage imageNamed:pauseBtnimgName] forState:UIControlStateNormal];
                    playingButton = playBtn;
                }else [playBtn setBackgroundImage:[UIImage imageNamed:playBtnimgName] forState:UIControlStateNormal];
            }else [playBtn setBackgroundImage:[UIImage imageNamed:playBtnimgName] forState:UIControlStateNormal];


            playBtn.tag = i;
            [cell.contentView addSubview:asyncImage];
            [cell.contentView addSubview:label];
            [cell.contentView addSubview:playBtn];
            label = nil;
            asy开发者_C百科ncImage = nil;



            if(curCellNo == 0){
                y += estCoverHeight;
                if(y>=(estCoverHeight*noOfRowsInCell)){
                    col++;
                    x += estCoverWidth;
                    if(col>=3)
                        y=0;
                    else 
                        y=yForFirst;
                }   
            }else{
                col =3;
                y += estCoverHeight;
                if(y>=(estCoverHeight*noOfRowsInCell)){

                    x += estCoverWidth;
                    y=0;
                }
            }

        }

    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    cell.contentView.transform = CGAffineTransformMakeRotation(M_PI/2);
    return cell; 
}


It's because you are never setting the contents of the re-used UITableViewCell's

You are only providing the conetent when (cell == nil) and a new cell is allocated


You're not clearing the contents of the cell when you reuse it. This is UITableView programming 101. Please, there are WWDC videos, Stanford videos all on iTunesU that you should be watching, tableview programming guides in text if you prefer, all that touch on cell reuse.

In a nutshell: Cells are reused to save on memory. You reuse old cells when they go off screen, allocate new ones if none can be reused. If you do reuse old cells, you have to clear any subviews you placed on them first.

On a side note, you probably want a custom cell where you do the bulk of the work you're doing in tableView:cellForRowAtIndexPath: I would strongly suggest you look into that as well.


I have made this some mistake - once you make it and fix it once, you never forget :)

Instances of UITableViewCell are cached for performance reasons. The UITableView class handles this all for you. It basically caches enough instances to cover a full screen's worth of cells plus a few. That means that a table with 100 rows will run off of the same number of instances as a table with 1000 rows.

That means it is your responsibility to update the contents as they appear.

(UITableViewCell *)tableView:(UITableView *) tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath is called when a cell is requested. In your implementation:

if(cell == nil) {
    // Instantiate the UITableViewCell here
    // Perform operations that need to occur on every cell, regardless of content
} 

// Out here, perform content assignments

Hopefully that clears a few things up. You basically just need to shift most of the code that is inside of the conditional to outside of the conditional. That said, I'm not condoning your use of code. You really should be implementing a custom cell. It is easier than what you are doing :)


just try this

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:nil];

It worked for me.I had a UITableView with composed cells.I also faced the same problems.

0

精彩评论

暂无评论...
验证码 换一张
取 消