In this tutorial, we’ll be developing an iOS Application using Swift that has a single iOS UITableView with two different types of UITableViewCells.

在本教程中,我们将使用Swift开发一个iOS应用程序,该应用程序具有一个iOS UITableView和两个不同类型的UITableViewCells

Multiple Cells inside a TableView are commonly seen in the Facebook newsfeed application which hosts cells largely grouped into three types – Status Posts, Image Posts and Video Posts.

TableView内部的多个单元格通常在Facebook新闻订阅源应用程序中看到,该应用程序托管的单元格大致分为三种类型-状态帖子,图像帖子和视频帖子。

iOS UITableView具有不同类型的单元格 (iOS UITableView with different type of cells)

We’ll be creating an application that displays two types of cells. First, that displays country flag and name. Second, that displays population for the country. We’ll be populating the data in the UITableView from a generic array.

我们将创建一个显示两种类型的单元格的应用程序。 首先,显示国家标志和名称。 其次,显示该国人口。 我们将从通用数组填充UITableView中的数据。

Let’s start our XCode and select a Single View Application template. We’ll setup our UI in Main.storyboard using Autolayout as shown below.

让我们启动XCode并选择Single View Application模板。 我们将使用Autolayout在Main.storyboard设置UI,如下所示。

  1. Adding a TableView and setting up its constraints. Displaying two prototype cells.添加一个TableView并设置其约束。 显示两个原型单元。
  2. Adding a UIImageView and Label in the first prototype cell followed by setting their constraints.在第一个原型单元中添加UIImageView和Label,然后设置它们的约束。
  3. Setting up the second prototype cell followed by defining the identifiers and class name for each UITableViewCell.设置第二个原型单元,然后为每个UITableViewCell定义标识符和类名称。
  4. Adding the references of the views in the ViewController and Custom Cells.在ViewController和“自定义单元格”中添加视图的引用。

The code for the ViewController.swift file is given below.

下面给出了ViewController.swift文件的代码。

