I have a static UITableView
. I'm trying to programmatically add a UISegmentedControl
to the third cell. 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.
I then tried adding a UILabel
, and that doesn't work either.
I can't place it in that method because I'm adding the SegmentedControl when a button is selected
When you have static tableView
, you can connect outlets to its cells directly and manipulate like simple subviews. So just connect outlet and use your code to add subviews. It will work
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
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.