自定義單元格的標籤中的文本將顯示一個塊。有沒有更好的方法來完成這一點?iOS7:UILabel自定義單元格中的文本不會在沒有GCD的情況下出現
CustomCell.h
@interface CustomCell : UITableViewCell
@property (nonatomic, strong) UILabel *label;
@property (nonatomic, strong) UIView *circle;
@end
CustomCell.m
@implementation CustomCell
- (void)layoutSubviews
{
[super layoutSubviews];
self.circle = [[UIView alloc] initWithFrame:CGRectMake(10, 10, 40.0f, 40.0f)];
[self.circle setBackgroundColor:[UIColor brownColor];
self.label = [[UILabel alloc] initWithFrame:CGRectMake(15, 20, 200.0f, 50.0f)];
self.label.textColor = [UIColor blackColor];
[self.contentView addSubview:self.label];
[self.contentView addSubview:self.circle];
//I have also tried [self addSubview:self.label];
}
tableView.m
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *customCellIdentifier = @"CustomCell";
CustomCell *cell = (CustomCell *)[tableView dequeueReusableCellWithIdentifier:customCellIdentifier];
if (cell == nil) {
cell = [[CustomCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:inviteCellIdentifier];
}
dispatch_async(dispatch_get_main_queue(), ^{
[[cell label] setText:@"This is Label"];
[cell setNeedsDisplay];
});
return cell;
}
我可以得到的UILabel來顯示文本的唯一方法是,使用嵌段以上。如果我不使用該塊,只需使用cell.Label.text = @"This is a Label"
後跟[cell setNeedsDisplay];
,文本就不會出現,我必須滾動tableview才能重新加載單元格,並且只有標籤中的文本纔會出現。
有沒有更好的方法,還是我堅持不得不使用該塊?
我正在嘗試此操作。謝謝。 – user1107173
我是否在layoutsubviews中初始化並設置了框架?或者在iniwithstyle中初始化並在layoutsubviews中設置框架? – user1107173
在你調用'self.label = [[UILabel alloc] initWithFrame:...];''和'[self.contentView addSubview:self.label]'的'init ...'方法中,以及設置其他標籤屬性。 'layoutSubviews'只需要根據需要調整框架。 – rmaddy