Iphone uitableview使用以前的单元格加载单元格';内容

Iphone uitableview使用以前的单元格加载单元格';内容,iphone,uitableview,Iphone,Uitableview,在我的表格视图中,单元格大小适合整个屏幕大小。所以它只创建了两个单元格,但从第三个单元格开始,它使用了旧单元格的内容…所以我得到了不正确的数据 如何解决这个 谢谢 - (UITableViewCell *)tableView:(UITableView *) tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { static NSString *CellIdentifier = @"Cell"; UITabl

在我的表格视图中,单元格大小适合整个屏幕大小。所以它只创建了两个单元格,但从第三个单元格开始,它使用了旧单元格的内容…所以我得到了不正确的数据

如何解决这个

谢谢

   - (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;
            asyncImage = 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; 
}
-(UITableViewCell*)tableView:(UITableView*)tableView cellForRowAtIndexPath:(NSIndexPath*)indexPath{
静态NSString*CellIdentifier=@“Cell”;
UITableViewCell*单元格=[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
如果(单元格==nil)
cell=[[UITableViewCell alloc]initWithFrame:CellFrame重用标识符:CellIdentifier]自动释放];
对于(UIView*v在[cell.contentView子视图]中)
[v从SuperView移除];
curCellNo=indexPath.row;
如果(curCellNo==0){
最小值=0;
最大值=8;
x=0;
y=y为第一;
col=1;
}否则{
if(curCellNo=(estCoverHeight*noOfRowsInCell)){
x+=宽度;
y=0;
}
}
}
cell.selectionStyle=UITableViewCellSelectionStyleNone;
cell.contentView.transform=CGAffineTransformMakeRotation(M_PI/2);
返回单元;
}

重复使用单元格时,您没有清除单元格的内容。这是UITableView编程101。请看,有WWDC视频,你们应该在iTunesU上观看的斯坦福视频,你们喜欢的话,还有文本版的tableview编程指南,所有这些都涉及到了细胞重用

简而言之:单元被重用以节省内存。当旧单元格离开屏幕时,您可以重用它们,如果没有可以重用的单元格,则分配新的单元格。如果确实重用旧单元,则必须首先清除放置在这些单元上的所有子视图


另一方面,您可能需要一个自定义单元格,在该单元格中您可以完成在
tableView:cellForRowAtIndexPath:
中所做的大部分工作。我强烈建议您也研究一下这个问题。

重用单元格时,您没有清除单元格的内容。这是UITableView编程101。请看,有WWDC视频,你们应该在iTunesU上观看的斯坦福视频,你们喜欢的话,还有文本版的tableview编程指南,所有这些都涉及到了细胞重用

简而言之:单元被重用以节省内存。当旧单元格离开屏幕时,您可以重用它们,如果没有可以重用的单元格,则分配新的单元格。如果确实重用旧单元,则必须首先清除放置在这些单元上的所有子视图


另一方面,您可能需要一个自定义单元格,在该单元格中您可以完成在
tableView:cellForRowAtIndexPath:
中所做的大部分工作。我强烈建议您也研究一下这个问题。

这是因为您从未设置重复使用的UITableViewCell的内容


您仅在(cell==nil)和分配了新单元格时提供conetent,这是因为您从未设置重复使用的UITableViewCell的内容


只有当(cell==nil)分配了一个新的单元格时,您才提供检测值

我犯了一些错误-一旦您犯了错误并修复了一次,您永远不会忘记:)

出于性能原因,缓存UITableViewCell的实例。UITableView类为您处理所有这些。它基本上缓存了足够多的实例来覆盖全屏的单元格加上几个。这意味着一个有100行的表将运行与一个有1000行的表相同数量的实例

这意味着您有责任在内容出现时更新内容

(UITableViewCell*)tableView:(UITableView*)tableView cellForRowAtIndexPath:(NSIndexPath*)在请求单元格时调用indexPath。在您的实施中:

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

// Out here, perform content assignments

希望这能澄清一些事情。您基本上只需要将条件内部的大部分代码转移到条件外部。也就是说,我不是在宽恕你使用代码。您真的应该实现一个自定义单元格。这比你正在做的事情要容易:)

我犯了一些错误——一旦你犯了错误并修复了一次,你永远不会忘记:)

出于性能原因,缓存UITableViewCell的实例。UITableView类为您处理所有这些。它基本上缓存了足够多的实例来覆盖全屏的单元格加上几个。这意味着一个有100行的表将运行与一个有1000行的表相同数量的实例

这意味着您有责任在内容出现时更新内容

(UITableViewCell*)tableView:(UITableView*)tableView cellForRowAtIndexPath:(NSIndexPath*)在请求单元格时调用indexPath。在您的实施中:

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

// Out here, perform content assignments
希望这能澄清一些事情。您基本上只需要将条件内部的大部分代码转移到条件外部。也就是说,我不是在宽恕你使用代码。您真的应该实现一个自定义单元格。这比你正在做的更容易:)

试试这个

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:nil];
这对我很有效。我有一个组合单元格的UITableView。我也面临同样的问题。

试试这个

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:nil];

这对我很有效。我有一个组合单元格的UITableView。我也面临同样的问题。

你能发布你的
tableView:CellForRowatineXpath:
方法的实现吗?你能发布你的
tableView:CellForRowatineXpath:
方法的实现吗?如果我删除