我已经创建了一个自定义UITableViewCell
,但是当我在iPad上运行我的应用时,单元格的内容与iPhone上的宽度相同。我希望内容 - 例如背景UIView
- 是完整的宽度。
customCell.h
#import <UIKit/UIKit.h>
@interface customCell : UITableViewCell
@property (nonatomic, strong) UILabel *title;
@property (nonatomic, strong) UILabel *description;
@property (nonatomic, strong) UIView *background;
@end
customCell.m
#import "customCell.h"
@implementation customCell
@synthesize title = _title;
@synthesize description = _description;
@synthesize background = _background;
- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
if (self) {
// configure background
self.background = [[UIView alloc]initWithFrame:CGRectMake(0, 0, self.contentView.frame.size.width, 115.0f)];
// configure title
self.title = [[UILabel alloc] initWithFrame:CGRectMake(5, 10, self.contentView.frame.size.width-10, 70)];
self.title.textAlignment = NSTextAlignmentNatural;
self.title.lineBreakMode = NSLineBreakByWordWrapping;
self.title.numberOfLines = 4;
self.title.preferredMaxLayoutWidth = 20;
self.title.adjustsFontSizeToFitWidth = NO;
self.title.textColor = [UIColor whiteColor];
self.title.font = [UIFont fontWithName:@"SourceSansPro-Regular" size:16];
[self.background addSubview:self.title];
// configure description
self.description = [[UILabel alloc] initWithFrame:CGRectMake(5, 80, self.contentView.frame.size.width-10, 20)];
self.description.textColor = [UIColor whiteColor];
self.description.textAlignment = NSTextAlignmentLeft;
self.description.font = [UIFont fontWithName:@"SourceSansPro-Bold" size:13];
[self.background addSubview:self.description];
[self addSubview:self.background];
[self sendSubviewToBack:self.background];
}
return self;
}
@end
我在这里做错了什么?
答案 0 :(得分:4)
尚未在initWithStyle:reuseIdentifier:
方法中设置单元格的大小。使用正确的autoresizingMask
值设置子视图,或者使用单元格的layoutSubviews
方法更新其大小。
此外,由于您将title
和description
标签添加到background
视图,因此标签的尺寸应基于background
视图的尺寸,而不是单元格的大小contentView
。
最后,永远不要命名属性description
。它会与从description
继承的NSObject
方法冲突。