我正在努力创建一个包含在故事板中创建的自定义子类化单元格的tableview。 除了“questionCell”之外,所有细胞都运行良好。此单元格仅显示其文本字段的片段(有时甚至不显示),直到我向下滚动然后再次备份。
初始外观:
向下滚动然后再向上:
我哪里错了?
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier1 = @"TextCell";
static NSString *CellIdentifier2 = @"QuestionCell";
static NSString *CellIdentifier3 = @"ImageCell";
NSInteger section = indexPath.section;
NSInteger row = indexPath.row;
UITableViewCell *cell = nil;
if (section == 0){
//the intro section
TextCell *textCell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier1 forIndexPath:indexPath];
textCell.labelText.text = _intro;
cell = textCell;
} else if (section == 4){
//this is the "the answer is . . ." section
TextCell *textCell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier1 forIndexPath:indexPath];
textCell.labelText.text = [NSString stringWithFormat:@"The only EOM to not be eliminated is %@",_theAnswer];
cell = textCell;
} else {
if (row == 0) {
NSString *item = [NSString stringWithFormat:@"question_%li", (long) section];
NSString *txt = [self valueForKey: item];
QuestionCell *qCell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier2 forIndexPath:indexPath];
qCell.questionText.text = txt;
cell = qCell;
} else if (row == 1) {
NSString *item = [NSString stringWithFormat:@"img_%li", (long)section];
NSString *img = [self valueForKey: item];
ImageCell *iCell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier3 forIndexPath:indexPath];
iCell.image.image = [UIImage imageNamed: img];
cell = iCell;
} else {
TextCell *textCell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier1 forIndexPath:indexPath];
NSString *item = [NSString stringWithFormat:@"explain_%li", (long)section];
NSString *txt = [self valueForKey: item];
textCell.labelText.text = txt;
cell = textCell;
}
}
return cell;
}
这是heightForRowAtIndexPath。如果我只是评论它并在原型单元格中设置高度,结果是相同的。
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
// Return NO if you do not want the specified item to be editable.
CGFloat hgt = 70;
if (indexPath.section == 0){
//the intro
hgt = 150;
} else if (indexPath.section == 4){
//the "answer is . . ."
hgt = 100;
} else {
if (indexPath.row == 1) {
//the image
hgt = 225;
} else if (indexPath.row == 2){
//the discussion
hgt = 180;
}
}
return hgt;
}
问题细胞只是设置了iboutlet:
#import <UIKit/UIKit.h>
@interface QuestionCell : UITableViewCell
@property (weak, nonatomic) IBOutlet UITextView *questionText;
@end