UI-9-UITableView

课程要点:

  • UITableView及其两种风格和三部分
  • UITableViewController
  • UITableViewCell及其四种风格
  • 通过代理给UITableView设置cell
  • 性能优化
  • tableView的其他代理方法

UITableView及其两种风格和三部分

UITableView是一个能滑动,能够承载多个单元格的视图控件,例如下图:能够滑动的是tableView,上面的每一条新闻都放在一个cell里面。

UITableView有两种风格

1、UITableViewStylePlain(普通风格)

2、UITableViewStyleGrouped(分组风格)

    //在设置frame的同时设置一种风格,现在先选择普通风格,后面讲到段的时候会用效果来展示两者的不同    UITableView *tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, 320, 480) style:UITableViewStylePlain];
    tableView.backgroundColor = [UIColor grayColor];

    [self.view addSubview:tableView];

UITableView由三部分组成

1、表头

2、UITableViewCell(单元格)

3、表尾

    //给tableView设置表头
    tableView.tableHeaderView = [self addHeaderView];
    //给tableView设置表尾
    tableView.tableFooterView = [self addFooterView];

PS:设置表头和表尾是通过点属性来设置的,都需要赋值一个View,所以自己写了两个返回值是View的方法来设置表头和表尾的试图,如果将某个试图作为表头或者表尾,该试图的x,y,width都按照tableView默认的来,只要height会变成表头的高。

//设置表头
- (UIView *)addHeaderView{

    UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, 0, 40)];

    label.text = @"表头";

    label.backgroundColor = [UIColor yellowColor];

    return label;
}

//设置表尾
- (UIView *)addFooterView{

    UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, 0, 40)];

    label.text = @"表尾";

    label.backgroundColor = [UIColor yellowColor];

    return label;
}

运行效果如下:

PS:此时只有表头和表尾,中间没有UITableViewCell(单元格)。这是因为目前我只给他设置了表头和表尾,并未设置单元格。

UITableViewController

每个控制器都自带一个视图,UITableViewController自带的试图是一个TableViewController。如果一个页面只有TableView的话可以采用这种控制器。自己可以私下尝试一下。这里不做过多解释了。

UITableViewCell及其四种风格

cell自带textLabel、detailTextLabel、imageView 不同风格的cell,这三种控件的摆放位置不同

1、UITableViewCellStyleDefault

图片居左,textlabel在图片右边,detailTextLabel默认不显示

2、UITableViewCellStyleValue1

图片居左,textlabel在图片的右边,detailTextLabel居右

3、UITableViewCellStyleValue2

左边一个主标题textLabel,挨着右边一个副标题detailTextLabel

4、UITableViewCellStyleSubtitle

图片居左,textLabel在图片的右边,deetailTextlabel在textlabel的下方。

将viewDidiLoad里的代码都给注释了,然后在ViewDidLoad中调用这个方法。

  //addTableViewCell方法是我自己写的,在这个方法内我将创建好的cell放到self.view上面  [self addTableViewCell];

实现addTableViewCell方法

- (void)addTableViewCell{

    //UITableCell也是一种试图空间,在这里初始化的同时给cell设置风格和标识符
    UITableViewCell *cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:@"cellID"];

    cell.frame = CGRectMake(0, 150, 320, 40);

    cell.backgroundColor = [UIColor brownColor];

    [self.view addSubview:cell];

    //cell自带textLabel、detailTextLabel、imageView 不同风格的cell,这三种控件的摆放位置不同

    /*
     * UITableViewCellStyleDefault
       图片居左,textlabel在图片右边,detailTextLabel默认不显示

     * UITableViewCellStyleValue1
       图片居左,textlabel在图片的右边,detailTextLabel居右

     * UITableViewCellStyleValue2
       左边一个主标题textLabel,挨着右边一个副标题detailTextLabel

     * UITableViewCellStyleSubtitle
       图片居左,textLabel在图片的右边,deetailTextlabel在textlabel的下方。
     */

    cell.textLabel.text = @"张三";
    cell.textLabel.textColor = [UIColor redColor];
    cell.detailTextLabel.text = @"张三是张家人";
    cell.imageView.image = [UIImage imageNamed:@"zhangsan.png"];

    //右边出现小箭头
    cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;

    //圈i加箭头
    cell.accessoryType = UITableViewCellAccessoryDetailDisclosureButton;

    //对号
    cell.accessoryType = UITableViewCellAccessoryCheckmark;

    //圈i
    cell.accessoryType = UITableViewCellAccessoryDetailButton;

}

运行效果如下:

通过代理给UITableView设置cell

PS:之前我们学习了两种控件,UITableView和UITableViewCell,他两之间的关系应该是UITableView中有很多排列的UITableViewCell。接下俩我们要做的就是通过代理将两者给关联起来。

