【发布时间】:2010-04-26 07:20:50
【问题描述】:
我正在开发一个分组的 UITableView,它有 4 个部分,每部分一行,并且单元格的行为很奇怪。 单元格是普通的 UITableViewCells,但单元格的高度约为 60 - 80 像素。 现在表格视图用圆角正确渲染单元格,但是当我选择单元格时,它们显示为蓝色和矩形。我不知道为什么这些单元格会这样,因为我有另一个具有自定义单元格和 88 像素高度的分组 UITableView 并且这些单元格按应有的方式工作。
如果我将高度更改为 44 像素的默认高度,则单元格的行为就像应该的那样。有谁知道这种行为以及原因是什么?
就像我提到的那样,我没有做任何花哨的事情,我在一个静态的、分组的 UITableView 中使用默认 UITableViewCells,它有 4 个部分,每个部分有 1 行。
新世纪福音战士2100
编辑:好的,这是我的代码的相关部分。因为我只为此 tableview 使用固定数量的单元格,所以我将单元格存储在 2d NSMutableArray 中。我在 -(void)viewDidLoad 方法中设置单元格,并且相应的委托方法使用存储的单元格访问数组。
如果它们被选中,我看不到任何会导致单元格出现这种奇怪行为的东西。
Edit2:对不起,我将单元格存储在数组中的原因不仅仅是单元格之间。如果视图发生变化并且 UITableView 重新出现,则单元格将与自定义单元格交换。那才是储存细胞的真正原因。这就像某种“添加新电子邮件”——行为类型,例如在 iphone 的联系人应用程序中。
-(void)viewDidLoad {
// set up the AccompanyingLecture cell
UITableViewCell *accompanyingLectureCell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:@"AccompanyingLectureCell"];
accompanyingLectureCell.textLabel.textAlignment = UITextAlignmentCenter;
accompanyingLectureCell.detailTextLabel.textAlignment = UITextAlignmentCenter;
accompanyingLectureCell.textLabel.text = NSLocalizedString(@"New Accompanying Lecture", @"");
accompanyingLectureCell.detailTextLabel.text = NSLocalizedString(@"Lecturer, Time, Location, etc.", @"");
accompanyingLectureCell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
accompanyingLectureCell.frame = CGRectMake(0, 0, 320, 82);
// initialize datasource for all four sections
datasource = [[NSMutableArray alloc] initWithObjects:[NSMutableArray arrayWithObject:[lecturerCell autorelease]], [NSMutableArray arrayWithObject:[lectureDetailsCell autorelease]], [NSMutableArray arrayWithObject:[timeAndLocationCell autorelease]], [NSMutableArray arrayWithObject:[accompanyingLectureCell autorelease]], nil];
}
- (UITableViewCell *) tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
return [[datasource objectAtIndex:indexPath.section] objectAtIndex:indexPath.row];
}
- (CGFloat) tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
CGRect currentFrame = [[[datasource objectAtIndex: indexPath.section] objectAtIndex:indexPath.row] frame];
return currentFrame.size.height;
}
【问题讨论】:
-
发布您的
-tableView:cellForRowAtIndexPath:方法和任何其他接触单元格的委托方法可能会有所帮助,例如-tableView:heightForRowAtIndexPath:
标签: iphone uitableview