您的位置:首页 > 移动开发 > Swift

[赵三哥Swift学习笔记]UITableView

2016-09-16 06:05 393 查看
添加委托

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的点击无样式变化

cell.selectionStyle = UITableViewCellSelectionStyle.None
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  ios swift 例子 uitableview