UITableView---总结

本文深入探讨了UITableView的基本概念、关键属性与方法,包括数据源与代理模式、常用属性及编辑模式等内容,并详细介绍了UITableView的初始化过程和数据源方法调用顺序。此外,文章还覆盖了表格视图的编辑功能、数据源方法与代理方法的调用顺序,以及如何通过注册Nib或Class来定制cell和header/footer view。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1.UITableView概述


我们生活中用到的应用几乎都用到了UITableView,例如QQ,微信,网易新闻,新浪微博等。它继承UIScrollView,可以在垂直方向上滚动,它里面的UITableViewCell可以自定义添加控件,功能强大。


2.常用属性和方法

UITableView它有两种样式:UITableViewStylePlain,UITableViewStyleGrouped tableView的样式为 plain的时候,如果设置了组头和组尾文本,在滚动的时候就会有一个悬浮效果(组头,组尾)


初始化: UITableView = [UITableView alloc] initWithFrame:(CGRect) style:(UITableViewStyle)];

1>数据源方法:<UITableViewDataSource>

@required 必须实现的方法

//返回每组的行数

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


//返回UITableVIewCell

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


@optional

//返回组数,默认是一组

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

            

//返回组头标题

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


//返回组尾标题

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


// Editing

tableView开启编辑模式

- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath;


### tableiView 数据源方法调用顺序###:

1. numberOfSectionInTableView:
2. numberOfRowsInSection: 
3. heightForRowAtIndexPath: (数据源中有多少条数据就调用多少次, 为了计算tableView的滚动范围)
4. cellForRowAtIndexPath: 
   再次调用 heightForAtIndexPath: 确认高度

2>代理方法:<UITableViewDelegate>

@optional

//返回每行的高度

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


//返回组头的高度

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


//返回组尾的高度

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


//返回自定义的组头

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


//返回自定仪的组尾

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


//选中某一行

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


//不选中某一行

- (void)tableView:(UITableView *)tableView didDeselectRowAtIndexPath:(NSIndexPath *)indexPath


3>常用属性及方法:

//行高

@property (nonatomic) CGFloat rowHeight; 


//组头高度          

@property (nonatomic) CGFloat sectionHeaderHeight; 


//组尾高度

@property (nonatomic) CGFloat sectionFooterHeight; 


//分割线的缩进

@property (nonatomic) UIEdgeInsets separatorInset 

  

//是否允许选中,默认是yes

@property (nonatomic) BOOL allowsSelection NS_AVAILABLE_IOS(3_0);  // default is YES. 


//在编辑的时候是否允许选中,默认是no

@property (nonatomic) BOOL allowsSelectionDuringEditing;                                 // default is NO. 


//是否允许多个选中,默认是no

@property (nonatomic) BOOL allowsMultipleSelection NS_AVAILABLE_IOS(5_0);                // default is NO.


//在编辑的时候是否允许多个选中,默认是no 

@property (nonatomic) BOOL allowsMultipleSelectionDuringEditing NS_AVAILABLE_IOS(5_0);   // 


//分割线的样式,默认是UITableViewCellSeparatorStyleSingleLine

@property (nonatomic) UITableViewCellSeparatorStyle separatorStyle __TVOS_PROHIBITED; // default is UITableViewCellSeparatorStyleSingleLine


//分割线的颜色

@property (nonatomic, strong, nullable) UIColor *separatorColor UI_A


//tableView的组头

@property (nonatomic, strong, nullable) UIView *tableHeaderView;   

                // default is nil. 


//tableView的组尾 

@property (nonatomic, strong, nullable) UIView *tableFooterView;  


//根据重用标识符到缓存池中找相应的cell

- (nullable __kindof UITableViewCell *)dequeueReusableCellWithIdentifier:(NSString *)identifier; 


//根据重用标识符到缓存池中找相应的HeaderFooterView

- (nullable __kindof UITableViewHeaderFooterView *)dequeueReusableHeaderFooterViewWithIdentifier:(NSString *)identifier NS_AVAILABLE_IOS(6_0);  

// Beginning in iOS 6, clients can register a nib or class for each cell.

// If all reuse identifiers are registered, use the newer -dequeueReusableCellWithIdentifier:forIndexPath: to guarantee that a cell instance is returned.

// Instances returned from the new dequeue method will also be properly sized when they are returned.


//为cell注册一个xib/class,绑定一个重用标识符

- (void)registerNib:(nullable UINib *)nib forCellReuseIdentifier:(NSString *)identifier NS_AVAILABLE_IOS(5_0);

- (void)registerClass:(nullable Class)cellClass forCellReuseIdentifier:(NSString *)identifier NS_AVAILABLE_IOS(6_0);


//为HeaderFooterView注册一个xib/class,绑定一个重用标识符

- (void)registerNib:(nullable UINib *)nib forHeaderFooterViewReuseIdentifier:(NSString *)identifier NS_AVAILABLE_IOS(6_0);

- (void)registerClass:(nullable Class)aClass forHeaderFooterViewReuseIdentifier:(NSString *)identifier NS_AVAILABLE_IOS(6_0);


//插入某一行

- (void)insertRowsAtIndexPaths:(NSArray<NSIndexPath *> *)indexPaths withRowAnimation:(UITableViewRowAnimation)animation;


//删除某一行

- (void)deleteRowsAtIndexPaths:(NSArray<NSIndexPath *> *)indexPaths withRowAnimation:(UITableViewRowAnimation)animation;


//刷新某一行(单行刷新)

- (void)reloadRowsAtIndexPaths:(NSArray<NSIndexPath *> *)indexPaths withRowAnimation:(UITableViewRowAnimation)animation NS_AVAILABLE_IOS(3_0);


//全部刷新

- (void)reloadData


//滚动到某一行 

- (void)scrollToRowAtIndexPath:(NSIndexPath *)indexPath atScrollPosition:(UITableViewScrollPosition)scrollPosition animated:(BOOL)animated;







评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值