2012-02-20 62 views
0

我用UITableView得到了應用程序。重新加載動態表中的TableViewCell的UILabels

每個UITableViewCell中都有UILabel。我以這種方式添加標籤:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    //look at the upd 
} 

但是,當我嘗試在此表以及其他一些信息,老infromation(舊標籤)重新加載數據不消失,但仍對細胞。

它看起來像這樣:...

我應該怎麼有組織加入UILabels?

UPD

我這樣正確的代碼:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    static NSString *CellIdentifier = @"Recent Dream"; 

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 

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

     UILabel *textLabel = [[UILabel alloc] init]; 
     textLabel.frame = CGRectMake(10, 0, self.view.frame.size.width -110, 48); 
     textLabel.backgroundColor = [UIColor clearColor]; 
     textLabel.font = [UIFont boldSystemFontOfSize:16]; 
     [textLabel setTag:(int)indexPath]; 
     [cell.contentView addSubview:textLabel]; 


    } 
    // Configure the cell... 

    Dream *tempDream = [self.dreams objectAtIndex:indexPath.row]; 

    NSDateFormatter *formatter = [[NSDateFormatter alloc] init]; 
    [formatter setDateFormat:@"MM/dd/yyyy"]; 

    //Optionally for time zone converstions 
    [formatter setTimeZone:[NSTimeZone timeZoneWithName:@"..."]]; 

    NSString *stringFromDate = [formatter stringFromDate:tempDream.dateAdd]; 

    UIColor *background = [[UIColor alloc] initWithPatternImage:[UIImage imageNamed:@"bodyWrapper.png"]]; 

    cell.contentView.backgroundColor = background; 

    UILabel *textLabel = (UILabel *)[cell viewWithTag:(int)indexPath]; 
    textLabel.text = tempDream.title; 

    NSLog(@"%@",tempDream.title); 
    NSLog(@"%@",textLabel.text); 

    cell.textLabel.text = @""; 
    cell.detailTextLabel.text = stringFromDate; 

    return cell; 
} 

和輸出是這樣的:

2012-02-20 15:58:10.512 DreamerIOS[3037:10403] lllooooonggg dreeeeeeeaaaaammmm yeeeeeeah 
2012-02-20 15:58:10.513 DreamerIOS[3037:10403] (null) 
2012-02-20 15:58:10.516 DreamerIOS[3037:10403] dream to end 
2012-02-20 15:58:10.516 DreamerIOS[3037:10403] (null) 
2012-02-20 15:58:10.519 DreamerIOS[3037:10403] adsfhadskgh dfagfadkuy gadyv dsfdfad fsdf a ghfncdhg hjfg f dfj ghf 
2012-02-20 15:58:10.519 DreamerIOS[3037:10403] (null) 

所以的NSLog(@ 「%@」,tempDream.title) ;是好的,但NSLog(@「%@」,textLabel.text);一片空白。爲什麼?

回答

1

這是因爲每次你添加一個新的標籤到單元格。

當你初始化一個新的細胞,可以添加標籤和標籤設置爲它

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

    // add label here 
    ... 
    [label setTag:999]; 
} 

然後,當您將單元配置,使用[UIView的viewWithTag:],以獲取標籤的參考

UILabel *label = (UILabel *)[cell.contentView viewWithTag:999]; 

更新:

您應該使用的標籤的恆定值。如果您使用indexPath作爲標記,則可能找不到視圖。由於您正在重複使用單元格,系統僅創建一定數量的單元格。當單元格不在視圖中時(不可見),dequeueReusableCellWithIdentifier將爲您獲取單元格,而不是創建新單元格。

reusableCell是您之前創建的視圖。 您對標記使用動態值的情況將如下所示: 您位於單元格10(indexPath),系統將找到在3(indexPath)處創建的reusableCell。由於它是在3創建的,因此標籤標籤爲3.但是您可以找到標籤爲10的視圖,因此結果爲空。

+0

看看UPD請 – 2012-02-20 10:04:41

+1

更新,對不起我的英文不好 – Hanon 2012-02-20 10:19:18

+0

[爲textLabel setTag:1];和UILabel * textLabel =(UILabel *)[cell viewWithTag:1];但仍null =( – 2012-02-20 10:50:16

1

這是因爲您沒有正確重用表格視圖單元格。 您必須在下面的代碼塊創建標籤
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}

+0

請看upd – 2012-02-20 10:04:04