【问题标题】:UITableView setRowHeight how to commit changesUITableView setRowHeight 如何提交更改
【发布时间】:2012-10-15 19:00:23
【问题描述】:

我有一个设计,其中UITableViewCells 包含一个UIImageView。此图像在纵向或横向中应具有相同的比例,这就是为什么我使用UIDeviceOrientationDidChangeNotification 通知在界面方向更改时调用的方法。

-(void)updateRowHeightForOrientation:(UIInterfaceOrientation)orientation {

    if(!IS_KNOWN_ORIENTATION(orientation))
        return;

    CGFloat rowHeightPortrait = (IPAD ? 320. : 160.);
    CGFloat widthPortrait = (IPAD ? 768. : 320.);
    CGFloat orientationWidth = (IS_PORTRAIT(orientation) ?
                                (IPAD ? 768. : 320.) :
                                (IPAD ? 1024. : 480.));

    CGFloat resultingRowHeight = rowHeightPortrait * orientationWidth / widthPortrait;

    [self.tableView setRowHeight:resultingRowHeight];
//    [self.tableView reloadData]; /* works if uncommented */
}

如果没有注释该行,则重绘视图并正确设置行高。如果不是,即使使用[self.tableView setNeedsDisplay][self.tableView setNeedsLayout],也不会使用该属性的新值。

如何强制重新加载 UI 以便应用新值?

使用-(void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration 工作了一段时间,因为rowHeight 属性是在界面旋转并重绘之前设置的,但由于一些奇怪的原因,不再调用此方法...

感谢您的帮助!

【问题讨论】:

    标签: objective-c uitableview setneedsdisplay row-height


    【解决方案1】:

    我认为

    [self.tableView beginUpdates];
    [self.tableView endUpdates];
    

    使表格视图重新评估所有行高,所以你可以试试这个。

    【讨论】:

    • 这简直太完美了!不幸的是,我还有一个UILabel 动态调整大小以使用单元格的整个宽度,文本居中。当我旋转屏幕时,标签的右侧似乎移动了,然后文本居中,看起来并不漂亮。如果他们有任何改善行为的方法,我很想知道如何,如果不是,这不是很重要
    猜你喜欢
    • 2020-09-06
    • 2010-09-07
    • 1970-01-01
    • 2013-08-28
    • 2012-09-30
    • 2011-02-13
    • 2020-09-20
    • 1970-01-01
    相关资源
    最近更新 更多