将viewDidLoad中的内容恢复至此:

    UITableView *tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, 320, 480) style:UITableViewStylePlain];

    //给tableView设置表头
    tableView.tableHeaderView = [self addHeaderView];
    //给tableView设置表尾
    tableView.tableFooterView = [self addFooterView];

    tableView.backgroundColor = [UIColor grayColor];

    [self.view addSubview:tableView];

本模块的标题已经说明咱们是通过代理方法来设置cell。

1、遵守协议

//这两个协议分别有不同的方法@interface ContactViewController()<UITableViewDataSource,UITableViewDelegate>
@end

2、挂代理

tableView.delegate = self;
tableView.dataSource = self;

3、实现代理方法,给tableView设置cell,有两个属性是必须的

1)tableView里面有多少行.(- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section)

2)每一行是个什么样的cell.(- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath)

//返回tableView有多少行
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{

    return 10;
}
//给每一行设置一个cell
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
       UITableViewCell *cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:nil];

    cell.textLabel.text = @"张三";
    cell.textLabel.textColor = [UIColor redColor];
    cell.detailTextLabel.text = @"张三是张家人";
    cell.imageView.image = [UIImage imageNamed:@"zhangsan.png"];

    //右边出现小箭头
    cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;

    return cell;

}

完整代码如下:

//
//  ContactViewController.m
//  UI-No8-UITableView
//
//  Created by  on 15/12/14.
//  Copyright © 2015年 王立广. All rights reserved.
//

#import "ContactViewController.h"

@interface ContactViewController()<UITableViewDataSource,UITableViewDelegate>@end

@implementation ContactViewController

- (void)viewDidLoad{
    [super viewDidLoad];

    UITableView *tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, 320, 480) style:UITableViewStylePlain];

    //给tableView设置表头
    tableView.tableHeaderView = [self addHeaderView];
    //给tableView设置表尾
    tableView.tableFooterView = [self addFooterView];

    tableView.delegate = self;
    tableView.dataSource = self;

    tableView.backgroundColor = [UIColor grayColor];

    [self.view addSubview:tableView];

}

//设置表头
- (UIView *)addHeaderView{

    UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, 0, 40)];

    label.text = @"表头";

    label.backgroundColor = [UIColor yellowColor];

    return label;
}

//设置表尾
- (UIView *)addFooterView{

    UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, 0, 40)];

    label.text = @"表尾";

    label.backgroundColor = [UIColor yellowColor];

    return label;
}

//返回tableView有多少行
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{

    return 10;
}

//tableView上将要显示一个cell时会调用这个方法,在方法内设置一个cell并返回便可将cell放到tableView上。
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{

    UITableViewCell *cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:nil];

    cell.textLabel.text = @"张三";
    cell.textLabel.textColor = [UIColor redColor];
    cell.detailTextLabel.text = @"张三是张家人";
    cell.imageView.image = [UIImage imageNamed:@"zhangsan.png"];

    //右边出现小箭头
    cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;

    return cell;
}
@end

性能优化

上面学习的东西,咱们已经能够将UItableView和UITableViewCell结合起来了,在上面的代码中有一个问题是每次出现一个新的cell,系统会创建一个新的cell对象。这样是十分浪费内存的,接下来咱们重写优化这个方法。

//tableView上将要显示一个cell时会调用这个方法,在方法内设置一个cell并返回便可将cell放到tableView上。
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    /*
     * UITableView每次滑动,必定有消失的cell,系统会自动将这些消失的cell放到缓存池里,需要新cell时,系统先在缓存池里看是否有cell,有的话就利用,没有的话就新建。

     * 前提:UITableView滑动
       1、旧的cell消失,系统自动将这个cell放到缓存池里面。
       2、新的cell要显示,就会代理方法。
          1) 首先看缓存池里面有没有cell
          2)  如果有cell就利用,如果没有就新建
          3) 在代理方法中返回设置的cell
     */

  static NSString *cellID = @"cellID";

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellID];

    if (cell == nil) {

        cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:cellID];
    }

    cell.textLabel.text = @"张三";
    cell.textLabel.textColor = [UIColor redColor];
    cell.detailTextLabel.text = @"张三是张家人";
    cell.imageView.image = [UIImage imageNamed:@"zhangsan.png"];

    //右边出现小箭头
    cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;

    return cell;

}

tableView的其他代理方法

//设置每行的高- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{

    return 100;
}
//给tablView设置段数/* *  tableView如果不实现这个代理方法,默认是一段,所以之前是给第一段设置行数,通过这个方法能够给tableView设置多段 */
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{

    return 3;
}

PS:之前讲UITableView时说到有两种风格,现在切换到grouped模式运行看效果,是不是有分组的意思。

