27.UITableView初探
2016-03-13 21:24
465 查看
UITableView
UITableView是iOS中使用非常常用的控件,它是一个列表控件, 下面我们学习如何使用它1. UITableView的创建
import UIKit class ViewController: UIViewController, UITableViewDataSource, UITableViewDelegate { override func viewDidLoad() { super.viewDidLoad() // Do any additional setup after loading the view, typically from a nib. let tableView = UITableView(frame: self.view.bounds, style: UITableViewStyle.Plain) tableView.dataSource = self tableView.delegate = self // 创建一个可重用的cell tableView.registerClass(UITableViewCell.self, forCellReuseIdentifier: "mycell") self.view.addSubview(tableView) } override func didReceiveMemoryWarning() { super.didReceiveMemoryWarning() // Dispose of any resources that can be recreated. } // MARK: - UITableViewDataSource // MARK: 返回分组数 func numberOfSectionsInTableView(tableView: UITableView) -> Int { return 10 } // MARK: 返回行数 func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int { return 3 } // MARK: 返回显示的内容 func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { var cell = tableView.dequeueReusableCellWithIdentifier("mycell") if cell == nil { cell = UITableViewCell(style: UITableViewCellStyle.Default, reuseIdentifier: "mycell") } // 右侧的图标 cell?.accessoryType = .DisclosureIndicator cell?.textLabel?.text = "section:\(indexPath.section), row:\(indexPath.row)" cell?.detailTextLabel?.text = "我是详情" cell?.imageView?.image = UIImage(named: "star") return cell! } }
运行程序:
我们发现UITableView就是一个可以滚动的列表, 它的显示内容,行数和分组数都是在dataSource中设置的.
上面的代码中,我们的UITableView的style是Plain, 我们设置成Grouped试试
let tableView = UITableView(frame: self.view.bounds, style: UITableViewStyle.Grouped)
2. UITableView设置footer和header的title
上面的实例中, 我们发现会有一个灰色的区域,我们给它们设置一些文字,实现UITableViewDataSource的两个方法即可:// Header的标题 func tableView(tableView: UITableView, titleForHeaderInSection section: Int) -> String? { return "headerTitle" } // Footer的标题 func tableView(tableView: UITableView, titleForFooterInSection section: Int) -> String? { return "footerTitle" }
运行程序:
现在我们将UITableView改为不分组的模式:
let tableView = UITableView(frame: self.view.bounds, style: UITableViewStyle.Plain)
运行程序:
我们发现灰色部分在滚动的时候先不动,直到下一个灰块将它顶上去.还是蛮有意思的效果.
3. UITableView的点击
现在,我们实现一个需求, 当点击一个item时候,我们在控制台中打印出被点击的item的section和row// MARK: - UITableViewDelegate // MARK: 选中item func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) { // 取消选中 tableView.deselectRowAtIndexPath(indexPath, animated: true) print("选中位置: section->\(indexPath.section), row->\(indexPath.row)") }
运行程序, 我们点击某个item的时候,控制台会打印出item所处的section和row,如:
选中位置: section->0, row->0 选中位置: section->0, row->1 选中位置: section->0, row->2
这个小结,我们只学习如何简单的使用UITableView来实现一个效果, 后面我们会慢慢深入.
4. 全部代码
import UIKit
class ViewController: UIViewController, UITableViewDataSource, UITableViewDelegate {
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
let tableView = UITableView(frame: self.view.bounds, style: UITableViewStyle.Plain)
tableView.dataSource = self
tableView.delegate = self
// 创建一个可重用的cell
tableView.registerClass(UITableViewCell.self, forCellReuseIdentifier: "mycell")
self.view.addSubview(tableView)
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
// MARK: - UITableViewDataSource
// MARK: 返回分组数
func numberOfSectionsInTableView(tableView: UITableView) -> Int {
return 10
}
// MARK: 返回行数
func table
a49d
View(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return 3
}
// MARK: 返回显示的内容
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
var cell = tableView.dequeueReusableCellWithIdentifier("mycell")
if cell == nil {
cell = UITableViewCell(style: UITableViewCellStyle.Default, reuseIdentifier: "mycell")
}
// 右侧的图标
cell?.accessoryType = .DisclosureIndicator
cell?.textLabel?.text = "section:\(indexPath.section), row:\(indexPath.row)"
cell?.detailTextLabel?.text = "我是详情"
cell?.imageView?.image = UIImage(named: "star")
return cell!
}
// Header的标题 func tableView(tableView: UITableView, titleForHeaderInSection section: Int) -> String? { return "headerTitle" } // Footer的标题 func tableView(tableView: UITableView, titleForFooterInSection section: Int) -> String? { return "footerTitle" }
// MARK: - UITableViewDelegate // MARK: 选中item func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) { // 取消选中 tableView.deselectRowAtIndexPath(indexPath, animated: true) print("选中位置: section->\(indexPath.section), row->\(indexPath.row)") }
}
相关文章推荐
- Apple Swift学习教程
- 介绍 Fedora 上的 Swift
- 访问控制列表概述
- 路由器访问列表的应用
- VBS CHR码值对应列表
- Perl中的列表和数组学习笔记
- 鼠标滑过 放大显示效果的列表
- Erlang语法学习笔记:变量、原子、元组、列表、字符串
- php通过分类列表产生分类树数组的方法
- dedecms列表中显示文章完整标题的解决办法
- 常见系统进程列表第1/2页
- JavaScript实现动态删除列表框值的方法
- JS 实现列表与多选框选择附预览动画
- jQuery实现仿腾讯视频列表分页效果的方法
- jquery实现列表上下移动功能
- jquery实现点击展开列表同时隐藏其他列表
- jquery制作select列表双向选择示例代码
- 用javascript实现li 列表数据隔行变换背景颜色
- iOS应用中UITableView左滑自定义选项及批量删除的实现