iOS UITableView 与 UITableViewController实例详解

yizhihongxing

首先我们需要明确UITableView和UITableViewController的概念。

UITableView是iOS中的一个视图控件,是显示列表数据的主要视图组件,通过UITableView可以方便的展示和管理大量的数据。

UITableViewController则是UIKit库中特定的视图控制器,主要作用是管理UITableView视图。UITableViewController提供了内存管理、数据源管理、滚动管理、编辑、行选中、重用单元格等常用功能,为处理UITableView提供了便利。

下面我们开始介绍iOS UITableView 与 UITableViewController实例详解,并附带两个示例。

一、UITableView实例详解

UITableView的使用流程一般为:创建UITableView→配置UITableView→实现数据源代理和UITableViewDelegate。下面简单介绍以下UITableView的实例详解。

1.创建UITableView

在UIViewController中新建一个UITableView对象,可以通过如下代码:

UITableView *tableView = [[UITableView alloc] initWithFrame:self.view.bounds style:UITableViewStylePlain];

其中参数style为UITableView的样式,包括Plain和Grouped两种。

2.配置UITableView

UITableView最为常见的配置以及一些样式设置如下:

2.1 设置数据源和代理

UITableView必须实现UITableViewDataSource和UITableViewDelegate协议。

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

设置UITableView的代理和数据源协议,其中delegate和datasource分别是UITableView的两个协议。

2.2 设置表格分割线

  • 隐藏分割线:
tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
  • 自定义分割线:
tableView.separatorColor = [UIColor redColor];//设置分割线的颜色
tableView.separatorStyle = UITableViewCellSeparatorStyleSingleLine;//设置分割线样式

2.3 设置表头和表尾

tableView.tableHeaderView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 100)];

上面代码中tableView的tableHeaderView为UIView类型,通过UIView来实现我们自定义的表头。

tableView.tableFooterView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 50)];

上面代码中tableView的tableFooterView为UIView类型,通过UIView来实现我们自定义的表尾。

2.4 设置单元格部分属性

  • 设置单元格高度:
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 44;
}
  • 设置背景颜色:
cell.backgroundColor = [UIColor whiteColor];
  • 处理选中:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    NSLog(@"选中了第%ld行", (long)indexPath.row);
}
  • 设置滚动条颜色:
tableView.indicatorStyle = UIScrollViewIndicatorStyleDefault;

3.实现数据源代理和UITableViewDelegate

UITableView的数据源代理委托类主要有以下几个方法:

  • numberOfRowsInSection:返回对应的section有多少行。
  • cellForRowAtindexPath:返回每行的单元格。
  • numberOfSections:返回表格中有多少个section。
  • titleForHeaderInSection:返回每个section的标题。
  • viewForHeaderInSection:返回每个section的头部视图。
  • viewForFooterInSection:返回每个section的尾部视图。

UITableViewDelegate委托类主要有以下几个方法:

  • heightForRowAtIndexPath:每一行的高度。
  • heightForFooterInSection:
  • heightForHeaderInSection:
  • willSelectRowAtIndexPath:
  • didSelectRowAtIndexPath:选中该行的时候调用。
  • willDeselectRowAtIndexPath:
  • didDeselectRowAtIndexPath:取消选中该行的时候调用。
  • willDisplayCell:在cell即将显示的时候调用。

示例一

下面是一个简单的UITableView示例,用于展示数据:

@interface ViewController ()<UITableViewDataSource, UITableViewDelegate>
@property (nonatomic, strong) UITableView *tableView;
@property (nonatomic, strong) NSArray *dataSource;
@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    self.navigationItem.title = @"UITableView";
    [self.view addSubview:self.tableView];
}

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
}

#pragma mark - UITableViewDataSource

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.dataSource.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    static NSString *cellID = @"cellID";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellID];
    if (!cell) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellID];
    }
    cell.textLabel.text = self.dataSource[indexPath.row];
    return cell;
}

#pragma mark - UITableViewDelegate

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 44;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    NSLog(@"选中了第%ld行", (long)indexPath.row);
}

#pragma mark - lazy

- (UITableView *)tableView {
    if (!_tableView) {
        _tableView = [[UITableView alloc] initWithFrame:self.view.bounds style:UITableViewStylePlain];
        _tableView.delegate = self;
        _tableView.dataSource = self;
    }
    return _tableView;
}

- (NSArray *)dataSource {
    if (!_dataSource) {
        _dataSource = @[@"张三", @"李四", @"王五", @"赵六", @"钱七"];
    }
    return _dataSource;
}

@end

二、UITableViewController实例详解

UITableViewController的使用流程一般为:继承UITableViewController父类→设置UITableView的状态并实现数据源代理和UITableViewDelegate。下面简单介绍以下UITableViewController的实例详解。

1.继承UITableViewControler父类

通过继承UITableViewController,自带了一个tableview,并且重写了UITableViewDataSource和UITableViewDelegate两个协议方法。

@interface TableViewController : UITableViewController
@end

@implementation TableViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationItem.title = @"UITableViewController";
    [self.tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"cellID"];
}

