大赏iOS DeveloperiOS 开发

UItableView的用法及懒加载的好处

2016-04-01  本文已影响2370人  微pk笑

1、认识一下UItableView

:我们知道,不管是之前的还是当前的还是以后的App,都会多多少少应用到UItableView,有的应用甚至绝大部分用的都是表格tableView,比如说咱们常见的图片浏览器类的App,可能更多的用的是集合视图(UICollectionView),但是本质都是表格。可能你正在使用UItableView,可能你马上就要用UItableView,或者你已经用过了,不管什么情况,作为“程序猿”的我们是躲不了它的,所以就让我们共同进入UItableView应用的场景吧!


:首先让我们看看基本的UItableView的效果吧,如下图是我们经常看到的App的UItableView(也俗称表格视图)的效果(他是一格一格的呈现给我们的).


屏幕快照 2016-04-01 上午8.47.50.png

:以上便是我们常见的表格视图的基本效果,那么咋们接着往下看:

二、细说表格视图(UItableView)

2.1、 UItableView和UIScrollView的关系

:我们知道,在众多的移动应用当中,我们会看到格式各样的表格,表格:顾名思义:是一格一格的(或大或小),我们常用的 QQ的聊天界面就是一个表格,我们也知道,tableView是可以滚动的,默认支持的是垂直滚动,而ScrollView也是可以滚动的,那么他们是什么关系呢?没有错,tableView是ScrollView的子类,他继承自ScrollView,所以他能滚动就不难理解了。

2.2、UItableView常见的两种样式

屏幕快照 2016-04-01 上午8.59.47.png

:如上图所示,左边的是我们常见的普通样式,而右边的是我们所熟悉的group(朋友圈是单独的一组,扫一扫和摇一摇是一组,附近的人和漂流是一组,单独的游戏[扯个淡:比如我们爱玩的LOL]是一组)样式。

三、代码演示

:不多啰嗦,我们要实现一个如下的效果:

屏幕快照 2016-04-01 上午9.13.12.png

那么首先我们新建一个工程项目(这里采用的是Storyboard),并且设置当前表格的代理和数据源(tableview上面右键拉倒视图控制器即可),也可以在代码里面直接写,如下图所示:

屏幕快照 2016-04-01 上午9.07.59.png 屏幕快照 2016-04-01 上午9.12.45.png
#import "TableViewController.h"

@interface TableViewController ()<UITableViewDataSource,UITableViewDelegate>

@end

@implementation TableViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.tableView.dataSource = self;
    self.tableView.delegate = self;
    
}

:那么tableview展示数据需要什么条件呢?

:既然当前控制器遵循了数据源协议和代理,那么他就必须实现相应的协议方法;


#pragma mark - table view dataSource
//返回多少组的方法
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}
//返回多少行的方法
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return 10;
}
//每一行的具体数据
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    static NSString *cellIdentifier = @"MTCell";
    
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
    
    if(cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
    }
    
    cell.textLabel.text = [NSString stringWithFormat:@"Cell %ld", (long)indexPath.row];
    
    return cell;
}
* 补充:

:我们要显示上面的数据,请往下看:

#pragma mark - table view dataSource

//返回多少组数据方法
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 3;
}

//每一组返回多少行数据的方法
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    
    //第0组(section:组的意思)
    if (section == 0) {
        return 3;
    }else if(section == 1){
        return 2;
    }else{
        return 3;
    }   
}

//每一行显示的数据方法
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    UITableViewCell *cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:nil];
    if (indexPath.section ==0) {
        if (indexPath.row ==0) {
            cell.textLabel.text = @"奥迪";
        }else if (indexPath.row ==1)
            cell.textLabel.text = @"宝马";
        else{
            cell.textLabel.text = @"奔驰";
        }
    }else if (indexPath.section ==1){
        if (indexPath.row ==0) {
            cell.textLabel.text = @"本田";
        }else{
            cell.textLabel.text = @"丰田";
        }
    }else{
        if (indexPath.row ==0) {
            cell.textLabel.text = @"别克";
        }else if (indexPath.row == 1){
            cell.textLabel.text = @"福特";
        }else{
            cell.textLabel.text = @"拖拉机";
        }
    }
    return cell;
}

//返回头部文字
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
    if (section ==0) {
        return @"德系品牌";
    }else if (section==1){
        return @"日系品牌";
    }else{
        return @"美系品牌";
    }
}
//返回尾部文字
- (NSString *)tableView:(UITableView *)tableView titleForFooterInSection:(NSInteger)section
{
    if (section ==0) {
        return @"高端大气上档次,世界一流平牌!";
    }else if (section==1){
        return @"日系品牌";
    }else{
        return @"美系品牌";
    }

}
*显示效果
屏幕快照 2016-04-01 上午10.16.21.png

:上面的方法便是用最笨的方法显示数据的,一旦数据多了之后就不能把用这种方法了,我们要用常用的MVC(这里就不再详细说明怎么实现了)的设计思想。

四、懒加载

在MVC的设计模式里面,我们用到了懒加载如下:

屏幕快照 2016-04-01 上午10.25.47.png

:那么用懒加载有什么优势呢?

基于此:当你的上司强烈要求你提高系统性能的时候你就不用那么愁着没法下手了。

上一篇 下一篇

猜你喜欢

热点阅读