您可以继承 UITableViewCell 并在 initWithStyle 方法中进行布局。例如:
MyCustomCell.h 文件:
@interface MyCustomCell : UITableViewCell
{
UILabel *_label1;
UILabel *_label2;
UILabel *_titleLabel;
}
@property (nonatomic, strong) UILabel *label1;
@property (nonatomic, strong) UILabel *label2;
@property (nonatomic, strong) UILabel *titleLabel;
@end
MyCustomCell.m 文件:
#import "MyCustomCell.h"
@implementation MyCustomCell
@synthesize label1 = _label1;
@synthesize label2 = _label2;
@synthesize titleLabel = _titleLabel;
- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
if (self) {
// Initialization code
self.label1 = [[UILabel alloc]initWithFrame:CGRectMake(x, y, w, h)]; //x: x coordinate, y: y coordinate, w is width , h is height
self.label2 = [[UILabel alloc]initWithFrame:CGRectMake(x, y, w, h)];
self.titleLabel = [[UILabel alloc]initWithFrame:CGRectMake(x, y, w, h)];
self.titleLabel.font = [UIFont fontWithName:@"Times New Roman" size:30.0f]; //change font name and size per your needed.
[self addSubview:self.label1];
[self addSubview:self.label1];
[self addSubview:self.titleLabel];
}
return self;
}
在您拥有cellForRowAtIndexPath 方法的类文件中,确保包含#import "MyCustomCell.h" 并使用MyCustomCell 而不是UITableViewCell 来分配单元格。并将数据分配给标签:
cell.label1.text = xxxx;
cell.label2.text = xxxx;
cell.titleLabel.text = xxxx;