首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >UITableView contentSize

UITableView contentSize
EN

Stack Overflow用户
提问于 2012-11-02 00:39:54
回答 4查看 24.4K关注 0票数 17

我的应用程序中有几个表视图有问题。

当表的内容发生变化(因此表视图的内容变得更大)时,就会出现问题。

由于某些原因,UITableView的contentSize保持不变,这意味着我无法滚动查看新内容。

当一个特定的单元格变得更大时(在tableView reloadData;之后),或者当我添加新的单元格时(使用NSFetchedResultsController委托方法),都会发生这种情况。

是否需要为tableView重新调整其contentSize做些什么?

编辑以显示代码,但这只是样板NSFetchedResultsController委托代码...

代码语言:javascript
复制
- (void)controllerWillChangeContent:(NSFetchedResultsController *)controller {
    // The fetch controller is about to start sending change notifications, so prepare the table view for updates.
    [self.tableView beginUpdates];
}


- (void)controller:(NSFetchedResultsController *)controller didChangeObject:(id)anObject atIndexPath:(NSIndexPath *)indexPath forChangeType:(NSFetchedResultsChangeType)type newIndexPath:(NSIndexPath *)newIndexPath {

    UITableView *tableView = self.tableView;

    switch(type) {

        case NSFetchedResultsChangeInsert:
            [tableView insertRowsAtIndexPaths:[NSArray arrayWithObject:newIndexPath] withRowAnimation:UITableViewRowAnimationFade];
            break;

        case NSFetchedResultsChangeDelete:
            [tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
            break;

        case NSFetchedResultsChangeUpdate:
            [self configureCell:(OJFPunchListCell*)[tableView cellForRowAtIndexPath:indexPath] atIndexPath:indexPath];
            break;

        case NSFetchedResultsChangeMove:
            [tableView deleteRowsAtIndexPaths:[NSArray
                                           arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
            [tableView insertRowsAtIndexPaths:[NSArray
                                           arrayWithObject:newIndexPath] withRowAnimation:UITableViewRowAnimationFade];
            break;
    }
}


- (void)controller:(NSFetchedResultsController *)controller didChangeSection:(id )sectionInfo atIndex:(NSUInteger)sectionIndex forChangeType:(NSFetchedResultsChangeType)type {

    switch(type) {

        case NSFetchedResultsChangeInsert:
            [self.tableView insertSections:[NSIndexSet indexSetWithIndex:sectionIndex] withRowAnimation:UITableViewRowAnimationFade];
            break;

        case NSFetchedResultsChangeDelete:
            [self.tableView deleteSections:[NSIndexSet indexSetWithIndex:sectionIndex] withRowAnimation:UITableViewRowAnimationFade];
            break;
    }
}


- (void)controllerDidChangeContent:(NSFetchedResultsController *)controller {
    // The fetch controller has sent all current change notifications, so tell the table view to process all updates.
    [self.tableView endUpdates];
}

我以前在iOS5中做过这样的事情,从来没有遇到过问题。这似乎是一个潜在的iOS6变化?

::编辑::

关于这个愚蠢的事情是,我在同一个应用程序中有许多其他UITableViewControllers,它们都工作得很好。我可以添加新的单元格,添加动态大小的单元格,调整单元格的大小,等等。所有的表格都能正确地上下滚动所有的内容。

这个问题现在发生在我最近添加的几个表视图上。

::编辑::

好的,只是在做一些调试。当我第一次进入表视图时,contentSize.height是594。

然后,当我添加一个新项目并返回相同的tableviewController时,contentSize.height是749。

但是,我无法滚动查看tableView底部新添加的单元格。它就是不能滚动到那里。它以旧的contentSize高度反弹。

EN

回答 4

Stack Overflow用户

发布于 2013-07-08 08:03:56

我也遇到了同样的问题,最终解决了这个问题。我要在[self.tableView endUpdates];之后设置contentSize

代码语言:javascript
复制
[self.tableView endUpdates];

NSLog(@"%f", self.tableView.contentSize.height);

self.tableView.contentSize = [self.tableView sizeThatFits:CGSizeMake(CGRectGetWidth(self.tableView.bounds), CGFLOAT_MAX)];

NSLog(@"%f", self.tableView.contentSize.height);

[self.tableView setContentInset:UIEdgeInsetsMake(50, 0, -50, 0)];
票数 20
EN

Stack Overflow用户

发布于 2013-03-06 04:59:29

我使用带有两个子VC的自定义视图控制器时也遇到了同样的问题,其中一个是tableview + fetched results控制器。

在从一个vc添加内容并返回到我的表格后,我会发现无法滚动到底部的新内容。

因此,在我的tableview + fetched results控制器中,我根据获取的对象的数量调整内容大小,而不管它们来自哪里。

最坏的情况是,我在显示VC时没有添加任何内容,但成本很低。

代码语言:javascript
复制
//In my tableview + fetched results controller class
-(void)viewDidAppear:(BOOL)animated{
    [super viewDidAppear:animated];


    NSUInteger objCount = self.fetchedResultsController.fetchedObjects.count;
    //My custom cell has a height of 50.0f, yours may not!
    CGFloat tableHeight = objCount * 50.0f;

    NSLog(@"%i objects, total height: %f",objCount,tableHeight);

    [self.tableView setContentSize:CGSizeMake(self.tableView.contentSize.width, tableHeight)];

}
票数 1
EN

Stack Overflow用户

发布于 2016-12-21 23:22:13

我有“UITableViewAutomaticDimension”的动态tableView。发生了一些事情,在重新加载之后,它的内容大小等于它的帧大小。在我手动将内容大小设置为表边界大小后,一切正常。我希望这能帮到你。

代码语言:javascript
复制
table.beginUpdates()
table.contentSize = CGSize(width: table.bounds.width, height: table.bounds.height)
table.endUpdates()

这是在我做了table.reloadData()之后

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/13181607

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档