表格查看单元格覆盖数据

时间:2014-09-03 12:50:07

标签: ios objective-c uitableview

我的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;
}

3 个答案:

答案 0 :(得分:2)

每次重复使用单元格时,都会添加标签。在else条件的if部分中使用此代码可以避免这种情况:

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;

通过此代码,您的标签只会添加一次。

答案 1 :(得分:1)

您每次出列时都会在expcell添加新的子视图。

答案 2 :(得分:0)

由于这些原因:

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

每次重复使用单元格时,都会添加新的UILabel。