繁体   English   中英

动态地将边框子视图添加到UITableViewCell

[英]Adding border subview to UITableViewCell dynamically

我试图使用我在运行时以编程方式添加到单元格的UIImageView向所有表视图单元格添加自定义底部边框。 问题在于,随着用户的滚动,边框经常会随机消失。

这段代码在我的cellForRowAtIndexPath方法中:

UIImageView* border = (UIImageView*)[cell viewWithTag:10];
if (border)
    [border removeFromSuperview];

NSInteger sectionRows = [tableView numberOfRowsInSection:[indexPath section]];
NSInteger row = indexPath.row;

if (row != sectionRows - 1) {
    UIImageView* border = [[UIImageView alloc] initWithImage:[[ImageManager sharedManager] imageNamed:@"dashed_border"]];
    [border setFrame:CGRectMake(10, [self tableView:tableView heightForRowAtIndexPath:indexPath], 290, 1)];
    [border setTag:10];
    [cell addSubview:border];
    [border release];
}

如果存在边框子视图,它将删除边框子视图;如果该行不是底行,则将边框插入到该行的底部。

我不确定这是否是竞争条件,或者在用户滚动时某些时候是否没有调用cellForRowAtIndexPath。

下方的单元格可能绘制在边框的顶部。 它取决于表视图中子视图的顺序,并且滚动时顺序会更改。

发生这种情况是因为您要在单元格边界之外添加边框。 尝试将其向上移动1px。 喜欢:

[border setFrame:CGRectMake(10, [self tableView:tableView heightForRowAtIndexPath:indexPath] - 1, 290, 1)];

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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