// tableViewCell 自定义
.h声明方法
- (void)getString:(NSString *)string;
.m
- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
if (self) {
// Initialization code
// 初始化cell
}
return self;
}
- (void)getString:(NSString *)string
{
// label适应属性 以下是例子(一个背景图片,一个自适应label, 最后还有一个label)
[_backImage setFrame:CGRectMake(0, 0, [[UIScreen mainScreen] bounds].size.width - 20, 70)];
if ([string isEqualToString:@""] || string == nil) {
string = @"";
_coalKindLabel.frame = CGRectMake(20, 10, _backImage.frame.size.width - 70, 30);
} else {
_coalKindLabel.text = [NSString stringWithString:string];
_coalKindLabel.frame = CGRectMake(20, 10, _backImage.frame.size.width - 70, 30);
_coalKindLabel.font = [UIFont fontWithName:@"Helvetica-Bold" size:19];
[_coalKindLabel setNumberOfLines:0];
[_coalKindLabel sizeToFit];
}
[_priceLabel setFrame:CGRectMake(20, _coalKindLabel.frame.origin.y + _coalKindLabel.frame.size.height, 65, 30)];
[_backImage setFrame:CGRectMake(0, 0, [[UIScreen mainScreen] bounds].size.width - 20, _priceLabel.frame.origin.y + _priceLabel.frame.size.height + 10)];
}
// tableView
// 高度
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
Coal_listModel *model = [[Coal_listModel alloc] init];
UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width - 40, 30)];
label.text = model.colliery;
label.numberOfLines = 0;
[label sizeToFit];
return label.frame.size.height + 40;
}
// 赋值
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
// BBCBKindTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"coalKindCell" forIndexPath:indexPath];
static NSString *str = @"coalKindCell";
BBCBKindTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:str];
if (nil == cell) {
cell = [[BBCBKindTableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:str];
}
Coal_listModel *model = [[Coal_listModel alloc] init];
model = [self.tableViewArray objectAtIndex:indexPath.row];
// 调用方法赋值
if (model.colliery == nil) {
[cell getString:@" "];
} else {
[cell getString:model.colliery];
}
return cell;
}