UITableView的两种内置样式:UITableViewStylePlain 和 UTTableViewStyleGrouped(分组)
1、分组例子 : storyboard选择一个UITableView,style设置为Grouped
// ViewController.m
#pragma mark - 数据源方法
#pragma mark 一共有多少组数据
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 2;
}
#pragma mark 第section组有多少行数据
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
}
#pragma mark 返回每一行显示的具体数据
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
// 组 indexPath.section
// 行 indexPath.row
UITableViewCell *cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:nil];
// 设置cell上面显示的文本数据
cell.textLabel.text = city;
return cell;
}
#pragma mark 第section组的标题
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
}
#pragma mark
- (NSString *)tableView:(UITableView *)tableView titleForFooterInSection:(NSInteger)section
{
}
#pragma mark 右侧快捷分组
- (NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView
{
return self.provinces;
//return @[@"A", @"B", @"C"];
}
2、单组例子
UITableViewCell *cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:nil];
// 图片的文件名
NSString *name = [NSString stringWithFormat:@"00%d.png", indexPath.row + 1];
// 设置左边的图标
cell.imageView.image = [UIImage imageNamed:name];
// 设置主标题
cell.textLabel.text = @"123213213";
// 设置副标题
cell.detailTextLabel.text = @"详细描述------";
// 设置cell的右边箭头样式 ->
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
return cell;
#pragma mark - 代理方法
#pragma mark 选中了某一行就会调用
- (void)tableView:(UITableView *)tableView didSelectRowA