调用自定义UITableViewCell函数,但显示本机表
这是我在init函数中的代码:
self.answersTable = [[UITableView alloc]initWithFrame:CGRectMake(0, 45, 280, 500)];
self.answersTable.dataSource = self;
self.answersTable.delegate = self;
这是我在viewDidLoad函数中的代码:
[self.answersTable registerClass:[AnswerTableViewCell class]
forCellReuseIdentifier:CellIdentifier];
这是我的表委托函数,它被调用,并且单元格最后不是nil:
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return [self.question.answers count];
}
-(AnswerTableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
AnswerTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[AnswerTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
AnswerObject* answer = self.question.answers[indexPath.row];
[cell setupAnswerTableViewCell:self.question answer:answer row:indexPath.row];
return cell;
}
这是AnswerTableViewCell:
#import "AnswerTableViewCell.h"
@implementation AnswerTableViewCell
- (id)init
{
self = [super init];
if (self) {
self.frame = CGRectMake(0, 0, 280, 77);
self.answerLabel = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, self.frame.size.width-20, self.frame.size.height)];
self.answerLabel.numberOfLines = 0;
self.answerLabel.textColor = [UIColor whiteColor];
self.answerLabel.font = [UIFont fontWithName:@"HelveticaNeue-Medium" size:20];
self.answerToggle = [[UIButton alloc]initWithFrame:CGRectMake(self.frame.size.width-50, 23, 30, 30)];
self.backgroundImage = [[UIImageView alloc]initWithFrame:self.frame];
[self addSubview:self.answerLabel];
[self addSubview:self.answerToggle];
[self addSubview:self.backgroundImage]; }
return self;
}
- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
if (self) {
self.frame = CGRectMake(0, 0, 280, 77);
self.answerLabel = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, self.frame.size.width-20, self.frame.size.height)];
self.answerLabel.numberOfLines = 0;
self.answerLabel.textColor = [UIColor whiteColor];
self.answerLabel.font = [UIFont fontWithName:@"HelveticaNeue-Medium" size:20];
self.answerToggle = [[UIButton alloc]initWithFrame:CGRectMake(self.frame.size.width-50, 23, 30, 30)];
self.backgroundImage = [[UIImageView alloc]initWithFrame:self.frame];
[self addSubview:self.answerLabel];
[self addSubview:self.answerToggle];
[self addSubview:self.backgroundImage]; }
return self;
}
-(void)setupAnswerTableViewCell:(QuestionObject*)question
answer:(AnswerObject*)answer
row:(NSInteger)row{
self.question = question;
self.answer = answer;
self.row = row;
self.answerLabel.text = answer.answerText;
[self.answerToggle addTarget:self
action:@selector(flip:)
forControlEvents:UIControlEventTouchDown];
self.answerToggle.tag = [answer.answerID intValue];
if (self.row == 0) {
self.backgroundImage.image = [UIImage imageNamed:@"List_Top_Item_Not_Selected_612x113px.png"];
}
else if (self.row == ([self.question.answers count] - 1)){
self.backgroundImage.image = [UIImage imageNamed:@"List_Bottom_Item_Not_Selected_612x113px.png"];
}
else{
self.backgroundImage.image = [UIImage imageNamed:@"List_Item_Not_Selected_612x113px.png"];
}
}
那么,怎么了?
答案 0 :(得分:0)
问题是:
cell = [[AnswerTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
具体来说:UITableViewCellStyleDefault
您要求它使用默认样式创建一个Cell。要么只使用[[AnswerTableViewCell alloc] init];
并设置重用标识符。或者创建自己的init,它接受一个没有样式参数的重用标识符
修改强>
添加Cell代码后,我认为问题是您正在使用init函数添加UI元素。这些很可能被单元格UIView
contentView
覆盖。它是contentView
您需要自定义以添加这些元素。
本文将指导您如何执行此操作并使用drawRect
方法自定义UIView
。
http://blog.giorgiocalderolla.com/2011/04/16/customizing-uitableviewcells-a-better-way/
答案 1 :(得分:0)
听起来很愚蠢,为我修理它的原因是:
self.answersTable.backgroundColor = [UIColor clearColor];