IOS开发控件视图day16:TableView进阶

1、TableViewController页面

//头文件申明全局变量tableView和dataSource并设置代理
()<UITableViewDelegate,UITableViewDataSource>
@property(nonatomic,strong)UITableView *tableView;
@property (nonatomic,strong) NSMutableArray *dataSource;

//加载初始化方法
- (void)viewDidLoad {
    [super viewDidLoad];
    [self initTableView];
}

//初始化tableView
-(void) initTableView
{
    _tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, Width, Height) style:UITableViewStyleGrouped];
    _tableView.dataSource = self;
    _tableView.delegate = self;
    _tableView.separatorStyle = UITableViewCellEditingStyleNone;
    
    if (@available(iOS 11.0, *)) {
        self.tableView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
    } else {
        self.automaticallyAdjustsScrollViewInsets = NO;
    }
    self.tableView.tableHeaderView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 0, 50)];

    [self.view addSubview:_tableView];
    
    [self.tableView reloadData];
}

//可变数组接收dataSource
- (NSMutableArray *)dataSource{
    if (!_dataSource) {
        _dataSource = [NSMutableArray new];
    }
    return _dataSource;
}

//分组 numberOfSections
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 2;
}

//每组几行数据 numberOfRows
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if (section == 0) {
        return 1;
    }else(section == 1) {
        return 2;
    }
}

//组间距:组头
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 0;
}
//组间距:组尾
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
    return 0;
}
//组头页面:组头组尾页面会强制约束左右间距为0,
//若要设置左右间距,可再创建一个view来嵌套
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    return [UIView new];
}
//组头页面
- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section
{
    return [UIView new];
}

//MARK: - 行高
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath

{
    CGFloat height = 200;

    if (indexPath.section == 0) {
        height = 100;
    }else if(indexPath.section == 1) {
        if (indexPath.row == 0) {
        	height = 150;
    	}else if(indexPath.row == 1) {
         	height = 100;
    	}
    }
    return height;
}

//MARK: - 每行显示的内容 cellForRow
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
	UITableViewCell *cell;
    if (indexPath.section == 0){
    //用一个继承于UITableViewCell的文件TableViewCell1来展示cell页面,
    //一个cell页面代表一个row,文件需要导入才可以使用
        TableViewCell1 *custCell = [tableView dequeueReusableCellWithIdentifier:@"TableViewCell1"];
        if (!custCell) {
            custCell = [[TableViewCell1 alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:@"TableViewCell1"];
        }
        custCell.backgroundColor = [UIColor grayColor];
        return custCell;
        
    }else if (indexPath.section == 1){
    //该section下有两个row,若用一个cell页面来接收,则会产生复用效果
        TableViewCell2 *custCell = [tableView dequeueReusableCellWithIdentifier:@"TableViewCell2"];
        if (!custCell) {
            custCell = [[TableViewCell2 alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:@"TableViewCell2"];
        }
        custCell.backgroundColor = [UIColor grayColor];
        return custCell;
    }else{
        return cell;
    }
    //返回单元格

}

2、cell页面

- (void)awakeFromNib {
    [super awakeFromNib];
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];
}
//初始化子视图
- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        [self setSelectionStyle:UITableViewCellSelectionStyleNone];
        [self setUI];
    }
    
    return self;
}

-(void)setUI
{
	//写入需要的控件代码,add到self.contentView中
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值