リンクを投稿した FAQ ページと同様のことをしたい場合は、動的なセルの高さを使用して 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 - when to calculate heightForRowAtIndexPath when each cell height is dynamic? )。
また、 scrollToRowAtIndexPath:atScrollPosition:animated を使用して、選択したセルが画面の真上にあることを確認できます。