iOS UITableView 与 UITableViewController实例详解

首先我们需要明确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日

相关文章

  • docker启动失败日志

    Docker启动失败时,可以查看Docker的日志来了解问题的原因。以下是详细的攻略: 查看Docker日志 在Docker启动失败后,可以使用以下命令查看Docker的日志: bash sudo journalctl -u docker.service 这将显示Docker的日志,包括启动失败的原因。 查看Docker容器日志 如果Docker容器启动失败…

    other 2023年5月7日
    00
  • 虾米音乐app怎么自定义随心听卡片类型?

    让我详细地讲解一下“虾米音乐app怎么自定义随心听卡片类型”的完整攻略: 步骤一:进入“随心听” 首先,在虾米音乐app的首页下方找到“随心听”选项,点击进入。 步骤二:点击“+”添加卡片 在随心听页面中,点击右上角的“+”号,就可以添加自己喜欢的卡片类型了。 步骤三:选择自定义卡片 在弹出的卡片类型列表中,选择“自定义卡片”即可。 步骤四:编辑卡片内容 编…

    other 2023年6月25日
    00
  • 微信小程序字体设置

    微信小程序字体设置 微信小程序中,字体是页面重要的显示元素之一。良好的字体设置能够提升用户的阅读体验和页面美观度。本文将介绍微信小程序的字体设置方法和注意事项。 1. 基本设置 微信小程序提供了一套基本的字体系列和大小样式,可以通过CSS属性进行设置。 1.1 字体系列 微信小程序提供了以下字体系列: 苹方字体:”PingFang SC”, “Helveti…

    其他 2023年3月28日
    00
  • win10英雄联盟图形设备初始化失败如何解决?

    当玩家在使用Windows 10操作系统时,在运行英雄联盟游戏时可能会遇到“图形设备初始化失败”的问题。这个问题通常出现在电脑的显卡驱动程序上。以下是解决这个问题的攻略: 步骤一:检查显卡驱动程序是否安装或过期 如果你碰到了“图形设备初始化失败”的问题,首先要检查显卡驱动程序是否安装或已过期。以下是解决这个问题的步骤: 按下Windows键+R来打开运行窗口…

    other 2023年6月20日
    00
  • asp 性能测试报告 学习asp朋友需要了解的东西

    以下是对ASP性能测试报告的详细攻略: 准备工作 安装性能测试工具,如Apache JMeter或LoadRunner。 配置测试环境,包括服务器、数据库和网络设置。 设计性能测试场景 确定测试目标,例如测试网站的并发用户数、响应时间和吞吐量。 创建测试计划,包括测试场景、用户行为和数据负载。 配置性能测试工具,设置并发用户数、请求频率和持续时间。 执行性能…

    other 2023年10月18日
    00
  • 关于sqlserver:如何在sql中自动生成唯一id

    以下是关于“如何在SQL Server中自动生成唯一ID”的完整攻略,包含两个示例。 背景 在SQL Server中,我们经常需要为表中的每个记录生成唯一的ID。这个ID可以用作主键或其他用途。在SQL Server中,我们可以使用IDENTITY列或GUID列来生成唯一ID。 使用IDENTITY列生成唯一ID IDENTITY列是SQL Server中一…

    other 2023年5月9日
    00
  • adobephotoshopcc2019formac(介绍及下载)

    Adobe Photoshop CC 2019 for Mac (介绍及下载) Adobe Photoshop CC 2019 for Mac是一款被广泛应用于美工设计和数字艺术领域的图像处理软件。该软件的核心功能是图像处理和编辑,支持大量的滤镜和特效。Adobe Photoshop CC 2019 for Mac整合了多种图像处理工具和功能,并且使用方便,…

    其他 2023年3月28日
    00
  • JS疑惑的数据类型及类型判断方法详解

    JS疑惑的数据类型及类型判断方法详解 在JavaScript中,存在一些疑惑的数据类型以及类型判断方法,本篇文章将对这些问题进行详细的讲解,并提供相关的示例说明,帮助读者更好地理解。 JS数据类型 JavaScript中共有七种数据类型: Number:数字类型,包括整数和浮点数。 String:字符串类型。 Boolean:布尔类型,只有true和fals…

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