我的GitHub:github.com/jixiang0903/swfit-UITableView/tree/master
class ViewController: UIViewController, UITableViewDataSource, UITableViewDelegate{
//MARK: 控件區(qū)
@IBOutlet weak var tableView: UITableView!
//MARK: cell 標(biāo)識(shí)符
fileprivate let cellId = "GGgirlsID"
var ctrNames:NSMutableArray?
override func viewDidLoad() {
super.viewDidLoad()
self.title = "主頁"
//初始化數(shù)據(jù)源
loadData()
//調(diào)用table方法
makeTable()
}
//初始化數(shù)據(jù)源
func loadData(){
ctrNames = ["1A","2B","3C","4D","5E","6F","7G","8H","9I","10J"]
self.tableView .reloadData()
}
//創(chuàng)建tableView
func makeTable(){
//設(shè)置cell分割線是否顯示
//self.tableView.separatorStyle = UITableViewCellSeparatorStyle.none
self.tableView.delegate = self//實(shí)現(xiàn)代理
self.tableView.dataSource = self//實(shí)現(xiàn)數(shù)據(jù)源
//nib
self.tableView.register(UINib(nibName: "GGirlsCell", bundle: nil), forCellReuseIdentifier: cellId)
//去除tableView 多余行的方法 添加一個(gè)tableFooterView 后面多余行不再顯示
self.tableView.tableFooterView = UIView()
}
//MARK: 數(shù)據(jù)源
//cell個(gè)數(shù)
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int
{
return self.ctrNames!.count
}
//tablViewCell 高度
func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat{
return 100.0;
}
/*
//頭部高度
func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
return 0.01
}
//底部高度
func tableView(_ tableView: UITableView, heightForFooterInSection section: Int) -> CGFloat {
return 0.01
}
*/
//cell賦值
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell
{
// Nib注冊(cè), 調(diào)用的時(shí)候會(huì)調(diào)用自定義cell中的? awakeFromNib? 方法
let cell = tableView.dequeueReusableCell(withIdentifier: self.cellId, for: indexPath) as! GGirlsCell
cell.girlImage.image = UIImage(named: "\(indexPath.row).jpg")
cell.titleLabel?.text = ctrNames![indexPath.row] as? String
return cell
}
//cell點(diǎn)擊事件
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
tableView.deselectRow(at: indexPath as IndexPath, animated: true)
print(ctrNames![indexPath.row])
}
//cell填滿屏幕
func tableView(_ tableView: UITableView, willDisplay cell: UITableViewCell, forRowAt indexPath: IndexPath) {
if cell.responds(to:#selector(setter: UIView.layoutMargins)) {
cell.layoutMargins = UIEdgeInsets.zero
}
if cell.responds(to: #selector(setter: UITableViewCell.separatorInset)) {
cell.separatorInset = UIEdgeInsets.zero
}
}
//允許編輯cell
func tableView(_ tableView: UITableView, canEditRowAt indexPath: IndexPath) -> Bool {
return true
}
//右滑觸發(fā)刪除按鈕
func tableView(_ tableView: UITableView, editingStyleForRowAt indexPath: IndexPath) -> UITableViewCellEditingStyle {
return UITableViewCellEditingStyle.init(rawValue: 1)!
}
//點(diǎn)擊刪除cell時(shí)觸發(fā)
func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCellEditingStyle, forRowAt indexPath: IndexPath) {
print("indexPath.row = editingStyle第\(indexPath.row)行")
}
}
我的GitHub:github.com/jixiang0903/swfit-UITableView/tree/master