[英]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.