import UIKitclass CustomCountryCell: UITableViewCell{@IBOutlet var countryName: UILabel!@IBOutlet var countryIcon: UIImageView!
}class CustomPopulationCell: UITableViewCell{@IBOutlet var countryPopulation: UILabel!}class ViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {@IBOutlet var tableView: UITableView!var tableData = ["Australia", 24.13, "Canada", 36.29 ,"China", 1379, "India", 1324, "United States of America", 323.1] as [Any]override func viewDidLoad() {super.viewDidLoad()// Do any additional setup after loading the view, typically from a nib.tableView.dataSource = selftableView.delegate = selftableView.tableFooterView = UIView()tableView.rowHeight = 60}override func didReceiveMemoryWarning() {super.didReceiveMemoryWarning()// Dispose of any resources that can be recreated.}func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {if let string = self.tableData[indexPath.row] as? String{let cell:CustomCountryCell = self.tableView.dequeueReusableCell(withIdentifier: "customCountryCell") as! CustomCountryCellcell.countryName?.text = stringcell.countryIcon?.image = UIImage(named:string)return cell}else if let population = self.tableData[indexPath.row] as? Any, population is Double || population is Int {let cell:CustomPopulationCell = self.tableView.dequeueReusableCell(withIdentifier: "customPopulationCell") as! CustomPopulationCellcell.countryPopulation?.text = "Population is \(population) million"return cell}return UITableViewCell()}func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {return tableData.count}func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {tableView.deselectRow(at: indexPath, animated: true)}}

Following are the notable things present in the ViewController class.

以下是ViewController类中存在的值得注意的事情。

  • We’ve implemented the two protocols present in the UITableView class i.e. UITableViewDelegate and UITableViewDataSource我们已经实现了UITableView类中存在的两个协议,即UITableViewDelegateUITableViewDataSource
  • .

  • tableData is a generic array that holds String, Double and Integer types. The string element is used to set the Image(the image assets have the same name set in the Assets folder) and country name.tableData是一个通用数组,其中包含String,Double和Integer类型。 字符串元素用于设置图像(图像资产具有在“资产”文件夹中设置的相同名称)和国家/地区名称。
  • tableView.tableFooterView = UIView() removes the empty cells after the last populated row in the UITableView.tableView.tableFooterView = UIView()删除UITableView中最后一个填充行之后的空单元格。
  • tableView.rowHeight = 60 sets the row height for each UITableViewCell.tableView.rowHeight = 60设置每个UITableViewCell的行高。
  • Cell of the type CustomCountryCell is added in the UITableView if the current element in the tableData is a String.如果tableData的当前元素是String,则在UITableView中添加CustomCountryCell类型的单元格。
  • To check whether the current element in the tableData is of the type Double or Integer the following condition is used :
    if let population = self.tableData[indexPath.row] as? Any, population is Double || population is Int {
    }

    In the above code snippet, the , acts as a where clause. The condition reads as : “if self.tableData[indexPath.row] is a valid element, typecast it to Any and check whether it is of the type Double OR type Int”.

    Note: The above condition can be written in following way too.

    要检查tableData的当前元素是Double类型还是Integer类型,请使用以下条件:

    if let population = self.tableData[indexPath.row] as? Any, population is Double || population is Int {
    }

    在以上代码片段中,充当where子句。 条件显示为:“如果self.tableData[indexPath.row]是有效元素,则将其类型转换为Any并检查其类型是否为Double OR type Int”。

    注意 :以上条件也可以用以下方式编写。

  • return UITableViewCell() is used to add a default empty cell if none of the conditions match.如果没有条件匹配,则return UITableViewCell()用于添加默认的空单元格。
  • didSelectRowAt function is used to add the click animation on each TableView row.didSelectRowAt函数用于在每个TableView行上添加单击动画。

The output of the above application in action is given below.

下面给出了上面应用程序的输出。

This brings an end to this tutorial. A similar implementation for Android using RecyclerView is given here. You can download the iOS TableViewMultipleCellTypes Project from the link below.

本教程到此结束。 这里给出了使用RecyclerView的Android类似实现。 您可以从下面的链接下载iOS TableViewMultipleCellTypes项目

Download iOS TableView Multiple Cells Project下载iOS TableView多个单元格项目

Reference: Apple Documentation

参考: Apple文档

翻译自: https://www.journaldev.com/15077/ios-uitableview-multiple-cell-types

具有多个单元格类型的iOS UITableView相关推荐

  1. java excel 单元格类型,POI Excel 单元格内容类型判断并取值

    个人用到的 String birthdayVal = null; switch (cell_2.getCellTypeEnum()) { case STRING: birthdayVal = cell ...

  2. vue xlsx.js获取单元格数据类型的hooks 单元格类型处理

    xlsx的单元格修改,获取当前单元格的数据类型 xlsx.js的单元格修改,获取当前单元格的数据类型 找遍全网没有找到xlsx.js,本地文件读取时获取当前单元格的类型,对特殊类型单元格进行修改的格式 ...

  3. Excel中单元格格式对应POI 单元格类型 CellType

    CellType 常量名 类型 值(int) CELL_TYPE_NUMERIC 数值型 0 CELL_TYPE_STRING 字符串型 1 CELL_TYPE_FORMULA 公式型 2 CELL_ ...

  4. php excel 单元格类型,设置20个单元格类型后,PhpExcel停止工作

    PHPExcel分配了相当多的内存 虽然PHPExcel是一个漂亮的库,但使用它可能需要为PHP分配大量内存. 根据this thread,只有5个单元可以渲染6 MB的内存使用量: require_ ...

  5. iPhone UITableViewCell 表格单元格类型

    iPhone提供了4种基本的表格视图单元格,在SDK 3.0 之后,每个单元格都有3个属性textLabel,detailTextLabel和imageView. 下面一一介绍这4种基本格式: 1.U ...

  6. iOS开发那些事--自定义单元格实现

    自定义单元格 当苹果公司提供给的单元格样式不能我们的业务需求的时候,我们需要自定义单元格.在iOS 5之前,自定义单元格可以有两种实现方式:代码实现和用xib技术实现.用xib技术实现相对比较简单,创 ...

  7. iOS:删除、插入、移动单元格

    删除.插入.移动单元格的具体实例如下:   代码如下: 1 #import "ViewController.h"2 #define NUM 203 typedef enum4 {5 ...

  8. java excel单元格数据格式_POI实战-java开发excel详解之单元格各类型数据读取

    2.复杂读取 2.1 单元格各类型数据读取 2.1.1 基本类型 在实际工作中,我们处理的Excel数据都不止限于字符型数据,更多的是数字.日期.甚至公式等. 下面是单元格类型说明: 类型 CELL_ ...

  9. POI 读写EXCEL日期类型单元格

    1.读日期单元格: Cell cell = row.getCell(col); // 先判断单元格类型为数字 if (cell.getCellType()==Cell.CELL_TYPE_NUMERI ...

最新文章

  1. matlab 二分法求方程近似解
  2. 主从复制跳过错误(未采用GTID)
  3. 区块链BaaS云服务(17)纸贵科技Z-BaaS零知识证明
  4. python 动态_python实现动态创建类的方法分析
  5. F5与Ctrl+F5及地址栏输入地址回车
  6. QT之QHash简介
  7. 使用阿里巴巴json映射_使用JSON模式验证来映射稀疏JSON
  8. linux vi 出现下划线,Vim高亮当前行(显示为下划线)的解决方案
  9. 樊登36个问题建立亲密关系_心理学家亚瑟·阿伦的36个问题-樊登读书
  10. sql server分页_SQL Server中的分页简介
  11. C# RSA和Java RSA互通
  12. 测试管理中的一个问题—功能点覆盖还是功能测试点覆盖
  13. IE iframe不刷新的问题之完美解决
  14. monkey4444勒索病毒解密方法方案成功处理复旦安全实验室
  15. DMX512协议及对接口电路的分析
  16. 9点EXCEL计算公式
  17. Python:猴子分香蕉
  18. 前端项目怎样合理使用模块化和闭包?
  19. idea配置有道翻译引擎
  20. 《弗洛伊德及其后继者》学习笔记(part2)--自我心理学

热门文章

  1. Ruby笔记三(类、对象、属性)
  2. [转载] 机器学习 scikit-learn1 预测贷款用户是否会逾期
  3. 二维平面上判断点是否在三角形内
  4. Windows下基于python3使用word2vec训练中文维基百科语料(三)
  5. SparkRDD内核
  6. 数据加载中,请稍等......
  7. jQuery 异步和同步请求
  8. 2016 Bird Cup ICPC7th@ahstu--“波导杯”安徽科技学院第七届程序设计大赛
  9. 【Mac】Mac键盘实现Home, End, Page UP, Page DOWN
  10. PermGen space 与 Java heap space