学习这个,这一个网页就够了http://www.cnblogs.com/kenshincui/p/3931948.html

 

第一步

 tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 64, 320, 480) style:UITableViewStyleGrouped];

第二步,设置各个分组的数据

 NSArray * gruop1 = @[@"北京"];
    [dataSourse addObject:gruop1];
    NSArray * gruop2 = @[@""];
    [dataSourse addObject:gruop2];
    NSArray * gruop3 = [NSMutableArray arrayWithObjects:@"北京",@"天津",@"上海",@"广州",@"石家庄",@"承德", nil];
    [dataSourse addObject:gruop3];
    NSArray * group4 = @[@"全部城市"];
    [dataSourse addObject:group4];
    self.automaticallyAdjustsScrollViewInsets = NO;
    tableView.delegate = self;
    tableView.dataSource = self;
    [self.view addSubview:tableView];

第三步:返回分组数(方法为number开头)

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return dataSourse.count;
}

第四步:返回每个分组的标题

-(NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
    if (section == 0) {
        return @"当前选中城市";
    }else if(section ==1)
        return @"GPS定位您所在的城市";
    else if ( section ==2)
        return @"热门城市";
    else
        return @"";
}

 第五步:给cell赋值
NSArray * array = dataSourse[indexPath.section];
    cell.textLabel.text = [array objectAtIndex:indexPath.row];

几个重要的协议方法

自定义
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section

修改
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath

 
 
posted on 2015-07-06 14:03  火星的蝈蝈  阅读(273)  评论(0编辑  收藏  举报