UIKit应用 - Swift 版本: 3.让UITableViewCell的背景色渐变
这次我们来分享一下关于 UITableView 的一个开发小技巧, 后面我会陆续的把关于 UITableView 的其他开发小技巧补充上, 废话少说, 让我们来看看代码
1.界面布局
关于怎么快速添加一个 UINavigationController 在上两篇文章里有讲解, 这里就不说了, 下面让我们来看看代码.
2.实现代码
遵守代理协议和数据源协议
class ViewController: UIViewController, UITableViewDataSource, UITableViewDelegate {}
设置代理对象
override func viewDidLoad() {super.viewDidLoad()myTableView.delegate = selfmyTableView.dataSource = self}
获取属性和声明数据
@IBOutlet weak var myTableView: UITableView!let stringArray = ["1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12", "13", "14", "15"]
实现代理方法和数据源方法
func numberOfSectionsInTableView(tableView: UITableView) -> Int {return 1}func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {return stringArray.count}func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {let cell = tableView.dequeueReusableCellWithIdentifier("tableCell", forIndexPath: indexPath) as! UITableViewCellcell.textLabel!.text = stringArray[indexPath.row]return cell}
实现自定义方法
func colorForIndex(index: Int) -> UIColor {let itemCount = stringArray.count - 1let color = (CGFloat(index) / CGFloat(itemCount)) * 0.6return UIColor(red: 0.8, green: color, blue: 0.2, alpha: 1.0)}func tableView(tableView: UITableView, willDisplayCell cell: UITableViewCell, forRowAtIndexPath indexPath: NSIndexPath) {cell.backgroundColor = colorForIndex(indexPath.row)}func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {self.myTableView.deselectRowAtIndexPath(indexPath, animated: true)}
3.最终效果
好了, 这次我们就讲到这里, 下次我们继续~~~
转载于:https://www.cnblogs.com/iOSCain/p/4529329.html
UIKit应用 - Swift 版本: 3.让UITableViewCell的背景色渐变相关推荐
- 使用swiftenv管理swift版本
swiftenv可以让你轻松安装和快速切换在多个版本之间切换的工具 安装swiftenv 1.下载swifenv git clone https://github.com/kylef/swiftenv ...
- MacOS Xcode与Swift版本对应关系
Swift4 Xcode9.0 or later macOS 10.12.6+ Xcode 9.4.1 macOS 10.13.2+ ...
- ios使用SARUnArchiveANY 解压rar文件(oc和swift版本)
SARUnArchiveANY简介 开源库网址: https://github.com/saru2020/SARUnArchiveANY 简介: 一个iOS的非常有用的库来解压zip,.rar,7z文 ...
- oc和swift版本的实时绘制曲线、折线图,股票折线图
oc和swift版本的实时绘制曲线.折线图,股票折线图,可设置曲线背景颜色,支持渐变颜色背景 demo:https://download.csdn.net/download/Wulitc/875132 ...
- Swift版本都到5了,ABI稳定了,你清楚吗
一.ABl是什么 每个操作系统都会为运行在该系统下的应用程序提供应用程序二进制接口(Application Binary Interface,即ABI), 描述了应用程序和操作系统之间的底层接口,一个 ...
- swift 3迁移到swift5 swift版本迁移指南
Swift 5已经发布下面是官方swift5的介绍. Introducing Swift 5.1 Swift 5.1 now makes it easier to create and share b ...
- core image几个滤镜例子 oc版本和swift版本
oc版本 //万花筒模式 + (CGImageRef) getKaleidoscope:(CIContext *)context {CIImage * image =[CIImage imageWit ...
- UIKit框架-高级控件Swift版本: 2.UITableView方法/属性详解
由于这段时间比较忙, 所以写博文的时间也挤了很长时间才完成, 希望大家能够原谅, 后面我也会陆续的更新, 直到控件都讲完, 好了废话少说, 现在让我们来看今天的内容. 1.UITableview常用属 ...
- UIKit框架-高级控件Swift版本: 6.UIAlertView方法/属性详解
前面我们学习了 UITextView, 现在让我们继续往下学. 1.UIAlertView的常用属性 // 1.设置 UIAlertView 的代理对象 var delegate: AnyObject ...
最新文章
- markdown语法进阶
- SubSonic数据库操作
- python双循环zip_Python如何同时进行两个循环
- UITableView-FDTemplateLayoutCell自动计算UITableView高度的使用
- Eclipse下导入外部jar包的3种方式
- 【HDU - 3714 】Error Curves (三分)
- python 执行完成后,cmd窗口自动关闭
- 批量生成10个虚拟主机配置
- IT NEWS WebSite
- 5.Springcloud的Ribbon组件的集成及实现轮询负载均衡方式
- 【汇总目录】嵌入式系统技术
- java丑数算法_Java代码如何实现求第N个丑数?实现和思路分享
- 在串口调试助手上使用AT命名控制GPRS模块发送短信
- 使用同源建模预测蛋白质结构
- 《机器学习》学习笔记
- word 流程图制作
- python对数正态分布函数_python生成具有上下限的截断对数正态分布
- unity, AnimatorCullingMode踩坑
- <Halcon>基于形状和轮廓的模板匹配
- 【Tool】TI公司14-Pin JTAG规范以及参考原理图