添加单元格时更新uipopover高度

时间:2013-08-10 04:44:54

标签: ios objective-c uipopovercontroller

我在一个有4个单元格的UIPopoverController中有一个表视图(没有滚动)。有时它需要一个额外的单元格(最多1个)。如果我动画添加和减去该单元格,我是否也可以更新弹出窗口的高度?

以下是我创建popover表视图的方法:

- (void)createPopoverTable
{

    //set up array
    _arrayList = [NSMutableArray arrayWithObjects:@"one", @"two", @"three", @"four", nil];


    //Make row selections persist.
    self.clearsSelectionOnViewWillAppear = NO;


    //View height
    NSInteger rowsCount = [_arrayList count];
    NSInteger singleRowHeight = [self.tableView.delegate tableView:self.tableView heightForRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:0]];
    NSInteger totalRowsHeight = (rowsCount * singleRowHeight) + 20;

    //View width
    CGFloat largestLabelWidth = 0;
    for (NSString *item in _arrayList) {
        //check size of font using default label
        CGSize labelSize = [item sizeWithFont:[UIFont boldSystemFontOfSize:20.0f]];
        if (labelSize.width > largestLabelWidth) {
            largestLabelWidth = labelSize.width;
        }
    }

    //some padding for the width
    CGFloat popoverWidth = largestLabelWidth + 200;

    //Tell popover the size
    self.contentSizeForViewInPopover = CGSizeMake(popoverWidth, totalRowsHeight);
}

然后在我的一个方法中,当表需要更改时,我有这个代码:

[self.tableView beginUpdates];

//update the array
[_arrayList insertObject:@"Blue" atIndex:3];

//insert the row
[self.tableView insertRowsAtIndexPaths:[NSArray arrayWithObject:[NSIndexPath indexPathForRow:3 inSection:0]] withRowAnimation:UITableViewRowAnimationTop];

[self.tableView endUpdates];

代码全部"工作"很好但是当我添加额外的单元格时,由于我的弹出控制器的非滚动特性,它被切断了。我可以用任何方式更新吗?

1 个答案:

答案 0 :(得分:2)

试试这个为我工作的那个

-(void) viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    self.contentSizeForViewInPopover = self.tableView.contentSize;
}

-(void) viewDidAppear:(BOOL)animated
{
    [super viewDidAppear:animated];
    [self.popoverControllerContainer setPopoverContentSize:self.contentSizeForViewInPopover animated:YES];
}