如果您想做类似于您发布链接的常见问题解答页面的操作,您可以使用 UITableView 使用动态单元格高度。
一个简单的 UITableViewController 可能如下所示:
#import "ViewController.h"
@interface ViewController () {
NSArray *data;
int selected;
}
@end
@implementation ViewController
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
selected = -1;
data = [NSArray arrayWithObjects:
[NSDictionary dictionaryWithObjectsAndKeys:@"Title 1", @"title", @"Lorem ipsum dolor bla blab bla", @"details", nil],
[NSDictionary dictionaryWithObjectsAndKeys:@"Title 2", @"title", @"Lorem ipsum dolor bla blab bla", @"details", nil],
[NSDictionary dictionaryWithObjectsAndKeys:@"Title 3", @"title", @"Lorem ipsum dolor bla blab bla", @"details", nil],
[NSDictionary dictionaryWithObjectsAndKeys:@"Title 4", @"title", @"Lorem ipsum dolor bla blab bla", @"details", nil],
[NSDictionary dictionaryWithObjectsAndKeys:@"Title 5", @"title", @"Lorem ipsum dolor bla blab bla", @"details", nil],
[NSDictionary dictionaryWithObjectsAndKeys:@"Title 6", @"title", @"Lorem ipsum dolor bla blab bla", @"details", nil],
[NSDictionary dictionaryWithObjectsAndKeys:@"Title 7", @"title", @"Lorem ipsum dolor bla blab bla", @"details", nil],nil ];
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
if (indexPath.row == selected) {
return 130;
} else
return tableView.rowHeight;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return [data count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *cellIdentifier = @"ci";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
if (!cell) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
}
NSDictionary *tmp = [data objectAtIndex:indexPath.row];
cell.textLabel.text = nil;
static int TITLE_LABEL_TAG = 122;
UILabel *titleLabel = (UILabel *)[cell.contentView viewWithTag:TITLE_LABEL_TAG];
if (!titleLabel) {
titleLabel = [[UILabel alloc] initWithFrame:CGRectNull];
titleLabel.tag = TITLE_LABEL_TAG;
[cell.contentView addSubview:titleLabel];
titleLabel.backgroundColor = [UIColor clearColor];
titleLabel.numberOfLines = 1;
titleLabel.font = [UIFont boldSystemFontOfSize:16];
[cell.contentView addSubview:titleLabel];
}
titleLabel.frame = (CGRectMake(8, 8, cell.contentView.frame.size.width-16, 20));
titleLabel.text = [tmp objectForKey:@"title"];
static int DETAIL_LABEL_TAG = 123;
UILabel *detailLabel = (UILabel *)[cell.contentView viewWithTag:DETAIL_LABEL_TAG];
if (!detailLabel) {
detailLabel = [[UILabel alloc] initWithFrame:CGRectNull];
detailLabel.tag = DETAIL_LABEL_TAG;
[cell.contentView addSubview:detailLabel];
detailLabel.backgroundColor = [UIColor clearColor];
detailLabel.numberOfLines = 0;
}
if (indexPath.row == selected) {
detailLabel.frame = (CGRectMake(8, 30, cell.contentView.frame.size.width-16, 100));
detailLabel.hidden = NO;
detailLabel.text = [tmp objectForKey:@"details"];
} else {
detailLabel.hidden = YES;
}
return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
NSMutableArray *ip2reload = [NSMutableArray array];
if (selected > -1 && selected != indexPath.row)
[ip2reload addObject:[NSIndexPath indexPathForRow:selected inSection:0]];
[ip2reload addObject:[NSIndexPath indexPathForRow:indexPath.row inSection:0]];
selected = indexPath.row;
[tableView reloadRowsAtIndexPaths:ip2reload withRowAnimation:UITableViewRowAnimationAutomatic];
}
@end
您肯定必须根据内容计算所选行的实际行高(例如,如下所述:Iphone - 当每个单元格高度为动态时,何时计算 tableview 的 heightForRowAtIndexPath?)。
您可以使用 scrollToRowAtIndexPath:atScrollPosition:animated 来确保所选单元格位于屏幕顶部。