[赵三哥Swift学习笔记]UITableView
2016-09-16 06:05
393 查看
添加委托
定义总行数
cell的select事件
自定义表格Cell
表格cell的系统样式四种
设置cell的点击无样式变化
cell.selectionStyle = UITableViewCellSelectionStyle.None
class ViewController:UIViewController,UITableViewDelegate, UITableViewDataSource
定义总行数
@objc func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int{ return arr.count }显示每一条数据
@objc func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell{ let cell = UITableViewCell(style: UITableViewCellStyle.Value1, reuseIdentifier: "SimpleTableCell") let index = indexPath.row let date = arr_logs[index].createdAt cell.textLabel!.text = "\(arr[index].name)" cell.imageView?.image = UIImage(named: "list") cell.detailTextLabel?.text = "\(arr[index].date)" return cell; }
cell的select事件
@objc func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) { print("Selected \(indexPath.row)!") let index = indexPath.row var segue = "" switch index { case 0:segue = "firstSegue" case 1:segue = "secondSegue" case 2:segue = "thirdSegue" default:break } self.navigationController!.performSegueWithIdentifier(segue, sender: nil) }设置表格名称
@objc func tableView(tableView: UITableView, titleForHeaderInSection section: Int) -> String? { return "我是标题" }设置分区个数
@objc func numberOfSectionsInTableView(tableView: UITableView) -> Int { return 1 }
自定义表格Cell
import Foundation import UIKit class MyCell:UITableViewCell { @IBOutlet weak var lb_name: UILabel! @IBOutlet weak var lb_times: UILabel! } class ViewController:UIViewController,UITableViewDelegate, UITableViewDataSource { var arr = [“Task1”,”Task2”,”Task3”,”Task4”,”Task5”] override func viewDidLoad() { super.viewDidLoad() } // total record count @objc func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int{ return arr.count } //display each item @objc func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell{ var cell:MyCell = tableView.dequeueReusableCellWithIdentifier(“MyCell", forIndexPath: indexPath) as! MyCell if cell.isEqual(nil) { cell = MyCell(style: UITableViewCellStyle.Default, reuseIdentifier: “MyCell") } cell.lb_name.text = "\(arr[indexPath.row])" cell.lb_times.text = “100 times" return cell; } }
表格cell的系统样式四种
UITableViewCellStyle.Default | UITableViewCellStyle.Value1 | UITableViewCellStyle.Value2 | UITableViewCellStyle.Subtitle |
cell.selectionStyle = UITableViewCellSelectionStyle.None
相关文章推荐
- 峰回路转,Firefox 浏览器即将重返 iOS 平台
- 峰回路转,Firefox 浏览器即将重返 iOS 平台
- 不可修补的 iOS 漏洞可能导致 iPhone 4s 到 iPhone X 永久越狱
- iOS 12.4 系统遭黑客破解,漏洞危及数百万用户
- Apple Swift学习教程
- 每日安全资讯:NSO,一家专业入侵 iPhone 的神秘公司
- [转][源代码]Comex公布JailbreakMe 3.0源代码
- 介绍 Fedora 上的 Swift
- ruby 一些简单的例子
- 多文件上传的例子
- JDBC-ODBC翻页例子
- 讲解iOS开发中基本的定位功能实现
- iOS中定位当前位置坐标及转换为火星坐标的方法
- js判断客户端是iOS还是Android等移动终端的方法
- iOS应用开发中AFNetworking库的常用HTTP操作方法小结
- iOS应用中UISearchDisplayController搜索效果的用法
- iOS App开发中的UISegmentedControl分段组件用法总结
- IOS开发环境windows化攻略