详细请移步github https://github.com/Zhangjingwang1993/Swift.git
var tableViewself = UITableView()
var arrayData = ["cell1", "cell2", "cell3", "cell4", "cell5"]
var buttonRight = UIButton()
创建TableView
func createTableview(){
tableViewself = UITableView.init(frame: self.view.bounds, style: UITableViewStyle.Plain)
self.view.addSubview(tableViewself)
tableViewself.delegate = self
tableViewself.dataSource = self
// 注册
tableViewself.registerClass(TableViewCell.self, forCellReuseIdentifier: "cellId")
}
Tableview Delegate
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return arrayData.count
}
func numberOfSectionsInTableView(tableView: UITableView) -> Int{
return 1;
}
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell{
let cell = TableViewCell.init(style: UITableViewCellStyle.Default, reuseIdentifier: "cellId")
cell.titleLabel?.text = "自定义cell"
return cell
}
// 删除一行
func tableView(tableView: UITableView, commitEditingStyle editingStyle: UITableViewCellEditingStyle, forRowAtIndexPath indexPath: NSIndexPath){
let index = indexPath.row as Int
arrayData.removeAtIndex(index)
tableViewself.deleteRowsAtIndexPaths([indexPath], withRowAnimation: UITableViewRowAnimation.Top)
}
// 选择一行
func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
let alter = UIAlertView()
alter.title = "提示"
alter.message = "您选择的是:\(arrayData[indexPath.row])"
alter.addButtonWithTitle("OK")
alter.show()
}
// 加右边的按钮
func createRightBarButtonItem() {
buttonRight = UIButton.init(type: UIButtonType.Custom)
buttonRight.frame = CGRectMake(0, 0, 40, 40)
self.navigationItem.rightBarButtonItem = UIBarButtonItem(customView: buttonRight)
buttonRight.setTitle("Edit", forState: UIControlState.Normal)
buttonRight.addTarget(self, action: Selector("jion:"), forControlEvents: UIControlEvents.TouchUpInside)
}
func jion(sender: UIButton){
sender.selected = !sender.selected
if sender.selected == true
{
tableViewself .setEditing(true, animated: true)
}else{
tableViewself .setEditing(false, animated: true)
}
}
自定义cell
// 定义
var titleLabel:UILabel?
// 初始化
override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
super.init(style: UITableViewCellStyle.Default, reuseIdentifier: reuseIdentifier)
self.creteCell()
}
required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
// 创建
func creteCell(){
titleLabel = UILabel.init(frame: CGRectMake(0, 0, 300, 20))
titleLabel?.backgroundColor = UIColor.cyanColor()
self.addSubview(titleLabel!)
}