带你一步步的读懂UITableView

相信对于学习iOS的童鞋,平时接触最多的,用到最多的就是UITableView了,那么你对UITableView了解多少呢?你真的会正确使用它吗?
下面就让我们一起走进UITableView的世界,去一点点的解析它,读懂它。

懒加载创建UITableView

-(UITableView *)tableView
{
    if (!_tableView) {
        _tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, self.view.width, self.view.height) style:UITableViewStyleGrouped];
        _tableView.separatorStyle = UITableViewCellStyleDefault;
        _tableView.delegate = self;
        _tableView.dataSource = self;
        _tableView.backgroundColor = [UIColor whiteColor];
    }
    return _tableView;
}

separatorStylecell显示的样式

UITableViewCellStyleDefault,    // 左侧显示textLabel(不显示detailTextLabel),imageView可选(显示在最左边)
UITableViewCellStyleValue1,        // 左侧显示textLabel、右侧显示detailTextLabel(默认蓝色),imageView可选(显示在最左边)
UITableViewCellStyleValue2,        // 左侧依次显示textLabel(默认蓝色)和detailTextLabel,imageView可选(显示在最左边)
UITableViewCellStyleSubtitle    // 左上方显示textLabel,左下方显示detailTextLabel(默认灰色),imageView可选(显示在最左边)

styleUITableView的显示风格

UITableViewStylePlain,          //普通样式
UITableViewStyleGrouped        // 分组样式

UITableView的内部属性

设置表示图的行高(默认为44)
@property (nonatomic) CGFloat rowHeight

设置分组样式时的头视图高度和尾视图高度(当代理方法没有实现时才有效)
@property (nonatomic) CGFloat sectionHeaderHeight
@property (nonatomic) CGFloat sectionFooterHeight

设置一个行高的估计值(默认为0,表示没有估计,7.0之后可用),注意:这个属性官方的解释是如果你的tableView的行高是可变的,那么设计一个估计高度可以加快代码的运行效率。
下面这两个属性和上面相似,分别设置分区头视图和尾视图的估计高度(7.0之后可用)
@property (nonatomic) CGFloat estimatedRowHeight 
@property (nonatomic) CGFloat estimatedSectionHeaderHeight 
@property (nonatomic) CGFloat estimatedSectionFooterHeight 

设置分割线的位置
@property (nonatomic) UIEdgeInsets separatorInset

设置分割线的风格
@property (nonatomic) UITableViewCellSeparatorStyle separatorStyle
这个风格是一个枚举,如下:
typedef NS_ENUM(NSInteger, UITableViewCellSeparatorStyle) 
{   
  UITableViewCellSeparatorStyleNone,//无线    
  UITableViewCellSeparatorStyleSingleLine,//有线  
  UITableViewCellSeparatorStyleSingleLineEtched 
 };
设置分割线颜色
@property (nonatomic, strong, nullable) UIColor *separatorColor

设置分割线毛玻璃效果(IOS8之后可用) 
@property (nonatomic, copy, nullable) UIVisualEffect *separatorEffect

设置tableView头视图
@property (nonatomic, strong, nullable) UIView *tableHeaderView
设置tableView尾视图                        
@property (nonatomic, strong, nullable) UIView *tableFooterView

从复用池中取cell                      
- (nullable __kindof UITableViewCell *)dequeueReusableCellWithIdentifier:(NSString *)identifier

获取一个已注册的cell
- (__kindof UITableViewCell *)dequeueReusableCellWithIdentifier:(NSString *)identifier forIndexPath:(NSIndexPath *)indexPath 

从复用池获取头视图或尾视图
- (nullable __kindof UITableViewHeaderFooterView *)dequeueReusableHeaderFooterViewWithIdentifier:(NSString *)identifier 

通过xib文件注册cell
- (void)registerNib:(UINib *)nib forCellReuseIdentifier:(NSString *)identifier;

通过OC类注册cell
- (void)registerClass:(nullable Class)cellClass forCellReuseIdentifier:(NSString *)identifier 
上面两个方法是IOS6之后的方法。

通过xib文件和OC类获取注册头视图和尾视图
- (void)registerNib:(nullable UINib *)nib forHeaderFooterViewReuseIdentifier:(NSString *)identifier 
- (void)registerClass:(nullable Class)aClass forHeaderFooterViewReuseIdentifier:(NSString *)identifier 

更改自定义分隔符值的解释方式。默认值是UITableViewSeparatorInsetFromCellEdges
@property (nonatomic) UITableViewSeparatorInsetReference separatorInsetReference
枚举类型
typedef NS_ENUM(NSInteger, UITableViewSeparatorInsetReference) {
    UITableViewSeparatorInsetFromCellEdges,//默认值,表示separatorInset是从cell的边缘的偏移量
    UITableViewSeparatorInsetFromAutomaticInsets//表示separatorInset属性值是从一个insets的偏移量
}

设置tableView背景view视图
@property (nonatomic, strong, nullable) UIView *backgroundView

获取section个数(只读属性)
@property (nonatomic, readonly) NSInteger numberOfSections;
设置每个section的行数
- (NSInteger)numberOfRowsInSection:(NSInteger)section;

获取分组的大小(包括头视图,所有行和尾视图)
- (CGRect)rectForSection:(NSInteger)section;    
 
根据分组分别获取头视图,尾视图和行的高度                               
- (CGRect)rectForHeaderInSection:(NSInteger)section;
- (CGRect)rectForFooterInSection:(NSInteger)section;
- (CGRect)rectForRowAtIndexPath:(NSIndexPath *)indexPath;

获取某个点在tableView中的位置信息
- (nullable NSIndexPath *)indexPathForRowAtPoint:(CGPoint)point;    

获取某个cell在tableView中的位置信息                    
- (nullable NSIndexPath *)indexPathForCell:(UITableViewCell *)cell;                      

根据一个矩形范围返回一个信息数组,数组中是每一行row的位置信息
- (nullable NSArray<NSIndexPath *> *)indexPathsForRowsInRect:(CGRect)rect;       

通过位置路径获取cell                    
- (nullable __kindof UITableViewCell *)cellForRowAtIndexPath:(NSIndexPath *)indexPath;   

获取所有可见的cell
@property (nonatomic, readonly) NSArray<__kindof UITableViewCell *> *visibleCells;

获取所有可见行的位置信息
@property (nonatomic, readonly, nullable) NSArray<NSIndexPath *> *indexPathsForVisibleRows;

根据分组获取头、尾视图
- (nullable UITableViewHeaderFooterView *)headerViewFor
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
在iOS中,一个视图只能有一个UITableView。但是可以通过创建多个UITableView来实现一个视图中显示多个表格的效果。以下是一个示例代码: 首先,你需要在视图控制器中添加多个UITableView的实例变量: ```swift class YourViewController: UIViewController { var tableView1: UITableView! var tableView2: UITableView! // ... } ``` 然后,在视图加载完成后,你可以创建和配置这些UITableView的实例: ```swift override func viewDidLoad() { super.viewDidLoad() // 创建第一个UITableView tableView1 = UITableView(frame: CGRect(x: 0, y: 0, width: view.frame.width, height: view.frame.height/2)) tableView1.dataSource = self tableView1.delegate = self view.addSubview(tableView1) // 创建第二个UITableView tableView2 = UITableView(frame: CGRect(x: 0, y: view.frame.height/2, width: view.frame.width, height: view.frame.height/2)) tableView2.dataSource = self tableView2.delegate = self view.addSubview(tableView2) // ... } ``` 接下来,你需要实现UITableViewDataSource和UITableViewDelegate协议的相关方法来提供表格的数据和处理交互事件。例如: ```swift extension YourViewController: UITableViewDataSource { func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { if tableView == tableView1 { // 返回第一个UITableView的行数 return 10 } else if tableView == tableView2 { // 返回第二个UITableView的行数 return 5 } return 0 } func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { let cell = tableView.dequeueReusableCell(withIdentifier: "Cell", for: indexPath) if tableView == tableView1 { // 配置第一个UITableView的单元格 cell.textLabel?.text = "Table View 1 - Row \(indexPath.row)" } else if tableView == tableView2 { // 配置第二个UITableView的单元格 cell.textLabel?.text = "Table View 2 - Row \(indexPath.row)" } return cell } } extension YourViewController: UITableViewDelegate { func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) { if tableView == tableView1 { // 处理第一个UITableView的行选中事件 print("Table View 1 - Row \(indexPath.row) selected") } else if tableView == tableView2 { // 处理第二个UITableView的行选中事件 print("Table View 2 - Row \(indexPath.row) selected") } } } ``` 这样,你就可以在同一个视图中使用多个UITableView了。记得在视图控制器中遵循UITableViewDataSource和UITableViewDelegate协议,并在视图加载完成后设置数据源和代理。 希望这能帮到你!

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值