`
sxtygyxy
  • 浏览: 2460 次
社区版块
存档分类
最新评论

UITableView

    博客分类:
  • Ios
阅读更多

#import "ViewController.h"

@interface ViewController ()<UITableViewDataSource>{

    NSArray *gdCities;
    NSArray *hnCities;
}

@end



@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
   
    //1.添加tableview
    UITableView *tableView = [[UITableView alloc] initWithFrame:self.view.bounds style:UITableViewStylePlain];
   
    tableView.dataSource = self;
   
    [self.view addSubview:tableView];
   
    //2.初始化数据
    gdCities = @[@"广东", @"深圳", @"梅州", @"东莞"];
    hnCities = @[@"长沙", @"岳阳", @"邵阳", @"益阳"];
}


#pragma mark - 数据源方法
#pragma mark - 一共有多少组
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 2;
}

#pragma mark - 一共有多少行
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    if(section == 0){//广东
//        return 4;
        return gdCities.count;
    }else{//湖南
//        return 2;
        return  hnCities.count;
    }
}

#pragma mark - 返回每行显示的内容
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    //indexpath 标示唯一的一行
//    indexPath.section == 0;
//    indexPath.row == 0;
   
    UITableViewCell *cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:nil];
   
    //设置cell显示的文字
    NSString *text = nil;
    if(indexPath.section == 0){//广东
//        if(indexPath.row == 0){
//            text = @"广州";
//        }else if(indexPath.row == 1){
//            text = @"深圳";
//        }else if (indexPath.row == 2){
//            text = @"梅州";
//        }else if (indexPath.row == 3){
//            text = @"东莞";
//        }
       
        text = [gdCities objectAtIndex:indexPath.row];
       
    }else{//湖南
//        if(indexPath.row == 0){
//            text = @"长沙";
//        }else if(indexPath.row == 1){
//            text = @"岳阳";
//        }
       
        text = [hnCities objectAtIndex:indexPath.row];
       
    }
   
    cell.textLabel.text = text;
   
    //设置cell右边剪头
    cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;

   
    return cell;
}

#pragma mark - 每组标题
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section{
    if(section == 0){
        return @"广东省";
    }else{
        return @"湖南省";
    }
}

#pragma mark



@end
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics