2014-09-03 108 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每次细胞被重用。