简体   繁体   English

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

[英]Adding subview to static UITableViewCell not working

I have a static UITableView . 我有一个static UITableView I'm trying to programmatically add a UISegmentedControl to the third cell. 我正在尝试以编程方式将UISegmentedControl添加到第三个单元格。 Here is the code: 这是代码:

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];

When the code is called, the SegmentedControl isn't added. 调用代码时,不会添加SegmentedControl

I then tried adding a UILabel , and that doesn't work either. 然后,我尝试添加UILabel ,但这也不起作用。

I can't place it in that method because I'm adding the SegmentedControl when a button is selected 我无法将其放置在该方法中,因为在选择按钮时添加了SegmentedControl

When you have static tableView , you can connect outlets to its cells directly and manipulate like simple subviews. 当拥有静态tableView ,可以将出口直接连接到其单元格,并像简单的子视图一样进行操作。 So just connect outlet and use your code to add subviews. 因此,只需连接插座并使用您的代码添加子视图即可。 It will work 会工作的

UPDATE UPDATE

Add subviews using the following code: 使用以下代码添加子视图:

[cell addSubview:segment];

Move your code into 将您的代码移入

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

}

then just reference the cell being passed by the method 然后只需引用该方法传递的cell

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

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