UIView在滚动时不在UITableViewCell内调整大小

时间:2014-04-19 13:14:05

标签: ios objective-c uitableview uiview

我在每个UITableViewCell上都有一个UIView ..这个UIView有一个触摸事件,可以让它在高度上增长。

当用户点击这个UIView时,增长动画工作正常,然后我标记一个标志,表明这个单元格有更大的UIView。

但是当我需要回收那个单元并让她自动变大时,当UITableView再次显示单元时,UIView不会增长。如果我改变backgroundColor它会显示更改,但如果我改变它们的帧高,则不起作用。

示例代码:

自定义UIViewController - UITableViewDelegate,UITableViewDataSource

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if([_dataSource count] > 0)
    {
        Model *model = [_dataSource objectAtIndex:indexPath.row];
        ModelCellView *cell = [model getViewFor: tableView];
        return cell;
    }
    return nil;
}

我的数据模型

- (ModelCellView*)getViewFor:(UITableView *)tableView
{
    NSString *cellIdentifier = @"ModelCell";
    ModelCellView *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];

    if (cell == nil)
    {
        cell = [[ModelCellView alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:cellIdentifier];
    }

    //Lots of code

    // Reset actionbar (UIView) to specific size
    [cell.actionBar resetToState: _actionBarOpened];

    //Lots of code

    return cell;
}

我在自定义UITableViewCell中的自定义UIView类(ActionBar)

- (void)resetToState:(BOOL)opened;
{
    if(opened)
    {
        _closed = NO;
        //[self open]; //tried to change de frame height here, but dont work too.
    }
    else
    {
        _closed = YES;
        //[self close];  //tried to change de frame height here, but dont work too.
    }

    [self setNeedsLayout];
}

- (void)layoutSubviews
{
    if(!_closed)
    {
        // This dont work
        int sizeToGrow = 60;
        [self setFrame: CGRectMake(
                                    _defaultPosition.x, 
                                    _defaultPosition.y - sizeToGrow, 
                                    _defaultSize.width, 
                                    _defaultSize.height + sizeToGrow)];

        // This work
        self.backgroundColor = [UIColor redColor];

        // This work
        [_saveButton setFrame: CGRectMake(
                                            _saveButton.frame.origin.x, 
                                            INSIDE_VIEW_Y_POSITION, 
                                            _saveButton.frame.size.width, 
                                            _saveButton.frame.size.height)];

        // This work
        [_deleteButton setFrame: CGRectMake(
                                            _deleteButton.frame.origin.x, 
                                            INSIDE_VIEW_Y_POSITION, 
                                            _deleteButton.frame.size.width, 
                                            _deleteButton.frame.size.height)];
    }
    else
    {
        // This dont work
        [self setFrame: CGRectMake(
                                    _defaultPosition.x, 
                                    _defaultPosition.y, 
                                    _defaultSize.width, 
                                    _defaultSize.height)];

        // This work
        self.backgroundColor = [UIColor greenColor];

        // This work
        [_saveButton setFrame: CGRectMake(
                                            _saveButton.frame.origin.x, 
                                            OUTSIDE_VIEW_Y_POSITION, 
                                            _saveButton.frame.size.width, 
                                            _saveButton.frame.size.height)];

        // This work
        [_deleteButton setFrame: CGRectMake(
                                            _deleteButton.frame.origin.x, 
                                            OUTSIDE_VIEW_Y_POSITION, 
                                            _deleteButton.frame.size.width, 
                                            _deleteButton.frame.size.height)];
    }

    [super layoutSubviews];
}

0 个答案:

没有答案
相关问题