博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
UIKit应用 - Swift 版本: 3.让UITableViewCell的背景色渐变
阅读量:4648 次
发布时间:2019-06-09

本文共 1773 字,大约阅读时间需要 5 分钟。

这次我们来分享一下关于 UITableView 的一个开发小技巧, 后面我会陆续的把关于 UITableView 的其他开发小技巧补充上, 废话少说, 让我们来看看代码


1.界面布局

1

关于怎么快速添加一个 UINavigationController 在上两篇文章里有讲解, 这里就不说了, 下面让我们来看看代码.


2.实现代码

遵守代理协议和数据源协议

class ViewController: UIViewController, UITableViewDataSource, UITableViewDelegate {}

设置代理对象

override func viewDidLoad() {        super.viewDidLoad()        myTableView.delegate = self        myTableView.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! UITableViewCell        cell.textLabel!.text = stringArray[indexPath.row]        return cell    }

实现自定义方法

func colorForIndex(index: Int) -> UIColor {        let itemCount = stringArray.count - 1        let color = (CGFloat(index) / CGFloat(itemCount)) * 0.6        return 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.最终效果

1


好了, 这次我们就讲到这里, 下次我们继续~~~

转载于:https://www.cnblogs.com/iOSCain/p/4529329.html

你可能感兴趣的文章
团队冲刺04
查看>>
我的Python分析成长之路8
查看>>
泛型在三层中的应用
查看>>
SharePoint2010 -- 管理配置文件同步
查看>>
.Net MVC3中取得当前区域的名字(Area name)
查看>>
获得屏幕像素以及像素密度
查看>>
int与string转换
查看>>
adb命令 判断锁屏
查看>>
推荐一个MacOS苹果电脑系统解压缩软件
查看>>
1035等差数列末项计算
查看>>
CDMA鉴权
查看>>
ASP.NET MVC Identity 兩個多個連接字符串問題解決一例
查看>>
过滤器与拦截器区别
查看>>
第二阶段站立会议7
查看>>
[18]Debian Linux Install GNU GCC Compiler and Development Environment
查看>>
JAVA多线程
查看>>
ACE(Adaptive Communication Environment)介绍
查看>>
delphi 更改DBGrid 颜色技巧
查看>>
python编码问题
查看>>
POJ 2031 Building a Space Station
查看>>