美文网首页
UITableView一些常用方式(一)

UITableView一些常用方式(一)

作者: BernardChina | 来源:发表于2016-03-08 17:24 被阅读245次

在我们的日常工作,用到最多的应该就属于UITableView。大家可能最熟悉的使用方式,也许就是实现UITableViewDataSourceUITableViewDelegate的几种方法。那下面除了介绍这些基本的方法外,再介绍几种简单的,但是可能不怎么经常用的方式。
1、UITableViewDataSource的使用
在实现UITableViewDataSource的时候,有几个方法是必须要实现的,直接上代码:

// 这两个方法是必须要实现的
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
      // 在当前section显示多少rows。
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
      // 返回每一个Cell
}

下面列出几个大家可能不经常使用的也不需要必须实现的:

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView;              // Default is 1 if not implemented

- (nullable NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section;    // fixed font style. use custom view (UILabel) if you want something different
- (nullable NSString *)tableView:(UITableView *)tableView titleForFooterInSection:(NSInteger)section;

2、UITableViewDelegate的使用

UITableViewDelegate必须实现的方法

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
      // 这个方法是必须要实现的,如果此方法不是实现或者返回的CGFloat =0,其实是不执行DataSource里面的方法的

}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
       // 其实这个方法不是必须要实现的,但是在我们的实际工作过程中,一般都有这样的需求,点击cell事件。
}

TableView还有编辑模式,在下一篇文章再作仔细的介绍。

以上就是我们TableView最最常用的使用方式.下面介绍一些TableView的属性.
介绍下面几种使用场景:
1、TableView背景透明
当我们看到这个需求的时候,我们的第一想法是这样的:

self.tableView.backgroundColor = [UIColor clearColor];
//或者
self.tableView.alpha = 0;

用这样的代码,我们看到的实际效果是,我们看不到任何内容
正确的做法:

self.tableView.backgroundColor = [UIColor colorWithWhite:0 alpha:0];

2、更改TableViewseparatorInset
在默认的TableView样式中,每个cell之间有一个分割线,实际的需求中,我们可能需要取消掉这样的分割线,或者更改分割线的inset也就是各种间距,或者更改分割线的颜色。
去掉分割线:

self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;

UITableViewCellSeparatorStyleNone, UITableViewCellSeparatorStyleSingleLine, UITableViewCellSeparatorStyleSingleLineEtched
三种分割线的样式

更改分割线的Inset

self.tableView.separatorInset = UIEdgeInsetsMake(0, CCWH(72), 0, CCWH(16));

更改分割线的颜色:

self.tableView.separatorColor = [UIColor redcolor];

3、TableViewheaderview
TableView中分两种headerview,其一是tableview本身的headerview,其二是section headerview,两者之间有什么区别呢?
慢慢细细道来:
tableviewheaderview是回随着tableview的滚动而滚动的,会滚动出屏幕的,看实现代码

// 此处不是真实代码
self.tableView.tableHeaderView = UIView();

sectionheaderview会随着屏幕滚动,固定到屏幕顶部
实现只需要实现tableviewdelegate就可以了

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{}

4、TableViewfooterView
tableviewfooterviewheaderview,其实是一样的。都有两种,其一tableview本身的footerview,其二是section footerview
区别:tablevewfooterview是会滚出屏幕的,sectionfooterview会固定到底部
代码:

// TableView footerView
self.tableView.tableFooterView = UIView();
// TableView section footerView
- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section{}

5、 当Tableview页面出现键盘后,滑动隐藏键盘
直接上代码

- (void)scrollViewWillBeginDragging:(UITableView *)scrollView {
    [self.view endEditing:YES];
}

重要的代码[self.view endEditing:YES];

相关文章

  • UITableView一些常用方式(一)

    在我们的日常工作,用到最多的应该就属于UITableView。大家可能最熟悉的使用方式,也许就是实现UITable...

  • TableView基础

    总结一些UITableView常见的问题 和 常用的方法iOS UITableView的多选UITableView...

  • UITableView--iOS笔记摘录

    UITableView 概念 UITableView的常用方法 UITableView代理方法 常用 选中 编辑(...

  • tableView 的折叠效果

    在开发中 UITableView 是最常见的布局控件,除了我们熟知的一些使用方式,也有另一些相对常见但不常用的使...

  • iOS 关于UITableView的黑科技

      UITableView是我们最常用的控件了,今天我就来介绍一些关于UITableView的黑科技和一些注意的地...

  • iOS开发 UITableView 常用细节

    iOS开发 UITableView 常用细节 iOS开发 UITableView 常用细节

  • swift纯代码编写UITableView

    本文主要使用纯代码方式创建UITableView视图,主要对一些常用方法进行了描述,有兴趣的朋友可以看看。 Vie...

  • Swift3.0 Controller瘦身

    缘由 iOS开发,我们常用的组件UITableView,UICollectionView,通过代理的方式实现数据的...

  • UITbaleView优化方案

    iOS中最常用的控件UITableView,在项目中大量的使用,再次总结一下UITableView性能优化的一些点...

  • 如何优化UITableView?

    优化UITableView常用的方式有:Cell重用、缓存Cell高度、Cell数据资源缓存、渲染、减少视图数目、...

网友评论

      本文标题:UITableView一些常用方式(一)

      本文链接:https://www.haomeiwen.com/subject/sdoikttx.html