总结:这次只是简单的讲了一些UITableView基本使用,和一些简单的代理方法。后面会继续深入带领大家学习。

时间: 2024-08-10 14:56:38

UI-9-UITableView的相关文章

iOS开发UI篇—UITableview控件基本使

iOS开发UI篇—UITableview控件基本使用 一.一个简单的英雄展示程序 NJHero.h文件代码(字典转模型) #import <Foundation/Foundation.h> @interface NJHero : NSObject /** * 头像 */ @property (nonatomic, copy) NSString *icon; /** * 名称 */ @property (nonatomic, copy) NSString *name; /** * 描述 */ @

iOS开发UI篇—UITableview控件使用小结

iOS开发UI篇—UITableview控件使用小结 一.UITableview的使用步骤 UITableview的使用就只有简单的三个步骤: 1.告诉一共有多少组数据 方法:- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView; 2.告诉每组一共有多少行 方法:- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSIntege

学习IOS开发UI篇--UITableView/数据模型嵌套/UITableViewCell/Cell的重用

1.UITableView ================================================== UITableView有两种格式:group和plain 2.UITableView如何展示数据 ================================================== UITableView需要一个数据源(dataSource)来显示数据 凡是遵守UITableViewDataSource协议的OC对象,都可以是UITableView的

IOS开发UI篇--UITableView的自定义布局==xib布局

利用Xib进行实现 应用场景:像团购网站的列表数据显示,新闻列表显示等(由于该类的显示的数据单元格内容格式相同) (1)主控制器文件,在文件中实现了自己自定义的代理,加载数据, 1 #import "SLViewController.h" 2 #import "SLTgDatas.h" 3 #import "SLTableViewCell.h" 4 #import "SLFooterView.h" 5 #import &quo

IOS开发UI篇--UITableView的自定义布局==纯代码布局

UITableView中除了利用系统的UItableViewCell不能完成需求进行布局时,还可以进行自定义布局: 自定义布局分为两类:(1)利用代码进行创建 (2)利用xib进行实现: 下面对利用代码进行创建分析: 应用场景:像微博,等列表数据展示(由于微博的每个单元格的数据大小不一致,所以得计算每个单元格的大小) 分析:前提是获取列表数据,然后建立每个单元格的模型(建立单元格模型应继承UITableViewCell)复写 - (id)initWithStyle:(UITableViewCel

iOS开发UI篇—UITableView全面解析

概述 在iOS开发中UITableView可以说是使用最广泛的控件,我们平时使用的软件中到处都可以看到它的影子,类似于微信.QQ.新浪微博等软件基本上随处都是UITableView.当然它的广泛使用自然离不开它强大的功能,今天这篇文章将针对UITableView重点展开讨论.今天的主要内容包括: 基本介绍 数据源 代理 性能优化 UITableViewCell 常用操作 UITableViewController MVC模式 基本介绍 UITableView有两种风格:UITableViewSt

iOS开发UI篇—UITableview控件基本使用

一.一个简单的英雄展示程序 NJHero.h文件代码(字典转模型) 1 #import <Foundation/Foundation.h> 2 3 @interface NJHero : NSObject 4 /** 5 * 头像 6 */ 7 @property (nonatomic, copy) NSString *icon; 8 /** 9 * 名称 10 */ 11 @property (nonatomic, copy) NSString *name; 12 /** 13 * 描述 1

iOS开发UI篇—UITableview控件简单介绍

一.基本介绍 在众多移动应?用中,能看到各式各样的表格数据 . 在iOS中,要实现表格数据展示,最常用的做法就是使用UITableView,UITableView继承自UIScrollView,因此支持垂直滚动,?且性能极佳 . UITableview有分组和不分组两种样式,可以在storyboard或者是用代码设置. 二.数据展示 UITableView需要?一个数据源(dataSource)来显示数据UITableView会向数据源查询一共有多少行数据以及每?行显示什么数据等 没有设置数据源

UI之UItableView重用机制的性能问题

*:first-child { margin-top: 0 !important; } body > *:last-child { margin-bottom: 0 !important; } a { color: #4183C4; } a.absent { color: #cc0000; } a.anchor { display: block; padding-left: 30px; margin-left: -30px; cursor: pointer; position: absolute

UI基础--UITableView,UITableViewDataSource,UITableViewDelegate的一些属性和方法

UITableView(继承自UIScrollView)的常用属性: 1.可以用连线的方式设置数据源和代理 1 self.tableView.dataSource = self; 2 self.tableView.delegate = self; 2.设置高度 1 @property (nonatomic) CGFloat rowHeight;//行高 2 @property (nonatomic) CGFloat sectionHeaderHeight; //组头的高度 3 @property