2010-11-24 89 views
0

在我的表格中查看单元格大小适合整个屏幕大小。所以它只创建两个单元,但从第三个单元使用旧单元格的内容...所以我得到不正确的数据..uitableview加载单元格的内容

如何解决此问题...?

谢谢。

- (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; 
} 
+0

你可以发布你的`tableView:cellForRowAtIndexPath:`方法的实现吗? – filipe 2010-11-24 19:36:14

回答

1

您在重新使用它时没有清除单元格的内容。这是UITableView编程101.请您看看WWDC视频,iTunesU上的所有斯坦福视频,您应该关注的视频,如果您愿意,还可以在文本中使用tableview编程指南,这一切都涉及到信元再利用。

简而言之:单元格被重用以节省内存。当他们离开屏幕时,您可以重新使用旧的单元格,如果不能重新使用,则分配新的单元格。如果您重复使用旧的单元格,则必须先清除所有放置在其上的子视图。

在附注中,您可能需要一个自定义单元格,您可以在tableView:cellForRowAtIndexPath:中完成大部分工作,我强烈建议您查看一下。

+0

如果我删除子视图的单元格的contentview之前添加子视图其工作正常,但如果滚动后点击该按钮上它的崩溃...和它很难在我的情况下使用自定义单元格和即时编辑问题中的代码 – rockey 2010-11-24 22:03:55

+0

使用自定义单元格永远不难,唯一困难的是告诉自己很容易做到,因为它是。 – jer 2010-11-24 23:38:30

2

这是因为你从来没有设置的重新使用的UITableViewCell的

你只提供conetent时(细胞==零),并分配

1

一个新的单元格中的内容我已经做一些这方面的错误 - 一旦你完成并修复一次,你永远不会忘记:)

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 

希望能够解决一些问题。你基本上只需要将条件中的大部分代码转移到条件之外。这就是说,我不是在宽恕你对代码的使用。你真的应该实现一个自定义单元格。这比你在做:)

0

什么只是试试这个

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:nil]; 

它的工作对我。我曾与组成cells.I一个UITableView也面临同样的问题更容易。

相关问题