2013-08-05 30 views
0

我有一个UITableView数据显示,但当它滚动数据(UILabel)要么消失,要么他们一遍又一遍地重复添加在彼此的顶部。每当我滚动每一次细胞交换数据。UITableView数据显示多次滚动

这里是我的cellForRowAtIndexPath:代码

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
    { 
     static NSString *cellIdentifier = @"Cell"; 

     UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier]; 

     if(cell == nil) { 
      cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier]; 

     } 

     cell.selectionStyle = UITableViewCellSelectionStyleNone; 

     // configure the cell's background 
     UIImageView *gradient = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"gradient"]]; 
     [cell.contentView addSubview:gradient]; 

     // configure the cell's label 
     UILabel *nameLabel = [[UILabel alloc] initWithFrame:CGRectMake(50, 130, 300, 44)]; 

     // grab a reference to the label's text from the tableData 
     nameLabel.text = [name objectAtIndex:indexPath.row]; 
     nameLabel.textColor = [UIColor blackColor]; 
     nameLabel.font = [UIFont fontWithName:@"DIN-Bold" size:12]; 
     nameLabel.backgroundColor = [UIColor clearColor]; 

     // set the autoReiszing mask -- this way if the label spills over the editing 
     // [icon?] then the text will trail off in ... 
     nameLabel.autoresizingMask = UIViewAutoresizingFlexibleWidth; 

     [cell.contentView addSubview:nameLabel]; 

     // configure the cell's label 
     UILabel *tableTextViewLbl = [[UILabel alloc] initWithFrame:CGRectMake(50, 80, 220, 50)]; 

     // grab a reference to the label's text from the tableData 
     tableTextViewLbl.text = [message objectAtIndex:indexPath.row]; 
     tableTextViewLbl.textColor = [UIColor blackColor]; 
     tableTextViewLbl.font = [UIFont fontWithName:@"DIN" size:10]; 
     tableTextViewLbl.backgroundColor = [UIColor clearColor]; 
     tableTextViewLbl.autoresizingMask = UIViewAutoresizingFlexibleWidth; 

     [cell.contentView addSubview:tableTextViewLbl]; 

     // configure the cell's label 
     UILabel *tableTimeStampViewLbl = [[UILabel alloc] initWithFrame:CGRectMake(50, 30, 200, 50)]; 

     // grab a reference to the label's text from the tableData 
     tableTimeStampViewLbl.text = [timeStamp objectAtIndex:indexPath.row]; 
     tableTimeStampViewLbl.textColor = [UIColor lightGrayColor]; 
     tableTimeStampViewLbl.font = [UIFont fontWithName:@"DIN" size:7]; 
     tableTimeStampViewLbl.backgroundColor = [UIColor clearColor]; 
     tableTimeStampViewLbl.autoresizingMask = UIViewAutoresizingFlexibleWidth; 

     [cell.contentView addSubview:tableTimeStampViewLbl]; 

    // UIImageView *image; 
    // UIImage *image1=[UIImage imageNamed:@"rest.png"]; 
    // image=[[UIImageView alloc]initWithImage:image1]; 
    // image.frame=CGRectMake(10,30,40,30); 
    // 
    // [cell.contentView addSubview:image]; 
    // 


     return cell; 

    } 
+1

不,请不要接受这个答案。这可能是你在这种情况下可能做的最糟糕的事情。你几乎可以保证有内存问题,表格会非常缓慢地工作。请投票回答。使用@Stavash答案,这是正确的,并将解决您的问题。 – Fogmeister

回答

3

您要为每一个细胞被加载/重新加载到视图时间创建一个UILabel实例。这不是如何完成的。相反,将UILabel作为属性(可能是IBOutlet)添加到UITableView子类中,并在cellForRowAtIndexPath:中进行更改。

所以你会有一个新的类,继承自UITableViewCell - 我们称之为MyCustomCell。

在MyCustomCell.h:

@interface MyCustomCell : UITableViewCell 

@property (weak, nonatomic) IBOutlet UILabel *nameLabel; 

@end 

MyCustomCell.xib将限定的UILabel位置和构型,当然需要将与nameLabel属性相关联。

在cellForRowAtIndexPath中,而不是实例化一个新的UILabel,您只需引用cell.nameLabel并更改文本。一定要定义为resuseIdentifier在Interface Builder细胞类和使用实例是:

MyCustomCell *cell = [tableView dequeueReusableCellWithIdentifier:@"MyIdentifier"]; 

if (!cell) { 
    cell = [[MyCustomCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"MyIdentifier"]; 

} 
+0

最佳答案 – Fogmeister

+1

完美!非常感谢! – user2588945

+1

没问题。每当你准备用UITableViews做一些更复杂的事情时,试着给这个读一下:http://stavash.wordpress.com/2012/12/14/advanced-issues-asynchronous-uitableviewcell-content-loading-done-right/ – Stavash

-2

用这种方式

if (cell == nil) { 
    cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier]; 
} 
else{ 
    cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier]; 
} 

但是考虑自定义单元格是最好的答案。检查此链接提供最佳解决方案custom cell

+0

它有什么问题。有没有人检查过它? –

