表格视图--- 基础

  //在网上找了一些表格视图,简单明了的代码不多,自己贴一个上来,有空研究一下。


@interface ViewController () <UITableViewDataSource,UITableViewDelegate>

@property (nonatomic,strong) NSMutableArray *dataArray;

@end


@implementation ViewController


- (void)viewDidLoad {

    [super viewDidLoad];

    

    [self getData];


    [self createTableView];

    

    NSString *home = NSHomeDirectory();

    NSLog(@"--=--%@",home);

}



- (void)getData{

    // 班级中,10个分组,每个分组10个人   --->100个数据

    _dataArray = [[NSMutableArray alloc] init];

    

    for (int i=0; i<10; i++) {

        NSMutableArray *mAarray = [[NSMutableArray alloc] init];

        for (int j=0; j<20; j++) {

            NSString *str = [NSString stringWithFormat:@"IOS1505 %d组中第%d个学生",i+1,j+1];

            [mAarray addObject:str];

        }

        [_dataArray addObject:mAarray];

    }

    

}


- (void)createTableView{

    UITableView *table = [[UITableView alloc] initWithFrame:self.view.bounds style:UITableViewStyleGrouped];

    

    table.dataSource = self;

    table.delegate = self;

    

    [self.view addSubview:table];

}


// 每组中的行数

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{

    NSArray *array = _dataArray[section];

    

    return array.count;

}


// 分组

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{

    return _dataArray.count;

}


// 单元格详细内容

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{

//    UITableViewCell *cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:nil];

    

    // 一个屏幕14单元格 --- 创建15个单元格

    

    // cell的复用

    static NSString *cellID = @"QF";

    // 去缓存池中找

    static int num = 0;

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellID];

    if (cell == nil) {

        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:cellID];

        num ++;

        NSLog(@"num:%d",num);

    }


    

    cell.textLabel.text = @"标题";

    cell.detailTextLabel.text = _dataArray[indexPath.section][indexPath.row];

    

//    if (indexPath.section == 1) {

//        if (indexPath.row == 2) {

//            cell.textLabel.text = @"--------";

//            cell.detailTextLabel.text = @"------------";

//        }

//    }


    // 专门有个方法处理

    cell.accessoryType = UITableViewCellAccessoryDetailDisclosureButton ;

    

    if (indexPath.row == 1) {

       // cell.backgroundColor = [UIColor redColor];

    }

    

    return cell;

}


// 分组标题

- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section{

    if (section == 0) {

        return @"第一组";

    }

    if (section == 1) {

        return @"第二组";

    }

    if (section == 2) {

        return @"第三组";

    }

    return @"其他";

}


// 分组脚注

- (NSString *)tableView:(UITableView *)tableView titleForFooterInSection:(NSInteger)section{

    return @"脚注";

}


// 右边索引

- (NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView{

    // 注意:和分组标题没有任何关系,只和分组位置有关

//    return @[@"a",@"b",@"c"];

//    return @[@"test",@"haha",@"hehe",@"xixi"];

    

    NSMutableArray *mArray = [[NSMutableArray alloc] init];

    for (int i='A'; i<='Z'; i++) {

        [mArray addObject:[NSString stringWithFormat:@"%c",i]];

    }

    return mArray;

}


// 单元格行高

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{

    if (indexPath.row == 1) {

        return 100;

    }

    

    return 44;

}


// 分组标题行高

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{

    return 200;

}


// 分组脚注行高

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{

    return 100;

}


// 分组标题的UIView

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{

    if (section == 3) {

        UIImageView *imageView = [[UIImageView alloc] init];

        imageView.image = [UIImage imageNamed:@"image0"];

        return imageView;

    }

    return nil;

}


// 分组脚注UIView

- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section{

    if (section == 1) {

        UIImageView *imageView = [[UIImageView alloc] init];

        imageView.image = [UIImage imageNamed:@"image0"];

        return imageView;

    }

    return nil;

}


// 选中某行的代理方法

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{

    UIAlertView *alert = [[UIAlertView alloc] init];

    alert.title = @"提示";

    NSString *str = [NSString stringWithFormat:@"%d-%d个学生",indexPath.section+1,indexPath.row+1];

    alert.message = str;

    [alert addButtonWithTitle:@"确定"];

    [alert show];

}


// 单元格右边感叹号

- (void)tableView:(UITableView *)tableView accessoryButtonTappedForRowWithIndexPath:(NSIndexPath *)indexPath{

    UIAlertView *alert = [[UIAlertView alloc] init];

    alert.title = @"错误提示";

    alert.message = @"错误!!!!!!!!!!!!!!!!";

    [alert addButtonWithTitle:@"确定"];

    [alert show];

}


- (void)didReceiveMemoryWarning {

    [super didReceiveMemoryWarning];

    // Dispose of any resources that can be recreated.

}


@end



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值