使用UITableView有两种方式:
1、使用TabViewController
2、继承UITableView
TableViewController
1,拖动一个TableViewController。选中,然后点击Editor -》 Embed in -》Navegation Controller。然后界面如下所示
2,创建一个Cocoa Touch类继承UITableViewController
class TableViewController: UITableViewController {
override func viewDidLoad() {
super.viewDidLoad()
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
}
// MARK: - Table view data source
// 这里表示tableview有个部分,这个值不能是0,如果是0将没有东西显示
override func numberOfSectionsInTableView(tableView: UITableView) -> Int {
return 1
}
// 一部分有几行
override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return 3
}
// 这里对cell初始化
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier("cell", forIndexPath: indexPath) as UITableViewCell
cell.textLabel.text = "item 1"
// Configure the cell...
return cell
}
}
3,在storyboard设置这个类
4,设置TableViewCell的style
可以选择tableViewCell的样式
5,设置TableViewCell的identifier
设置好后左侧面板的Table View cell应该变成了你设置的那个名字
6,在TableViewController 类的tableView方法中对cell赋值
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
// 第一个参数就是之前设置的identifier属性
let cell = tableView.dequeueReusableCellWithIdentifier("cell", forIndexPath: indexPath) as UITableViewCell
cell.textLabel?.text = "item "
return cell
}
7,选择一个平台,然后点击三角形运行
8,效果图:
自定义Table View Cell
1,选择style为custom
2,给Table View Cell添加一个Label
为了方便演示我只拖了一个label
3,创建一个类继承UITableViewCell
4,修改Table View Cell对应的类为创建的那个类
5,修改TableViewController中方法
6,效果图
UITableView
1,拖拽一个TableView到View Controller
2,创建一个类继承UITableView
class MyTableView: UITableView {
/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
override func drawRect(rect: CGRect) {
// Drawing code
}
*/
}
3,添加一个初始化方法
required init(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
self.dataSource = self
}
4,实现UITableDataSource
<pre name="code" class="javascript">class MyTableView: UITableView , UITableViewDataSource {
5,添加两个方法
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int{
return 3
}
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell{
// 这里记得要到tableViewCell设置identifier
let cell = tableView.dequeueReusableCellWithIdentifier("cell", forIndexPath: indexPath) as UITableViewCell
cell.textLabel?.text = "This is Title"
cell.detailTextLabel?.text = "This is detail"
return cell
}
6,设置到TableView
7,运行
完整代码
class MyTableView: UITableView , UITableViewDataSource {
/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
override func drawRect(rect: CGRect) {
// Drawing code
}
*/
required init(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
self.dataSource = self
}
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int{
return 3
}
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell{
let cell = tableView.dequeueReusableCellWithIdentifier("cell", forIndexPath: indexPath) as UITableViewCell
cell.textLabel?.text = "This is Title"
cell.detailTextLabel?.text = "This is the detail"
return cell
}
}