[英]UITableviewCell subview appears only on scrolling or cell reuse (iOS)
尝试使用dequeueReusableCellWithIdentifier:forIndexPath:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
CustomTableViewCell *cell1 = (CustomTableViewCell *)[tableView dequeueReusableCellWithIdentifier:@"CustomID" forIndexPath:indexPath];
最可能的问题是,当您首次通过调用以下内容访问单元格的帧高度时
cell.categoryRoundBackground.frame.size.height / 2
该单元格从未放置在表格视图中,也不知道其框架高度将是什么,因此它使用一些默认值。 实际高度取决于您的代码在heightForRowAtIndexPath:
方法中返回的值。
您可以通过自己计算帧大小来解决此问题。 您应该能够执行此操作,因为您的代码将值提供给heightForRowAtIndexPath:
在视图布局其子视图之前,可能需要更改拐角半径。 你应该尽量把线
cell.categoryRoundBackground.layer.cornerRadius=cell.categoryRoundBackground.frame.size.height/2;
内
- (void)viewDidLayoutSubviews {}
您必须重写CustomTableViewCell中的方法
- (void)layoutSubviews{
[super layoutSubviews];
self.categoryRoundBackground.layer.cornerRadius=self.categoryRoundBackground.frame.size.height/2;
self.categoryRoundBackground.layer.masksToBounds = YES;
}
在CellForRowAtIndexPath中:您必须在最后写这些行
// Update layout
[cell.contentView setNeedsLayout];
[cell.contentView layoutIfNeeded];
希望它能解决您的问题
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.