UITableView一些常用方式(一)

2016-03-08  本文已影响245人  BernardChina

在我们的日常工作,用到最多的应该就属于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];

上一篇 下一篇

猜你喜欢

热点阅读