繁体   English   中英

UITableviewCell子视图仅在滚动或单元重用(iOS)上出现

[英]UITableviewCell subview appears only on scrolling or cell reuse (iOS)

如下图所示, UITableView子视图仅在重新加载表视图或单元格重用时出现(主要是在滚动过程中)。 蓝色圆圈是我要在UITableViewCell颜色。 第一次出现时,它将是一个小点,如您在图片中看到的那样,并且在滚动或刷新表格视图时,它显示为一个完整的圆圈。

可能是什么问题?

屏幕图像

我在cellforRowAtIndexPath方法中使用以下代码

cell.categoryRoundBackground.layer.cornerRadius=cell.categoryRoundBackground.frame.size.height/2;

尝试使用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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM