//
// 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