import UIKit
//实现tableview的代理
class ViewController: UIViewController,UITableViewDelegate,UITableViewDataSource{
@IBOutlet weak var editBUtton: UIButton!
var item:[String] = ["高级应用","草卡其","杭州","宁波","上海"]
var listTableView = UITableView()
override func viewDidLoad() {
//定义数组
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
//代码创建UITABLEview
listTableView = UITableView(frame: CGRectMake(0, 0, 300, 600),style:UITableViewStyle.Plain)
listTableView.delegate = self
listTableView.dataSource = self
self.view.addSubview(listTableView)
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
//显示文本或图片 实现以下4各步骤
//1系统默认是一节
func numberOfSectionsInTableView(tableView: UITableView) -> Int {
return 1
}
// 2设置单元格的行数
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return item.count
}
//3设置单元格 创建单元格需注意 必须先定义一个标示符 指定给cell
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cellIdentifier:String = "cellIdentifier"
//有可能不存在 所以用optional
var cell:UITableViewCell = tableView.dequeueReusableCellWithIdentifier(cellIdentifier)! as UITableViewCell
//如果cell去到为空
//创建新的cell 样式:default 标示符:cellidentifier
cell = UITableViewCell(style: UITableViewCellStyle.Default, reuseIdentifier: "cellIdentifier")
cell.textLabel?.font = UIFont.systemFontOfSize(14)
//选中的cell样式
cell.selectionStyle = .Gray
//cell后面箭头的方式
cell.accessoryType = .DisclosureIndicator
//从数组中赋值
cell.textLabel?.text = self.item[indexPath.row]
return cell
}
//设置单元格的高度
func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
return 50
}
//tableviewcell的点击事件
func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
//释放选中的效果
tableView.deselectRowAtIndexPath(indexPath, animated: true)
//获得点击的cell
let cell = tableView.cellForRowAtIndexPath(indexPath)
print(cell?.textLabel?.text)
var viewCT1:ViewController?
//判断cell是否为空
if cell?.textLabel?.text == "杭州"
{
var newCt1 = UITableViewController(nibName: "UITableViewControllerAF", bundle: NSBundle.mainBundle())
viewCT1?.title = cell?.textLabel?.text
}
}
//确定每一行是否具有编辑功能
func tableView(tableView: UITableView, canEditRowAtIndexPath indexPath: NSIndexPath) -> Bool {
return true
}
//指定每一行的编辑类型
func tableView(tableView: UITableView, editingStyleForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCellEditingStyle {
return .Insert //插入 当然还有删除 含有没有功能的
}
//指定单元格是否允许拖动
func tableView(tableView: UITableView, canMoveRowAtIndexPath indexPath: NSIndexPath) -> Bool {
return true
}
@IBAction func editButtonClick(sender: UIButton) {
if editBUtton.titleForState(UIControlState.Normal) == "编辑" {
//完成编辑的操作
listTableView.setEditing(true, animated: true)
editBUtton.setTitle("完成", forState: UIControlState.Normal)
}else {
//如果是完成 则设置成编辑
listTableView.setEditing(false, animated: true)
editBUtton.setTitle("编辑", forState: UIControlState.Normal)
}
}
//之后实现tableviewcell的删除操作
func tableView(tableView: UITableView, commitEditingStyle editingStyle: UITableViewCellEditingStyle, forRowAtIndexPath indexPath: NSIndexPath) {
if editingStyle == UITableViewCellEditingStyle.Delete
{
item.removeAtIndex(indexPath.row)
//tableview删除改行
tableView.deleteRowsAtIndexPaths([indexPath], withRowAnimation: UITableViewRowAnimation.Left)
}else if editingStyle == UITableViewCellEditingStyle.Insert
{
item.append("新城市(item.count)")
tableView.insertRowsAtIndexPaths([indexPath], withRowAnimation: UITableViewRowAnimation.Right)
}
}
}
tableview的使用
最新推荐文章于 2024-08-22 10:08:33 发布