表格单元格在取消隐藏后并不总是显示隐藏内容

时间:2013-06-20 10:05:17

标签: ios tableview expand heightforrowatindexpath

我有一个带有自定义单元格的表格,单元格中有两个UIViews,顶部的一个始终显示,底部的一个是隐藏的。

当调用 cellCellForRowAtIndexPath 时,单元格的高度都会降低,只显示顶部的UIView。

当用户单击该单元格时,该单元格将展开,然后我取消隐藏第二个UIView。问题是有时候第一次点击单元格时不会出现第二个UIView。

再次单击该单元格使其全部消失,然后再次单击,它始终显示完美。向下滚动,然后选择另一个并且第一次点击它可能不会出现或将出现在错误的位置,两个更多的舔,它是完美的。

我认为这是一个ReusableCell问题,但我不知道如何绕过它。

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

cellID = @"Cell";

customCell *cell = [tableView dequeueReusableCellWithIdentifier:cellID forIndexPath:indexPath];

// Test to see if the Cell has already been built
if (cell == nil)
{
    cell = [[customCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellID];
}

// Check if the selected Cell is being drawn and unhide the pull down view and adjust y origin
if(indexPath.row == selectedCell)
{
     // Unhide the view
     cell.pullDownView.hidden = NO;

    CGRect tempFrame = cell.pullDownView.frame;
    tempFrame.origin.y = [[secondViewY objectAtIndex:indexPath.row] floatValue];
    cell.pullDownView.frame = tempFrame;

     // Image in second view    
     cell.mainImage2.image = [theImages objectAtIndex:indexPath.row]; 
}
else
{
    cell.pullDownView.hidden = YES;
}

// Image in first view
cell.mainImage.image = [theImages objectAtIndex:indexPath.row];




return cell;
}

因此,单击一个单元格会调用 didSelectRow ,如下所示

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{

if(selectedCell == indexPath.row)
{
    selectedCell = -1;
    [tableView deselectRowAtIndexPath:indexPath animated:NO];
}
else
{
    selectedCell = indexPath.row;
}

//    [tableView reloadData];

[tableView beginUpdates];
[tableView reloadRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationMiddle];
[tableView endUpdates];
}

我已经尝试了reloadData和BeginUpdates,但是这仍然无效。这是因为我隐藏/取消隐藏我应该选择Alpha为0然后选择1吗?这是一个可重复使用的细胞吗?

哦,为了完整。您可能会注意到我有一张桌子,其中所有高度都不同,这就是我改变第二个UIView的Y的原因。

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{

float returnHeightValue;

if(indexPath.row == selectedCell)
{
    returnHeightValue = [[selectedCellHeights objectAtIndex:indexPath.row] floatValue];
}
else
{
    returnHeightValue = [[normalCellHeights objectAtIndex:indexPath.row] floatValue];
}


return returnHeightValue;

}

2 个答案:

答案 0 :(得分:0)

好的,我已经改变了我解决这些问题的方法。我删除了UIView(它们都嵌入了它们),而我现在已经在我的自定义单元格中。

@property (nonatomic, strong) IBOutletCollection(UILabel) NSArray *pullDownItems;
@property (weak, nonatomic) IBOutlet UIImageView *mainImage;

而不是

@property (weak, nonatomic) IBOutlet UIView *pullDownView;

好的,所以现在我必须取消隐藏并移动两个项目而不是一个但是这比单独使用它们会更加痛苦(大约有10个UILabels)。我像这样列举了数组;

    for(UILabel *obj in cell.pullDownItems)
    {
        obj.hidden = NO;
        tempFrame = obj.frame;
        tempFrame.origin.y = tempFrame.origin.y - tempYOffset;
        obj.frame = tempFrame;
    }

我刚刚附上的任何新商品。

答案 1 :(得分:0)

好吧改变了一切,因为隐藏和取消隐藏在更改的单元格上的内容不能很好地工作(在表格中留下了工件,有时候事情不会出现)。

我现在正在创建两个自定义单元格,当选中单元格时,其中包含所有其他对象。

到目前为止似乎是更好的方法。