表格视图UITableView

(一)UITableView内部自动封装了一套复用机制。会让空闲的cell进入可重用线程池,当有新的cell出现会先去线程池中找有没有可复用的,没有才会创建。假如有100组数据,需要100个cell,但是手机上每屏只能放下10个,其实这时候只需创建11个cell就够用了。每一个数据模型就是一个cell。通过数据源方法来对每个cell进行数据设置。通过代理方法设置关于tableView的头,尾等视图设置。

(二)tableView有两种样式:UITableViewStylePlain和UITableViewStyleGroup。一个是正常的,一个是分组的。

正常样式 和 分组样式

代码:

import UIKit

class ViewController: UITableViewController {

    override func viewDidLoad() {
        super.viewDidLoad()
        tableView.registerClass(UITableViewCell.self, forCellReuseIdentifier: CELLID)
    }

    override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {

        if section == 0 {
            return 3
        } else if section == 1 {
            return 4
        }else {
            return 5
        }
    }

    override func numberOfSectionsInTableView(tableView: UITableView) -> Int {
        return 3
    }

    private let CELLID = "CELLID"

    override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {

        let cell = tableView.dequeueReusableCellWithIdentifier(CELLID, forIndexPath: indexPath)

        cell.textLabel?.text = "第\(indexPath.section)组第\(indexPath.row)行"

        return cell

    }

}

一个UITableView可以有多种不同类型的cell,可以通过cell的id来进行区分。

(三)系统自带的cell

typedef NS_ENUM(NSInteger, UITableViewCellStyle) {
    UITableViewCellStyleDefault,    // Simple cell with text label and optional image view (behavior of UITableViewCell in iPhoneOS 2.x)
    UITableViewCellStyleValue1,        // Left aligned label on left and right aligned label on right with blue text (Used in Settings)
    UITableViewCellStyleValue2,        // Right aligned label on left with blue text and left aligned label on right (Used in Phone/Contacts)
    UITableViewCellStyleSubtitle    // Left aligned label on top and left aligned label on bottom with gray text (Used in iPod).
}; 

其中包含主标签,副标签,图片和accessoryType按钮。

(四)设置UITableView的行高和头尾视图:初级的行高设置是在代理中完成了,那些复杂的高度不一的cell则需要另外设置。

    // cell行高设置
    override func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {

        return 44
    }

    // footerView高度设置
    override func tableView(tableView: UITableView, heightForFooterInSection section: Int) -> CGFloat {
        return 50
    }

    // headerView高度设置
    override func tableView(tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
        return 50
    }

    // 设置尾视图
    override func tableView(tableView: UITableView, viewForFooterInSection section: Int) -> UIView? {
        let frame = CGRect(x: 0, y: 0, width: view.frame.width, height: 50)
        let v = UIView(frame: frame)
        v.backgroundColor = UIColor.yellowColor()
        return v

    }

    // 设置头视图
    override func tableView(tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
        let frame = CGRect(x: 0, y: 0, width: view.frame.width, height: 50)
        let v = UIView(frame: frame)
        v.backgroundColor = UIColor.orangeColor()
        return v
    }

效果图:

(五)tableView的用户交互:

(1)当选中某行时会调用代理:

// 选中某航时调用
    override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {

    }

(2)对cell进行增/删/移动操作会调用代理:

首先让tableView进入可编辑模式

