繁体   English   中英

将子视图添加到静态UITableViewCell无法正常工作

[英]Adding subview to static UITableViewCell not working

我有一个static UITableView 我正在尝试以编程方式将UISegmentedControl添加到第三个单元格。 这是代码:

UISegmentedControl *segment = [[UISegmentedControl alloc] initWithItems:@[@"First", @"Second"]];
segment.frame = CGRectMake(0, 0, 50, 30);

UITableViewCell *cell = [self.tableView cellForRowAtIndexPath:[NSIndexPath indexPathForRow:2 inSection:1]];
[cell.contentView addSubview:segment];

调用代码时,不会添加SegmentedControl

然后,我尝试添加UILabel ,但这也不起作用。

我无法将其放置在该方法中,因为在选择按钮时添加了SegmentedControl

当拥有静态tableView ,可以将出口直接连接到其单元格,并像简单的子视图一样进行操作。 因此,只需连接插座并使用您的代码添加子视图即可。 会工作的

UPDATE

使用以下代码添加子视图:

[cell addSubview:segment];

将您的代码移入

- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath{

}

然后只需引用该方法传递的cell

暂无
暂无

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

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