UITableView的使用及代理方法

在App开放中我们经常会使用到UITabbleView,常用于数据展示。那么使用时不得不引入两个代理方法<UITableViewDataSource,UITableViewDelegate>。 下面我们来简单的创建一个TableView并介绍下其基本属性。 @property (nonatomic,strong) UITableView * myTable; //声明对象 建议使用懒加载的方式创建,可以节省内存,然后再外部请求到数据后用.语法调用。

- (UITableView *)myTable{ if (!_myTable) { _myTable = [[UITableView alloc]initWithFrame:CGRectMake(0, 64, WIDTH, HEIGHT-64-44) style:UITableViewStylePlain]; //初始化对象并设定大小和风格样式 _myTable.delegate = self; _myTable.dataSource = self; //设置代理 _myTable.showsHorizontalScrollIndicator = NO; //不显示水平滚动条 _myTable.showsVerticalScrollIndicator = NO; //不显示竖直滚动条 _myTable.bounces = NO; //关闭弹性效果 } return _myTable; }

我们要在UITableView上展示数据,所以要有一个数据源,同理数据源也采用懒加载的方式。 @property (nonatomic,strong) NSMutableArray * dataSorce;

- (NSMutableArray *)dataSorce{ if (!_dataSorce) { _dataSorce = [[NSMutableArray alloc]init]; } return _dataSorce; }

下面开始设置代理方法:


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { return _dataSorce.count; //返回cell的个数 }


- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{ return 40; //返回cell的高度 }


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{ static NSString * string = @"patrcell"; PartCell * cell = [tableView dequeueReusableCellWithIdentifier:string]; if (!cell) { cell = [[PartCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:string]; } cell.selectionStyle = UITableViewCellSelectionStyleNone; return cell; //cell的复用及自定义cell的样式 }


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

转载于:https://juejin.im/post/5a3207155188253da72e76af

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值