[英]Adding a SubView to UITableViewCell doesn't get displayed
我已經通過drawRect
創建了自己的圖表
@interface FTChartsView : UIView
@implementation FTChartsView
-(void)drawRect:(CGRect)rect
{
...
}
我也將UITableViewCell子類化
@interface FTSummaryCellView : UITableViewCell
...
現在在ViewController中,生成單元時:
- (void)viewDidLoad
{
[super viewDidLoad];
UINib *nib = [UINib nibWithNibName:@"FTSummaryCellView" bundle:nil];
[[self tableView] registerNib:nib forCellReuseIdentifier:@"FTSummaryCellView"];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
FTSummaryCellView *cell = [tableView dequeueReusableCellWithIdentifier:@"FTSummaryCellView"];
FTChartsView *chart = [[FTChartsView alloc] init];
[cell addSubview:chart];
return cell;
}
單元格將chatrView添加為子視圖。 但是ChartsView的drawRect
永遠不會中斷,因此也不會顯示。
我在這里想念什么?
您忘記了setFrame
。
- (UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
MyCell *cell = [tableView dequeueReusableCellWithIdentifier:@"MyCell"];
TView *chart = [[TView alloc] initWithFrame:CGRectMake(10, 10, 30, 30)];
[cell addSubview:chart];
return cell;
}
非常適合我。
Ty用於[[self tableView] registerNib:nib forCellReuseIdentifier:@"FTSummaryCellView"];
。 我不知道這種方法)
如我所知,您必須將子視圖添加到內容視圖。 但是,所有這些對我都有效。
[cell.contentView addSubview:chart];
在黑暗中這是一個刺...在將FTChartView添加到單元格之后...可以嘗試執行以下操作:
[chart setNeedsDisplay];
如果您有一個自定義的drawRect方法,則有時可行。
甚至會
[chart.view setNeedsDisplay];
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.