+0

@ user2588945你检查了我的答案吗? –

+0

是的,这是行不通的。他仍然会遇到同样的问题。此外,你在做什么是没有意义的。你的if和else语句都做同样的事情。 (顺便说一句,我没有投票)。 – Fogmeister

0

在您按照我的回答之前,我想告诉您,以下代码对内存管理不利,因为它将为每行UITableView创建新单元,因此请小心。

但是最好使用,当UITableView有限行(大约50-100可能是)那么下面的代码对你有帮助,如果它适合你使用它。

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

    NSString *CellIdentifier = [NSString stringWithFormat:@"S%1dR%1d",indexPath.section,indexPath.row]; 
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 
    if(cell == nil) 
    { 
     cell = [[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease]; 

     /// Put your code here 
    } 

    return cell; 
} 

如果您的行数有限,那么这是最适合您的代码。

0

首先您需要了解“UITableView如何工作?”其实UITableView的电池概念是,每次您滚动表视图,它不是为您创造新的细胞,它只是重用细胞与cellIdentifier

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];的帮助,那时候我们只是需要更新单元格的数据..多数民众赞成它..最好。快乐编码...谢谢...嗯,我这样做只是......你可以看到下面:::

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
    { 
     static NSString *cellIdentifier = @"Cell"; 

    UILabel *nameLabel = nil; 

    UILabel *tableTextViewLbl= nil; 

    UILabel *tableTimeStampViewLbl= nil; 
    //Here we are , reuse the cells... 
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier]; 



    if(cell == nil) { 
     cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier]; 

     cell.selectionStyle = UITableViewCellSelectionStyleNone; 

     // configure the cell's background 
     UIImageView *gradient = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"gradient"]]; 
     [cell.contentView addSubview:gradient]; 

     // configure the cell's label 
     nameLabel = [[UILabel alloc] initWithFrame:CGRectMake(50, 130, 300, 44)]; 

     // grab a reference to the label's text from the tableData 
     nameLabel.textColor = [UIColor blackColor]; 
     nameLabel.font = [UIFont fontWithName:@"DIN-Bold" size:12]; 
     nameLabel.backgroundColor = [UIColor clearColor]; 
     nameLabel.tag = 111;//Giving this component to tag so we can access it 

     // set the autoReiszing mask -- this way if the label spills over the editing 
     // [icon?] then the text will trail off in ... 
     nameLabel.autoresizingMask = UIViewAutoresizingFlexibleWidth; 

     [cell.contentView addSubview:nameLabel]; 

     // configure the cell's label 
     tableTextViewLbl = [[UILabel alloc] initWithFrame:CGRectMake(50, 80, 220, 50)]; 
     tableTextViewLbl.textColor = [UIColor blackColor]; 
     tableTextViewLbl.font = [UIFont fontWithName:@"DIN" size:10]; 
     tableTextViewLbl.backgroundColor = [UIColor clearColor]; 
     tableTextViewLbl.autoresizingMask = UIViewAutoresizingFlexibleWidth; 
     tableTextViewLbl.tag = 222;//Giving this component to tag so we can access it 
     [cell.contentView addSubview:tableTextViewLbl]; 

     // configure the cell's label 
     tableTimeStampViewLbl = [[UILabel alloc] initWithFrame:CGRectMake(50, 30, 200, 50)]; 
     tableTimeStampViewLbl.textColor = [UIColor lightGrayColor]; 
     tableTimeStampViewLbl.font = [UIFont fontWithName:@"DIN" size:7]; 
     tableTimeStampViewLbl.backgroundColor = [UIColor clearColor]; 
     tableTimeStampViewLbl.autoresizingMask = UIViewAutoresizingFlexibleWidth; 
     tableTimeStampViewLbl.tag = 333;//Giving this component to tag so we can access it 
     [cell.contentView addSubview:tableTimeStampViewLbl]; 
    } 



    nameLabel = (UILabel*)[cell.contentView viewWithTag:111];//Here we access the name label with the help of tag, is that we have assigned tag while making the componant. 
    nameLabel.text = [name objectAtIndex:indexPath.row]; 


    tableTextViewLbl = (UILabel*)[cell.contentView viewWithTag:222];//Here we access the name label with the help of tag, is that we have assigned tag while making the componant. 
    // grab a reference to the label's text from the tableData 
    tableTextViewLbl.text = [message objectAtIndex:indexPath.row]; 


    tableTimeStampViewLbl = (UILabel*)[cell.contentView viewWithTag:333];//Here we access the name label with the help of tag, is that we have assigned tag while making the componant. 
    // grab a reference to the label's text from the tableData 
    tableTimeStampViewLbl.text = [timeStamp objectAtIndex:indexPath.row]; 


    return cell; 

} 
1

只需添加以下代码

NSArray *subviews = [[NSArray alloc] initWithArray:cell.contentView.subviews]; 
for (UILabel *subview in subviews) 
{ 
    [subview removeFromSuperview]; 
} 
[subviews release]; 
subviews = nil; 

后 -

if (cell == nil) 
{ 
    cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault  reuseIdentifier:CellIdentifier]; 
} 

然后添加你的代码。