UITableView之(一):基本概念和用法

UITableView 是iOS中最重要的控件,几乎所有的页面都可以用UITableView完成。

 

基本用法:

tableView的使用需要遵循代理和数据源,这也是一种非常棒的设计模式,数据源模式可以近似为代理模式。

 

    // 基本属性
    // 设置tableView的类型
    // UITableViewStylePlain    基本类型,分区头标题会悬浮
    // UITableViewStyleGrouped  分组的类型,分区头标题不会悬浮
    UITableView *tableView = [[UITableView alloc] initWithFrame:
    CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height) style:(UITableViewStyleGrouped)];
    
    // 设置背景
    tableView.backgroundColor = [UIColor whiteColor];
    
    // 设置分割线的类型
    tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    
    // 设置分割线颜色
    tableView.separatorColor = [UIColor blackColor];
    
    // 设置分割线的位置
    tableView.separatorInset = UIEdgeInsetsMake(0, 20, 0, 20);
    
    // 数据源和代理
    tableView.dataSource = self;
    tableView.delegate = self;
    
    self.tableView = tableView;
    [self.view addSubview:self.tableView];

 

 

设置数据源方法和代理方法:

 

// 数据源方法
@protocol UITableViewDataSource<NSObject>

@required // 必须实现的方法

// 每个section有多少rows
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section;

// 设置cell的详情,需要用到的重要概念,cell的重用机制
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath;


@optional //可选的实现方法

// 设置section的数量,默认为1
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView;

// section由两部分组成,header和footer,分别设置各自需要展示的字符串,也可以自定义显示的样式,需要用到其他的方法
- (nullable NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section;
- (nullable NSString *)tableView:(UITableView *)tableView titleForFooterInSection:(NSInteger)section;

// Editing 编辑需要用到的方法
// 哪些row可以被编辑
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath;

// Moving/reordering  移动
// 哪些row可以被移动
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath;

// Index
// 设置索引
- (nullable NSArray<NSString *> *)sectionIndexTitlesForTableView:(UITableView *)tableView __TVOS_PROHIBITED;  // return list of section titles to display in section index view (e.g. "ABCD...Z#")
// 设置索引的响应
- (NSInteger)tableView:(UITableView *)tableView sectionForSectionIndexTitle:(NSString *)title atIndex:(NSInteger)index __TVOS_PROHIBITED;  // tell table which section corresponds to section title/index (e.g. "B",1))

// Data manipulation - insert and delete support 删除和插入实现的方法
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath;

// Data manipulation - reorder / moving support 移动实现的方法
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)sourceIndexPath toIndexPath:(NSIndexPath *)destinationIndexPath;

@end
 


查看代理的方法:

 

 

@protocol UITableViewDelegate<NSObject, UIScrollViewDelegate>

@optional // 可选的常用的方法

// 设置对应的高度
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath;
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section;
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section;

// 自定义header和footer
- (nullable UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section;
- (nullable UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section;

// 设置编辑的样式
- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath;

// 移动or排序
- (NSIndexPath *)tableView:(UITableView *)tableView targetIndexPathForMoveFromRowAtIndexPath:(NSIndexPath *)sourceIndexPath toProposedIndexPath:(NSIndexPath *)proposedDestinationIndexPath;

@end

 

 

 

 

 

 

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值