版本记录
版本号 | 时间 |
---|---|
V1.0 | 2017.08.03 |
前言
前面已经对
swift
的基础理论进行了讲解,下面我们就来给出一个简单的swift程序,以慢慢熟悉代码,后面会增加难度。
详情
这里给出的程序是UITableView
相关的,UITableView
是app
中常用的元素,想了好久写一个简单的代码,慢慢熟悉swift
的使用,那么下面我们就从这里开始吧。
下面我们就直接看代码。
import UIKit
import Masonry
class JJFirstTableVC : UIViewController, UITableViewDelegate, UITableViewDataSource
{
var tableView : UITableView?
let kJJTableViewCellIdentify = "kJJTableViewCellIdentify"
let titleArr = ["第一个", "第二个", "第三个", "第四个", "第五个"]
//MARK - Override Base Function
override func viewDidLoad()
{
super.viewDidLoad()
title = "TableView"
view.backgroundColor = UIColor.lightGray
setupUI()
}
//MARK - Object Private Function
func setupUI(){
tableView = UITableView(frame: view.frame, style: UITableViewStyle.plain)
tableView!.backgroundColor = UIColor.white
tableView!.dataSource = self
tableView!.delegate = self
tableView!.rowHeight = 80.0
tableView!.register(JJTableViewCell.self, forCellReuseIdentifier: kJJTableViewCellIdentify)
view.addSubview(tableView!)
}
//MARK - UITableViewDelegate, UITableViewDataSource
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return titleArr.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: kJJTableViewCellIdentify, for: indexPath) as! JJTableViewCell
cell.accessoryType = UITableViewCellAccessoryType.disclosureIndicator
cell.setStringForCell(titleStr: titleArr[indexPath.row])
return cell
}
}
class JJTableViewCell : UITableViewCell
{
var titleLabel : UILabel?
//MARK - Override Base Function
override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
super.init(style: style, reuseIdentifier: reuseIdentifier)
selectionStyle = UITableViewCellSelectionStyle.none
self.backgroundColor = UIColor.lightGray
setupUI()
}
required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
//MARK - Object Private Function
func setupUI() {
titleLabel = UILabel()
titleLabel?.sizeToFit()
addSubview(titleLabel!)
titleLabel?.mas_makeConstraints({ (make : MASConstraintMaker!) in
make!.center.equalTo()(self.center)?.setOffset(0.0)
})
}
func setStringForCell(titleStr : String){
titleLabel?.text = titleStr
}
}
上面就是自定义cell
的UITableView
,下面看一下效果示意图。
后记
这里只是简单的给出了swift版本的自定义TableView,很简单的一个程序,后面我会结合工程具体说明swift的使用以及具体遇到的坑。