2009-10-31 72 views
1

我的tableview顯示像這樣當我滾動.......你會幫助我犯了什麼錯誤? ?任何幫助,請你看到的圖像,i的值已經超過寫入選擇........UITableview單元格的值一遍又一遍地寫入問題?

http://www.freeimagehosting.net/image.php?fa76ce6c3b.jpg

代碼

  • (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] autorelease]; 
    cell.textLabel.font = [UIFont boldSystemFontOfSize:14.0]; 
    cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator; 
} 




CGRect rectLbl2 = CGRectMake(75 ,1, 215, 32); 
CGRect rectLbl3 = CGRectMake(75 ,28, 215, 30); 
addLabel2= [[UILabel alloc] initWithFrame:rectLbl2]; 
addLabel3= [[UILabel alloc] initWithFrame:rectLbl3]; 



addLabel2.text = @"senthil"; 
[cell addSubview:addLabel2]; 
[addLabel2 release]; // for memory release 


addLabel3.text= @"senthil"; 
[cell addSubview:addLabel3]; 
[addLabel3 release]; 


return cell; 

}

+1

這將是我們更容易幫助你,如果你發佈你用它來把內容插入到細胞中的代碼。例如,你的'-tableView:cellForRowAtIndexPath:'方法中的代碼。 – 2009-10-31 04:35:51

+0

我給了我的代碼 – 2009-10-31 11:09:16

+0

,也改變了圖像 – 2009-10-31 11:52:14

回答

5

出隊的單元格已經有一個標籤,因此您要爲這些單元格添加另一個標籤。在最初的UITableViewCell創建中添加標籤並更改標籤內容。

嘗試是這樣的:

(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] autorelease]; 
            cell.textLabel.font = [UIFont boldSystemFontOfSize:14.0]; 
            cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator; 

     UILabel *lab; 
     CGRect rectLbl2 = CGRectMake(75 ,1, 215, 32); 
     lab = [[UILabel alloc] initWithFrame:rectLbl2]; 
     lab.tag = 2; 
     [cell addSubview:lab]; 
     [lab release]; 

     CGRect rectLbl3 = CGRectMake(75 ,28, 215, 30); 
     lab = [[UILabel alloc] initWithFrame:rectLbl3]; 
     lab.tag = 3; 
     [cell addSubview:lab]; 
     [lab release]; 
    } 

    ((UILabel *)[cell viewWithTag:2]).text = @"senthil"; 
    ((UILabel *)[cell viewWithTag:3]).text = @"senthil"; 

    return cell; 
} 
+0

非常感謝你.... – 2009-11-02 03:31:15

0

我認爲問題在於,每次tableview滾動時都會重新分配一個新標籤,導致它重新寫入單元格。

另請確保您正確使用dequeueReusableCellWithIdentifier。可能出現這種情況,您沒有正確出院。

這就是我可以說不看你的代碼。

+0

我已經給我的代碼 – 2009-10-31 11:10:30

+0

問題是,你每次滾動時再次分配標籤。您可以在viewDidLoad中分配標籤,然後在cellForRowAtIndexPath中設置文本。 – 2009-10-31 13:06:44

+0

通常,確保cellForRowAtIndexPath中沒有「alloc」調用。否則,每次滾動表格視圖時都會調用它,並且會導致一次又一次覆蓋同一個標籤。 – 2009-10-31 13:14:41

相關問題