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

Swift学习之UITableView 排序

2016-01-13 22:48 369 查看
import UIKit

class ViewController: UIViewController, UITableViewDataSource, UITableViewDelegate {

var dataList:[String]?

var tableView:UITableView?

override func loadView() {

super.loadView()

}

override func viewDidLoad() {

super.viewDidLoad()

// Do any additional setup after loading the view, typically from a nib.

self.dataList! = ["数据1", "数据2", "数据3", "数据4", "数据5", "数据6", "数据7", "数据8", "数据9", "数据10", "数据11", "数据12", "数据13", "数据14"];

self.tableView = UITableView(frame: self.view.bounds, style: UITableViewStyle.Plain)

self.tableView!.dataSource = self;

self.tableView!.delegate = self;

self.tableView!.registerClass(UITableViewCell.self, forCellReuseIdentifier: "Cell")

self.view.addSubview(tableView!)

let editButton:UIButton = UIButton(frame: CGRectMake(0, 0, 100, 50))

editButton.backgroundColor = UIColor.blueColor()

editButton.setTitle("编辑", forState: UIControlState.Normal)

editButton.setTitleColor(UIColor.whiteColor(), forState: UIControlState.Normal)

editButton.addTarget(self, action: Selector("clickButtonAction:"), forControlEvents: UIControlEvents.TouchUpInside)

self.view.addSubview(editButton)

}

func clickButtonAction(button:UIButton) {

if self.tableView!.editing {

self.tableView!.editing = false

button.setTitle("编辑", forState: UIControlState.Normal)

} else {

self.tableView!.editing = true

button.setTitle("完成", forState: UIControlState.Normal)

}

}

//返回有多少个分组

func numberOfSectionsInTableView(tableView: UITableView) -> Int {

return 1

}

//每个分组对应的cell个数

func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {

return dataList!.count

}

//创建Cell

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {

let identifier:String = "Cell"

let cell = tableView.dequeueReusableCellWithIdentifier(identifier, forIndexPath: indexPath) as UITableViewCell

cell.textLabel!.text = self.dataList![indexPath.row]

return cell;

}

//处理选中事件

func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {

self.tableView!.deselectRowAtIndexPath(indexPath, animated: true)

print("选中的Cell 为\(self.dataList![indexPath.row])")

}

//返回编辑类型

func tableView(tableView: UITableView, editingStyleForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCellEditingStyle {

//设置为None就没有滑动删除

return UITableViewCellEditingStyle.None

}

//是否允许移动

func tableView(tableView: UITableView, canMoveRowAtIndexPath indexPath: NSIndexPath) -> Bool {

return true

}

//移动后的事件处理

func tableView(tableView: UITableView, moveRowAtIndexPath sourceIndexPath: NSIndexPath, toIndexPath destinationIndexPath: NSIndexPath) {

}

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: