TableView之修改单元格

//
// ViewController.swift
// UITableViewDemo6
//
// Created by Sundy on 1/14/15.
// Copyright (c) 2015 maiziedu. All rights reserved.
//

import UIKit

class ViewController: UIViewController,UITableViewDataSource,UITableViewDelegate {

@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.
}


}

 

转载于:https://www.cnblogs.com/heartless/p/5030721.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值