2013-08-05 64 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]; 
} 

然後添加你的代碼。