AppDelegate设置根视图控制器
RootViewController.h
@interface RootViewController : UIViewController<UITableViewDataSource,UITableViewDelegate>
@property(nonatomic, retain)NSArray *data;
RootViewController.m
- (void)viewDidLoad
{
[super viewDidLoad];
//创建表视图
UITableView *tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 20, 320, 460) style:UITableViewStylePlain];
tableView.delegate = self;
tableView.dataSource = self;
[self.view addSubview:tableView];
[tableView release];
//取得文件的路径
NSString *path = [[NSBundle mainBundle] pathForResource:@"news" ofType:@"plist"];
_data = [[NSArray alloc] initWithContentsOfFile:path];
}
#pragma mark - UITableView dataSource
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return _data.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *iden = @"cell_new";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:iden];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:iden] autorelease];
<span style="color:#ff0000;">//设置cell的显示行数,自动折行</span>
cell.textLabel.numberOfLines = 0;
cell.textLabel.font = [UIFont systemFontOfSize:14];
}
cell.textLabel.text = [_data objectAtIndex:indexPath.row];
return cell;
}
#pragma mark - UITableView delegate
//设置单元格的高度
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
//取得单元格需要现实的内容
NSString *text = _data[indexPath.row];
//计算文本的高度,20000是设置的高度上限
CGSize size = [text sizeWithFont:[UIFont systemFontOfSize:14] constrainedToSize:CGSizeMake(300, 20000)];
return size.height + 20;
}