tableView.editing = true
// 设置cell的编辑模式,有NONE,插入,删除三种
    override func tableView(tableView: UITableView, editingStyleForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCellEditingStyle {

        return UITableViewCellEditingStyle.Insert

    }

此时就会出现编辑选项

(3)如果需要UITableViewCell支持移动,需要实现如下两个代理方法

    // 是否能够移动
    override func tableView(tableView: UITableView, canMoveRowAtIndexPath indexPath: NSIndexPath) -> Bool {

        return true
    }

    // 位置移动后回调的方法
    override func tableView(tableView: UITableView, moveRowAtIndexPath sourceIndexPath: NSIndexPath, toIndexPath destinationIndexPath: NSIndexPath) {

        // 改变模型设置
    }

效果:

此时长按??选项,就可以移动了。

(4)如需要删除某个cell。一来可以滑动删除,二来可以在上方所述的枚举中选的删除枚举并实现代理

代码:

    // 设置删除按钮的标题
    override func tableView(tableView: UITableView, titleForDeleteConfirmationButtonForRowAtIndexPath indexPath: NSIndexPath) -> String? {
        return "删除"
    }

    // 删除/插入单击事件
    override func tableView(tableView: UITableView, commitEditingStyle editingStyle: UITableViewCellEditingStyle, forRowAtIndexPath indexPath: NSIndexPath) {
        // DOSOMETHING
        print("DOSOMETHING")
    }

效果:

(5)如果需要滑动删除,比较特殊,此时应该关闭tablview的交互并且实现代理

tableView.editing = false
    // 是否能够滑动
    override func tableView(tableView: UITableView, canEditRowAtIndexPath indexPath: NSIndexPath) -> Bool {
        return true
    }

    // 设置cell的编辑模式,有NONE,插入,删除三种
    override func tableView(tableView: UITableView, editingStyleForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCellEditingStyle {

        return UITableViewCellEditingStyle.Delete

    }

    // 删除/插入单击事件
    override func tableView(tableView: UITableView, commitEditingStyle editingStyle: UITableViewCellEditingStyle, forRowAtIndexPath indexPath: NSIndexPath) {
        // DOSOMETHING
        print("DOSOMETHING")

    }

(6)为UITableView添加索引:类似于通讯录。右边会有一栏索引功能。当选择索引就会跳转到相应的区域,需要实现如下一个代理方法。

    // 索引功能
    override func sectionIndexTitlesForTableView(tableView: UITableView) -> [String]? {

        return ["A","B","C","D","E","F"]
    }

效果如下:

当选择A,就会去第一组,B就回去第二组。 这可是做查询必不可少的一部哦。

时间: 2024-08-03 08:04:38

表格视图UITableView的相关文章

iOS:带主标题、副标题、图像类型的表格视图UITableView

制作一个通讯录,包括姓名.电话.头像,将表格视图类型设置为UITableViewCellStyleSubtitle 效果图: //创建一个联系人的类,初始化数据 在视图控制器中实现表格内容的显示 1 #import "ViewController.h" 2 #import "Contact.h" 3 #define NUM 20 4 5 @interface ViewController ()<UITableViewDataSource,UITableView

iOS:分组的表格视图UITableView,可以折叠和展开

虽然表格视图可以分组,但是如果分组后,每一行的内容太多,往后翻看起来比较的麻烦.为了解决这个麻烦,可以将分组的行折叠和展开.折叠时,行内容就会隐藏起来:展开时,行内容就会显示出来. 折叠时: 展开后:       具体的代码如下: 1 #import "ViewController.h" 2 3 @interface ViewController ()<UITableViewDataSource,UITableViewDelegate> 4 @property (weak,

iOS:UITableView表格视图控件

UITableView:表格视图控件,继承滚动视图控件UIScrollView,(类似于UIPickerView选择器,它主要通过设置数据源代理和行为代理实现协议来设置单元格) 对表格的操作主要有:创建表格.设置单元格(行数.内容.行高).编辑单元格(删除单元格.插入单元格).移动单元格.标记单元格.修改单元格等. 一.表格式图的属性和行为: 1.基本属性: @interface UITableView : UIScrollView <NSCoding> @property (nonatomi

IOS UITableView表格视图详解

IOS 表格视图类UITableView 实现的协议:UITableViewDataSource,UITableViewDelegate 必须实现下面的3个方法: - (NSInteger)numberOfSectionsInTableView:(UITableView*)tableView { return 1; } - (NSInteger)tableView:(UITableView*)tableView numberOfRowsInSection:(NSInteger)section {

Objective-C:UITableView表格视图控件

UITableView:表格视图控件,继承滚动视图控件UIScrollView,(类似于UIPickerView选择器,它主要通过设置数据源代理和行为代理实现协议来设置单元格) 对表格的操作主要有:创建表格.设置单元格(行数.内容.行高).编辑单元格(删除单元格.插入单元格).移动单元格.标记单元格.修改单元格等. 一.表格式图的属性和行为: 1.基本属性: @interface UITableView : UIScrollView <NSCoding> @property (nonatomi

表格视图 - 使用代码自定义行高度

1. 实现UITableViewDelegate协议 @interface ViewController () <UITableViewDelegate> 2. 将表格视图的代理属性指向其父容器视图 self.myTableView.delegate = self; 3. 实现协议对应的方法 tableView:heightForRowAtIndexPath: 完整代码(ViewController.m): #import "ViewController.h" @inter

IOS开发之表视图(UITableView)

IOS开发之表视图(UITableView)的基本介绍(一) (一):UITableView的基本概念 1.在IOS开发中,表视图的应用十分广泛和普及.因此掌握表视图的用法显得非常重要.一般情况下对于数据的展示 我们都会选择表视图,比如通讯录和一些数据列表. 2.我们可以选择创建表视图也可以创建表视图控制器. (二)UITableView基本样式如下(1:UITableViewStylePlain(普通表视图),2:UITableViewStyleGroup(分组表视图)): (三)UITabl

137在搜索框中实现下拉列表效果(扩展知识:表格视图数据源为空数据时显示提示信息)

效果如下: ViewController.h 1 #import <UIKit/UIKit.h> 2 #import "DropDownListViewController.h" 3 4 @interface ViewController : UITableViewController<UISearchBarDelegate, PassValueDelegate> 5 @property (strong, nonatomic) UISearchBar *sear

【iOS7的一些总结】9、用列表显示内容(上):列表视图UITableView

列表视图,顾名思义就是将数据的内容用列表的形式显示在屏幕上的视图.在ios中列表视图以UITableView实现,这个类在实际应用中非常的频繁,但是对于初学者来说不是非常容易理解.这里将UITableView的主要用法总结一下以备查. UITableView定义在头文件UITableView.h中,具体的定义可以查看官方文档:从定义中可以看出,UITableView继承自UIScrollView类,因此在支持方便地显示列表数据的同时,还天生支持垂直滚动操作.组成列表的每一个元素称为UITable