1、不要忘记设置代码哇
// 创建tableview
_tableView = [[UITableView alloc] initWithFrame:view.bounds style:UITableViewStylePlain];
// 设置数据源 注意这个方法设置不对,没有数据哦!
_tableView.dataSource = self;
// 设置表格的代理
_tableView.delegate = self;
2、UITableViewStylePlain 样式
// 选中一行的时候触发
- (void) tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
cell.accessoryType = UITableViewCellAccessoryCheckmark;
}
// 离开一行的时候触发,必须设置代理哦
- (void) tableView:(UITableView *)tableView didDeselectRowAtIndexPath:(NSIndexPath *)indexPath {
NSLog(@"%d", indexPath.row);
DetailViewController *detailVc = [[DetailViewController alloc] init];
[self.navigationController pushViewController:detailVc animated:YES];
[detailVc release];
}
// 表视图sescations有多少个
- (NSInteger) numberOfSectionsInTableView:(UITableView *)tableView {
return 1;
}
// 设置行数
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return [_listArray count];
}
// 设置行数据
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *cellIdentifier = @"cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier: cellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier] autorelease];
}
NSString *fontName = _listArray[indexPath.row];
cell.textLabel.text = fontName;
cell.textLabel.textColor = [UIColor blueColor];
cell.textLabel.font = [UIFont fontWithName:fontName size:12];
return cell;
}
3、UITableViewStyleGrouped
// sections 数量 sections == 部分
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return [_listArray count];
}
// section title
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section; {
NSString *title = [NSString stringWithFormat:@"这是第%d个Sections", section + 1];
return title;
}
// sections 有几个 rows
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return [_listArray[section] count];
}
// 选中事件
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
NSLog(@"section is :%d", indexPath.section);
NSLog(@"row is:%d", indexPath.row);
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *cellIdentifier = @"cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier: cellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier] autorelease];
}
NSString *fontName = [[_listArray objectAtIndex:indexPath.section] objectAtIndex:indexPath.row];
cell.textLabel.text = fontName;
return cell;
}