//
// ViewController.swift
// UITableViewDemo6
//
// Created by Sundy on 1/14/15.
// Copyright (c) 2015 maiziedu. All rights reserved.
//swift
import UIKit數組
class ViewController: UIViewController,UITableViewDataSource,UITableViewDelegate {ide
@IBOutlet var tableView1: UITableView!
var markTag = 1
//對應storyborad insert按鈕的action
@IBAction func editButtonClick(sender: UIBarButtonItem) {
//設置編輯狀態
markTag = 1
tableView1.setEditing(!tableView1.editing, animated: true)
if(tableView1.editing){
sender.title = "Done"
}else{
sender.title = "Edit"
}
}
//對應storyborad insert按鈕的action
@IBAction func insertButtonClick(sender: UIBarButtonItem) {
markTag = 2
//設置編輯狀態
tableView1.setEditing(!tableView1.editing, animated: true)
if(tableView1.editing){
sender.title = "Done"
}else{
sender.title = "Insert"
}排序
}
//設置是否可編輯
func tableView(tableView: UITableView, canEditRowAtIndexPath indexPath: NSIndexPath) -> Bool {
return true
}
//返回編輯狀態
func tableView(tableView: UITableView, editingStyleForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCellEditingStyle {
if(markTag==1){
return UITableViewCellEditingStyle.Delete
}else{
return UITableViewCellEditingStyle.Insert
}
}
//內容提示
func tableView(tableView: UITableView, titleForDeleteConfirmationButtonForRowAtIndexPath indexPath: NSIndexPath) -> String! {
return "確認刪除?"
}
//提交編輯
func tableView(tableView: UITableView, commitEditingStyle editingStyle: UITableViewCellEditingStyle, forRowAtIndexPath indexPath: NSIndexPath) {
//刪除 , 1,刪除數組元素,2,刪除tableview單元格
var proName = provinces[indexPath.section]
if(markTag == 1){
cities[proName]?.removeAtIndex(indexPath.row)
tableView.deleteRowsAtIndexPaths([indexPath], withRowAnimation: UITableViewRowAnimation.Fade)
}else{
//首先獲得當前項的城市名字
var cityName = cities[proName]?[indexPath.row]
cities[proName]?.insert(cityName!, atIndex: indexPath.row+1)
tableView.insertRowsAtIndexPaths([indexPath], withRowAnimation: UITableViewRowAnimation.Fade)
}
}
var provinces = ["四川","雲南","山東"]
var cities = ["四川":["成都","綿陽","廣元","成都","綿陽","廣元"],"雲南":["昆明","大理","麗江","昆明","大理","麗江"],"山東":["濟南","青島","威海","濟南","青島","威海","濟南","青島","威海","濟南","青島","威海"]]
//把拖進來的tableview加載到此UIViewController來
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
tableView1.dataSource = self
tableView1.delegate = self
}
//cell的個數
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
var provinceName = provinces[section]//經過節點找provinces
return cities[provinceName]!.count//經過provinces找到cities(key-value)
}
//Section節點的名稱
func tableView(tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
return provinces[section]
}
//節點索引的名稱(按照index排序的,內容可任意)
func sectionIndexTitlesForTableView(tableView: UITableView) -> [AnyObject]! {
return ["A","B","C"]
}
//cell的內容
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
var cellId = "sundycell"
var cell:UITableViewCell? = tableView.dequeueReusableCellWithIdentifier(cellId) as? UITableViewCell
if(cell == nil){
cell = UITableViewCell(style: UITableViewCellStyle.Default, reuseIdentifier: cellId)
}
//先獲得當前section名稱,省名稱
var proName = provinces[indexPath.section]
//獲得當前row索引的城市的名稱
cell?.textLabel?.text = cities[proName]![indexPath.row]
cell?.accessoryType = UITableViewCellAccessoryType.DisclosureIndicator
return cell!
}
//節點的個數
func numberOfSectionsInTableView(tableView: UITableView) -> Int {
return provinces.count
}索引
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}ci
}rem