2014-09-03 105 views
-1

我在tableView中有兩種不同類型的單元格佈局,一種是正常高度,另一種是擴展高度。它們都是在XIB中製作的。問題是擴展單元覆蓋了數據。例如,當我繼續展開不同的單元格時,標籤會設置一些文字,這些文字將繼續添加在先前的文字之上)。表格單元格覆蓋數據

下面是如何加載單元格。

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

    static NSString *cellIdentifier = @"Cell"; 
    static NSString *expandedCellIdentifier = @"ExpandedCell"; 
    if (!isExpanded) 
    { 
     ListCell *cell =(ListCell*) [tableView dequeueReusableCellWithIdentifier:cellIdentifier]; 
     if (cell==nil) 
     { 
      NSArray *nibs = [[NSBundle mainBundle]loadNibNamed:@"ListCell" owner:self options:nil]; 
      cell = nibs[0]; 
     } 
     cell.Name.text = [[bArray objectAtIndex:indexPath.row]valueForKey:@"Name"]; 
     return cell; 
    } 
    else 
    { 
     expCell =(ExpandedCell*) [tableView dequeueReusableCellWithIdentifier:expandedCellIdentifier]; 
     if (expCell==nil) 
     { 
      NSArray *nibs = [[NSBundle mainBundle]loadNibNamed:@"ExpandedCell" owner:self options:nil]; 
      expCell = nibs[0]; 
     } 
     UILabel *jTime = [[UILabel alloc]initWithFrame:CGRectMake(27, 6, 72, 10)]; 
     jTime.text = [NSString stringWithFormat:@"%@",timeRequired]; 
     [expCell.background_View addSubview:jTime]; 

     return expCell; 
    } 
    return nil; 
} 

回答

2

每當單元格被重用時,您的標籤都會被添加。在你的if條件else部分使用此代碼來避免:

expCell = (ExpandedCell*) [tableView dequeueReusableCellWithIdentifier:expandedCellIdentifier]; 
if (expCell == nil) 
{ 
    NSArray *nibs = [[NSBundle mainBundle]loadNibNamed:@"ExpandedCell" owner:self options:nil]; 
    expCell = nibs[0]; 
    UILabel *jTime = [[UILabel alloc]initWithFrame:CGRectMake(27, 6, 72, 10)]; 
    [jTime setTag:100]; //or any value 
    [expCell.background_View addSubview:jTime]; 
} 
UILabel *jTimeLabel = (UILabel *)[expCell.background_View viewWithTag:100]; 
jTimeLabel.text = [NSString stringWithFormat:@"%@",timeRequired]; 

return expCell; 

通過這個代碼,您的標籤將被添加一次。

+0

非常感謝; - ] – iSD 2014-09-03 13:28:14

1

您要添加新的子視圖的每一個它的出隊時你expcell

+0

什麼是解決方法然後: - ] – iSD 2014-09-03 12:59:18

+0

如果你從nib加載單元格,爲什麼不把你的子視圖放在那裏,首先? – Kreiri 2014-09-03 13:00:53

+0

還有一些繪圖的東西,我需要編程方式。這就是爲什麼用這種方式。 – iSD 2014-09-03 13:07:01

0

因爲這些行:

UILabel *jTime = [[UILabel alloc]initWithFrame:CGRectMake(27, 6, 72, 10)]; 
jTime.text = [NSString stringWithFormat:@"%@",timeRequired]; 
[expCell.background_View addSubview:jTime]; 

這將增加新的UILabel每次細胞被重用。