#pragma mark - UITableViewDataSource

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return 100;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cellID"];
    cell.textLabel.text = [NSString stringWithFormat:@"这是第%ld行", (long)indexPath.row];
    return cell;
}

#pragma mark - UITableViewDelegate

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
}

@end

2.设置UITableView的状态并实现数据源代理和UITableViewDelegate

这时,需要设置tableview的属性,具体如下:

@interface TableViewController : UITableViewController
@end

@implementation TableViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationItem.title = @"UITableViewController";
    // 不显示分割线和滚动条
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    self.tableView.showsVerticalScrollIndicator = NO;

    // 设置表格区头、区尾和行高
    self.tableView.estimatedRowHeight = 80;
    self.tableView.rowHeight = UITableViewAutomaticDimension;
    self.tableView.sectionHeaderHeight = 0.1;
    self.tableView.sectionFooterHeight = 10.0;

    [self.tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"cellID"];
}

#pragma mark - UITableViewDataSource

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return 100;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cellID"];
    cell.textLabel.text = [NSString stringWithFormat:@"这是第%ld行", (long)indexPath.row];
    return cell;
}

#pragma mark - UITableViewDelegate

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
}

@end

以上就是对iOS UITableView 与 UITableViewController实例详解的详细讲解,希望对您有所帮助。

本站文章如无特殊说明,均为本站原创,如若转载,请注明出处:iOS UITableView 与 UITableViewController实例详解 - Python技术站

(0)
上一篇 2023年6月26日
下一篇 2023年6月26日

相关文章

  • python 实验3 循环结构

    Python 实验3 循环结构 循环结构是编程中非常重要和常用的一种语句形式,目的是帮助我们重复执行某些操作。Python 提供两种循环结构:for 循环和 while 循环。本实验将介绍这两种循环结构,并通过一些例子来讲解循环结构的使用方法。 for 循环 for 循环用来遍历一个可迭代对象中的所有元素,其语法格式如下: for 变量 in 可迭代对象: …

    其他 2023年3月28日
    00
  • 苹果iOS8.1 beta今凌晨向开发者开放固件下载(附固件下载地址)

    苹果iOS8.1 beta今凌晨向开发者开放固件下载攻略 今天早上,苹果公司发布了 iOS 8.1 beta 版本,并向开发者开放了下载。本文将为大家介绍如何下载和安装 iOS 8.1 beta 版本,希望对大家有所帮助。 1. 检查设备是否支持 在下载 iOS 8.1 beta 版本之前,我们需要先检查设备是否支持。iOS 8.1 beta 支持 iPho…

    other 2023年6月26日
    00
  • 微信小程序 Tab页切换更新数据

    productList: [], cartData: [] }, updateCartData: function() { // 更新购物车数据的逻辑 // … }, onShow: function() { this.updateCartData(); // 更新购物车数据 // … }, // …})“` 在这个示例中,我们在onShow函…

    other 2023年7月29日
    00
  • C语言链表与单链表详解

    C语言链表与单链表详解 什么是链表 链表是由一系列节点组成的线性结构,每个节点由两个部分组成:数据域和指针域。数据域用来存储节点的数据,指针域用来指向下一个节点的地址,也就是说每个节点保存了下一个节点的地址信息。由此构成的链式结构被称为链表。 链表相对于数组来说,其大小可以动态调整,插入和删除元素操作更加高效。 单链表 单链表是链表的一种,每个节点中只包含一…

    other 2023年6月27日
    00
  • 微软工具ilmerge

    微软工具ilmerge ilmerge是由微软提供的一个命令行工具,可以把多个.NET程序集合并成一个程序集。 安装和使用 ilmerge可以从NuGet中获取,也可以从官方网站下载。 安装好ilmerge后,打开命令行工具,切换到包含程序集文件的目录中,使用以下命令即可将多个程序集合并成一个程序集: ilmerge /out:Merged.dll Asse…

    其他 2023年3月29日
    00
  • springboot项目中实现访问druid内置监控页面

    以下是在springboot项目中实现访问druid内置监控页面的完整攻略: 1. 添加druid依赖 在pom.xml文件中添加druid依赖: <dependency> <groupId>com.alibaba</groupId> <artifactId>druid-spring-boot-starter&…

    other 2023年6月27日
    00
  • Vscode Remote Development远程开发调试的实现思路

    下面我会详细讲解 “Vscode Remote Development 远程开发调试的实现思路” 的完整攻略。 1. 什么是 Vscode Remote Development? Vscode Remote Development 是 Visual Studio Code 扩展的一种能力。它使用 SSH 或容器来在远程机器或容器中开发代码,在本地 VS Co…

    other 2023年6月27日
    00
  • python实现用户名密码校验

    对于如何使用Python实现用户名密码校验,这里提供一些具体的攻略和示例: 1. 必备条件 在实现用户名密码校验之前,需要确保已经安装了Python,同时还需要了解如何读取输入信息和进行基础的字符串操作。 2. 核心思路 Python实现用户名密码校验的核心思路是:读取用户输入的用户名和密码,进行判断和检验,然后输出校验结果。 具体步骤如下: 读取用户输入的…

    other 2023年6月27日
    00
合作推广
合作推广
分享本页
返回顶部