ios - UITableViewCell 不响应 setNeedsLayout

标签 ios objective-c uitableview uiview

我有一个分组格式的 UITableView,其中有几个单元格。单元格中有一个 UIView,我要向其添加另一个 UIView。当我更新第一个 UIView 的框架时,它不会改变它的位置和大小。

如果单元格离开屏幕并再次出现在屏幕上,UIViews 会正确地重新对齐自己,因此正在应用实际的转换,只是重新渲染没有正确发生。

这方面的代码就在我的cellForRowAtIndexPath中:

[cell.cellMoney setBackgroundColor:[UIColor clearColor]];

[cell.cellMoney.subviews makeObjectsPerformSelector:@selector(removeFromSuperview)];

UIView *sellMoneySubView = [UIMoneyDisplay createViewWithAmount:item.min_sale_unit_price fontSize:17.0];
[cell.cellMoney setFrame:CGRectMake(cell.frame.size.width-sellMoneySubView.frame.size.width-20, 7, sellMoneySubView.frame.size.width, sellMoneySubView.frame.size.height)];

[cell.cellMoney addSubview:sellMoneySubView];

[cell setNeedsLayout];

我添加到第一个 UIView 的子 UIView 已正确添加和呈现,只是第一个的位置变得奇怪。

哦,我也在 Storyboard中使用 AutoLayout。

补充:我修复了部分问题,但标题仍然有效。我还有一个包含需要通过网络下载的图像的单元格,但该单元格也不响应任何形式的布局调用。

我通过删除中间 View 并直接添加到单元格的 contentView 解决了 UIView 的位置问题。

最佳答案

而不是这个 [单元格 setNeedsLayout] 使用 [cell layoutIfNeeded]

[cell.cellMoney setBackgroundColor:[UIColor clearColor]];

[cell.cellMoney.subviews makeObjectsPerformSelector:@selector(removeFromSuperview)];

UIView *sellMoneySubView = [UIMoneyDisplay createViewWithAmount:item.min_sale_unit_price fontSize:17.0];
[cell.cellMoney setFrame:CGRectMake(cell.frame.size.width-sellMoneySubView.frame.size.width-20, 7, sellMoneySubView.frame.size.width, sellMoneySubView.frame.size.height)];

[cell.cellMoney addSubview:sellMoneySubView];

[cell layoutIfNeeded]; //use this
return cell;

这可能对你有帮助:)

关于ios - UITableViewCell 不响应 setNeedsLayout,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29557721/

相关文章:

ios - KVO 和重构

iphone - 如何有效地迭代 NSSet (Objective-C) - 核心数据中的对多关系表示?

iphone - 如何替换整个操作系统的默认字体

ios - 从自定义 UITableView 获取 UItextFields 文本

ios - 更改 UITableView 自定义标题 View 的按钮图像不起作用

ios - 如何将对象添加到数组?

ios - 为什么我的应用程序因应用程序内购买而被拒绝?

ios - 如何使用适用于 iOS 的 Facebook SDK 3.1 向用户发送用户请求

ios - 本地址栏中只有前缀时,使 iphone webview 转到不同的网址

ios - 如何将文件从主包复制到文档文件夹