IOS学习 UITableView 委托方法

#import <UIKit/UIKit.h>

#define VIEW_WIDTH self.view.bounds.size.width

#define VIEW_HEIGHT self.view.bounds.size.height

#import "DetaiViewController.h"


@interface HomeViewController : UIViewController<UITableViewDataSource,UITableViewDelegate>

{

    UITableView *_tableView;

}


@property (nonatomic,retain) NSArray *listArray;


@end



@implementation HomeViewController


- (void)viewDidLoad {

    [super viewDidLoad];

    // Do any additional setup after loading the view.

    

    _listArray = @[@"UITableViewStylePlain",@"UITableViewStyleGrouped"];

    

    _tableView = [[UITableView alloc]initWithFrame:self.view.bounds style:UITableViewStylePlain];

    [self.view addSubview:_tableView];

    

    //设置数据源

    _tableView.dataSource = self;

    //设置委托

    _tableView.delegate = self;

}


#pragma mark - UITableView DataSource

//表视图中存在section的个数,默认为1

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

    return 1;

}


//section中包含row的数量

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

    return _listArray.count;

}


//创建单元格

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

    static NSString *cellIndentifier = @"cell";

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIndentifier];

    if (cell == nil) {

        cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIndentifier];

    }

    cell.textLabel.text = _listArray[indexPath.row];

    return cell;

}


#pragma mark - UITableView delegate

//当用户选择某一行时

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

    

    DetaiViewController *detaiVC = [[DetaiViewController alloc]init];

    //获取点击内容

    detaiVC.isPlain = indexPath.row ? NO : YES ;

    [self.navigationController pushViewController:detaiVC animated:YES];

}




@interface DetaiViewController : UIViewController<UITableViewDelegate,UITableViewDataSource>{

    NSArray *array;

    NSMutableArray *tempArray;

    NSMutableArray *fontArray;

}


@property (nonatomic,assign) BOOL isPlain;


@end




@implementation DetaiViewController


- (void)viewDidLoad {

    [super viewDidLoad];

    // Do any additional setup after loading the view.

    

    array = [UIFont familyNames];

    

    //根据选择内容,使用表格的类型

    UITableViewStyle style = self.isPlain ? UITableViewStylePlain :UITableViewStyleGrouped ;

    NSLog(@"%ld",(long)style);

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

    [self.view addSubview:detaiTV];

    

    fontArray = [[NSMutableArray alloc]init];

    

    for (int i = 0; i<array.count; i++) {

        //取出字体

        NSString *font = array[i];

        //5整除的时候,创建tempArray数据,添加至fontArray

        if (i %5 == 0) {

            tempArray = [[NSMutableArray alloc]init];

            [fontArray addObject:tempArray];

        }

        [tempArray addObject:font];

    }

    detaiTV.delegate = self;

    detaiTV.dataSource = self;

}


#pragma mark - UITableView DataSource

//表视图中存在section的个数,默认为1

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

    return fontArray.count;

}



//section中包含row的数量

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

    return [fontArray[section] count];

}


//创建单元格

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

    static NSString *cellIndentifier = @"cell";

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIndentifier];

    if (cell == nil) {

        cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIndentifier];

    }

    

    cell.textLabel.text = [[fontArray objectAtIndex:indexPath.section] objectAtIndex:indexPath.row];

 

    return cell;    

}


//设置section头部视图的title   此方法与自定义的头部视图不能并存

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

    NSString  *title = [NSString stringWithFormat:@"%ldsection",(long)section+1];

    return title;

}


//设置section尾部视图的title  此方法与自定义的尾部视图不能并存

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

//    return @"-----------";

//}


#pragma mark - UITableView Delegate

//设置行高

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

    if (indexPath.row == 0 && indexPath.section == 2 ) {

        return 80 //第三个section的第一行

    }return 44;

}


//设置section头部的高度

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

    if (section == 0) {

        return 80;

    }return 25;

}


//设置section底部的高度

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

    if (section == 1) {

        return 100;

    }return 25;

}


//设置section自定义的头部视图

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

//    UIView *headerView = [[UIView alloc]initWithFrame:CGRectZero];  //设置x,y,,,都为0

//    headerView.backgroundColor = [UIColor orangeColor];

//    return headerView;

//}


//设置section自定义的尾部视图  默认高度为25

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

    UIView *footerView = [[UIView alloc]initWithFrame:CGRectZero];  //设置x,y,,,都为0

    footerView.backgroundColor = [UIColor cyanColor];

    UILabel *tipLabel = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, 200, 25)];

    tipLabel.text = @"***********";

    [footerView addSubview:tipLabel];

    return footerView;

}


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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值