ios cell点击对勾

In this tutorial, we’ll be developing an iOS Application that contains a custom TableView with cells having a custom layout inclusive of images, label and a checkmark. Let’s create a new SingleView Application project and get on with it.

在本教程中,我们将开发一个iOS应用程序,其中包含一个自定义TableView,其单元格具有包含图像,标签和对勾标记的自定义布局。 让我们创建一个新的SingleView Application项目并继续进行。

iOS自定义TableView项目结构 (iOS Custom TableView Project Structure)

The project consists of a ViewController.swift class file which would hold the class for CustomTableViewCells too. Also, the images that we would be displaying shall be present in the Assets folder.

该项目包含一个ViewController.swift类文件,该文件也将保存CustomTableViewCells的类。 另外,我们将要显示的图像应显示在Assets文件夹中。

为iOS TableView构建情节提要 (Building Storyboards for iOS TableView)

  1. Adding a TableView and setting its constraints.
  2. Adding a TableViewCell and an ImageView in the cell and setting its constraints.
  3. Adding a label between the ImageView and the AccessoryType – checkmark and setting its constraints.在ImageView和AccessoryType之间添加标签-选中标记并设置其约束。
  4. The ViewController.swift would contain another class CustomCell that implements the UITableViewCell protocol as shown below.
    import UIKitclass CustomCell : UITableViewCell{}class ViewController: UIViewController {override func viewDidLoad() {super.viewDidLoad()// Do any additional setup after loading the view, typically from a nib.}override func didReceiveMemoryWarning() {super.didReceiveMemoryWarning()// Dispose of any resources that can be recreated.}}

    ViewController.swift将包含另一个实现UITableViewCell协议的CustomCell类,如下所示。

  5. Let’s link these classes with the storyboard.

    让我们将这些类与情节提要链接起来。

iOS自定义TableView示例代码 (iOS Custom TableView Example Code)

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

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

import UIKitclass CustomCell : UITableViewCell{@IBOutlet var myImage: UIImageView!@IBOutlet var myText: UILabel!
}class ViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {@IBOutlet var tableView: UITableView!var labelData = ["Australia", "Brazil", "Canada","China","Germany","India","Malaysia", "Pakistan", "Russia", "South Africa", "United States of America"]var imageData = ["Australia", "Brazil", "Canada","China","Germany","India","Malaysia", "Pakistan", "Russia", "SouthAfrica", "UnitedStatesofAmerica"]override func viewDidLoad() {super.viewDidLoad()// Do any additional setup after loading the view, typically from a nib.tableView.dataSource = selftableView.delegate = selftableView.tableFooterView = UIView()}override func didReceiveMemoryWarning() {super.didReceiveMemoryWarning()// Dispose of any resources that can be recreated.}func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {let cell:CustomCell = self.tableView.dequeueReusableCell(withIdentifier: "customCell") as! CustomCellcell.myText?.text = self.labelData[indexPath.row]cell.myImage?.image = UIImage(named:self.imageData[indexPath.row])return cell}func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {return labelData.count}func numberOfSections(in tableView: UITableView) -> Int {return 1}func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {tableView.deselectRow(at: indexPath, animated: true)if let cell = tableView.cellForRow(at: indexPath as IndexPath) {if cell.accessoryType == .checkmark{cell.accessoryType = .none}else{cell.accessoryType = .checkmark}}}
}

In the above code, we set the label and image from the labelData and imageData arrays respectively.
To check/uncheck a cell, we check the accessoryType attribute on the cell. If it’s equal to checkmark we toggle it to none and vice versa.

在上面的代码中,我们分别从labelData和imageData数组设置了标签和图像。
要检查/取消选中一个单元格,我们检查该单元格上的accessoryType属性。 如果它等于选中标记,则将其切换为无,反之亦然。

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

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

In case we want to allow single choice selection only we can use the below code:

如果我们只允许单选选择,我们可以使用以下代码:

import UIKitclass CustomCell : UITableViewCell{@IBOutlet var myImage: UIImageView!@IBOutlet var myText: UILabel!//For single choice selectionrequired init(coder aDecoder: NSCoder) {super.init(coder: aDecoder)!selectionStyle = .none}
}class ViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {@IBOutlet var tableView: UITableView!var labelData = ["Australia", "Brazil", "Canada","China","Germany","India","Malaysia", "Pakistan", "Russia", "South Africa", "United States of America"]var imageData = ["Australia", "Brazil", "Canada","China","Germany","India","Malaysia", "Pakistan", "Russia", "SouthAfrica", "UnitedStatesofAmerica"]override func viewDidLoad() {super.viewDidLoad()// Do any additional setup after loading the view, typically from a nib.tableView.dataSource = selftableView.delegate = selftableView.tableFooterView = UIView()}override func didReceiveMemoryWarning() {super.didReceiveMemoryWarning()// Dispose of any resources that can be recreated.}func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {let cell:CustomCell = self.tableView.dequeueReusableCell(withIdentifier: "customCell") as! CustomCellcell.myText?.text = self.labelData[indexPath.row]cell.myImage?.image = UIImage(named:self.imageData[indexPath.row])return cell}func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {return labelData.count}func numberOfSections(in tableView: UITableView) -> Int {return 1}//    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
//        tableView.deselectRow(at: indexPath, animated: true)
//
//        if let cell = tableView.cellForRow(at: indexPath as IndexPath) {
//            if cell.accessoryType == .checkmark{
//                cell.accessoryType = .none
//            }
//            else{
//                cell.accessoryType = .checkmark
//            }
//        }
//
//    }//For single choice selectionfunc tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {tableView.cellForRow(at: indexPath as IndexPath)?.accessoryType = .checkmark}func tableView(_ tableView: UITableView, didDeselectRowAt indexPath: IndexPath) {tableView.cellForRow(at: indexPath as IndexPath)?.accessoryType = .none}}

In the above code, we override the functions didSelectRowAt as well as didDeselectRowAt to allow single choice selection. We can’t use tableView.deselectRow(at: indexPath, animated: true) to animate the selection anymore since that method is already being overridden. Hence we set the selectionStyle in the customCell as none:

在上面的代码中,我们重写了didSelectRowAt函数和didDeselectRowAt以允许单选选择。 我们不能再使用tableView.deselectRow(at: indexPath, animated: true)为选择设置动画,因为该方法已被覆盖。 因此,我们将customCell中的selectionStyle设置为none:

required init(coder aDecoder: NSCoder) {super.init(coder: aDecoder)!selectionStyle = .none}

The output of the above implementation is given below:

以上实现的输出如下:

This brings an end to this tutorial. You can download the final iOS CustomTableViewWithImagesAndCheckMarks Project from the link below.

本教程到此结束。 您可以从下面的链接下载最终的iOS CustomTableViewWithImagesAndCheckMarks项目

Download Swift TableView Example Project下载Swift TableView示例项目

Reference: iOS Table View

参考: iOS表格视图

翻译自: https://www.journaldev.com/14180/ios-custom-tableview-images-checkmarks

ios cell点击对勾

ios cell点击对勾_带图像和对勾的iOS自定义TableView相关推荐

  1. jq苹果手机全屏下点击无效果_苹果系统自带外挂?无需越狱也可录制动作脚本...

    网络游戏开始流行起来之后,随之而来的是外挂的兴起,各式各样的外挂中有两种最为致命:物理外挂和系统自带外挂. 随着国内电竞行业和电竞直播的兴起,"物理外挂"出现在我们周围的频率也越来 ...

  2. cad的文字嵌入线条_带你玩转CAD!

    CAD画图已经成为化工人的必备技能.什么,这么多CAD必备技巧你居然还不知道?我该拿什么拯救你,我最最最最最最亲爱的旁友!!!下面给大家整理了50个相见恨晚的CAD技巧,带你玩转CAD!!相见恨晚的5 ...

  3. vue点击input弹出带搜索键盘并监听该元素

    1.遇到问题: 需要做一个点击input弹出带搜索的键盘. 解决: input的type="search",可弹出带搜索的键盘.并监听搜索按钮,请求数据 <input @ke ...

  4. 羊了个羊游戏网站源码_带前后端源码,带教程

    羊了个羊游戏源码_带前后端源码_及配置教程说明 这是啥游戏?据悉,这是一款卡通背景的消除闯关游戏.玩家们需要点击上方卡牌,被选中的卡牌会下移到底部的木框中,框内最多可以储存7张卡牌,当有3张相同的卡牌 ...

  5. ios 打印 详细错误日志_【专家专栏】林相宇 | IOS错误日志抓取和分析

    原标题:[专家专栏]林相宇 | IOS错误日志抓取和分析 在调试自己和别人的IOS App时,发生Crash是非常正常的情况,分析这些Crash的主要手段之一就是分析Crash发生时产生的错误日志.对 ...

  6. IOS UISearchDisplayController 点击搜索出现黑条问题解决方案

    IOS UISearchDisplayController 点击搜索出现黑条问题解决方案 参考文章: (1)IOS UISearchDisplayController 点击搜索出现黑条问题解决方案 ( ...

  7. 解决iOS机型点击输入框不能聚焦的问题

    解决iOS机型点击输入框不能聚焦的问题 参考文章: (1)解决iOS机型点击输入框不能聚焦的问题 (2)https://www.cnblogs.com/blackbentel/p/11138017.h ...

  8. iOS开发点击UIButton实现UIView的旋转

    iOS开发点击UIButton实现UIView的旋转 更多阅读请访问http://www.hopean.com http://www.hopean.com 首先创建界面,在viewDidLoad创建v ...

  9. ios 蓝牙命令发送_实战恢复cisco 2950交换机的IOS

    本来想用两台思科交换机做实验的,可是通过console口进入其中一台交换机后却发现这个台交换机的IOS文件丢失了.本来正常进入交换机后应该是首先进入到用户模式的,而且提示符应该是">& ...

最新文章

  1. 12-Qt6 列表类QList
  2. MBB IN CONSULTING
  3. angular 绑定自定义属性_Angular2实现自定义双向绑定属性
  4. javascript库之Mustache库使用说明
  5. 35年编程史沉淀下来的8条宝贵经
  6. Chain of responsibility(职责链)--对象行为型模式
  7. Nginx大规模并发原理
  8. 嵌套查询和连接查询的效率_Elasticsearch 7.x Nested 嵌套类型查询 | ES 干货
  9. 数据库大作业预览-教室管理系统
  10. Matlab Coder将m文件转换成C/C++
  11. 测量运放的输入偏置电流 - 实验准备
  12. word文档删除空白页,选中空白页面按住 delete
  13. 计算机表格增加,怎样在表格中新增一行和上排一样格式的表格。
  14. 移动端百度点击软件操作方法及常见问题
  15. w ndows10隐藏桌面设置,据说,这是80%的人都不知道的win10隐藏功能
  16. JAVA常用数据结构
  17. BigDecimal的8种round舍入模式
  18. vnr光学识别怎么打开_SLS46CK4单光束安全传感器原版使用说明-Leuzeelectronic.PDF
  19. 计算机音乐学院,乐与录音艺术学院学生在中国大学生计算机设计大赛(计算机音乐创作类)中喜获佳绩...
  20. 店铺差评有什么影响原因有哪些方面,怎么避免差评,被差评了怎么解决

热门文章

  1. mysql 查询表注释
  2. spring+hibernate的clob大字段处理
  3. 重新理解“失败是成功他妈”
  4. [转载] python中set怎么循环_Python Set集合操作
  5. [转载] python模板字符串和格式化字符串
  6. [转载] kotlin 字符串_Kotlin基本类型字符串
  7. [转载] java简易爬虫Crawler
  8. Java匹马行天下之学编程的起点——编程常识知多少
  9. 序列的修改、散列和切片
  10. 洛谷——P4053 [JSOI2007]建筑抢修