class ZHZOneViewController: ZHZBaseViewController,UITableViewDelegate,UITableViewDataSource {
private var tabV:UITableView?
lazy var dataArr:NSMutableArray = {
var arr = NSMutableArray()
for index in 1...100 {
arr .addObject(NSString(format: "%d", index))
}
return arr
}()
override func viewDidLoad() {
super.viewDidLoad()
initTab()
setUpLeftBtn()
setUpRightBtn()
}
private func setUpRightBtn(){
let nextItem = UIBarButtonItem(title: "add", style: .Plain, target: self, action: "leftBtnClick")
self.navigationItem.leftBarButtonItem = nextItem
}
private func setUpLeftBtn(){
let nextItem = UIBarButtonItem(title: "编辑", style: .Plain, target: self, action:"rightBtnClick")
self.navigationItem.rightBarButtonItem = nextItem
}
func rightBtnClick(){
tabV!.setEditing(!tabV!.editing, animated: true)
if (tabV!.editing == true ) {
self.navigationItem.leftBarButtonItem!.enabled = true
self.navigationItem.rightBarButtonItem!.title = "完成"
}else{
self.navigationItem.leftBarButtonItem!.enabled = false
self.navigationItem.rightBarButtonItem!.title = "编辑"
}
}
func leftBtnClick(){
let row = self.dataArr.count
let indexPath = NSIndexPath(forRow:row,inSection:0)
self.dataArr.addObject("杭州")
self.tabV?.insertRowsAtIndexPaths([indexPath], withRowAnimation: UITableViewRowAnimation.Left)
}
private func initTab(){
tabV = UITableView(frame: self.view.frame, style: .Plain)
tabV!.delegate = self;
tabV!.dataSource = self;
tabV!.setEditing(false, animated: false)
view .addSubview(tabV!)
}
//总行数
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return dataArr.count
}
//加载数据
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let Id = "cellId"
var cell = tableView.dequeueReusableCellWithIdentifier(Id)
if cell == nil{
cell = UITableViewCell(style: .Default, reuseIdentifier: Id)
}
cell?.textLabel?.text = dataArr[indexPath.row] as? String
cell?.accessoryType = UITableViewCellAccessoryType.DetailDisclosureButton
return cell!
}
//选中行
func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
tableView.deselectRowAtIndexPath(indexPath, animated: true)
next()
}
func next(){
let testVC = ZHZTestViewController()
testVC.view.backgroundColor = UIColor.grayColor()
self.navigationController? .pushViewController(testVC, animated: true)
}
func tableView(tableView: UITableView, canEditRowAtIndexPath indexPath: NSIndexPath) -> Bool {
return true
}
func tableView(tableView: UITableView, commitEditingStyle editingStyle: UITableViewCellEditingStyle, forRowAtIndexPath indexPath: NSIndexPath) {
let index = indexPath.row as Int
self.dataArr.removeObjectAtIndex(index)
self.tabV?.deleteRowsAtIndexPaths([indexPath], withRowAnimation: .Right)
}
func tableView(tableView: UITableView, editingStyleForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCellEditingStyle {
return UITableViewCellEditingStyle.Insert
}
func tableView(tableView: UITableView, titleForDeleteConfirmationButtonForRowAtIndexPath indexPath: NSIndexPath) -> String? {
return "删除"
private var tabV:UITableView?
lazy var dataArr:NSMutableArray = {
var arr = NSMutableArray()
for index in 1...100 {
arr .addObject(NSString(format: "%d", index))
}
return arr
}()
override func viewDidLoad() {
super.viewDidLoad()
initTab()
setUpLeftBtn()
setUpRightBtn()
}
private func setUpRightBtn(){
let nextItem = UIBarButtonItem(title: "add", style: .Plain, target: self, action: "leftBtnClick")
self.navigationItem.leftBarButtonItem = nextItem
}
private func setUpLeftBtn(){
let nextItem = UIBarButtonItem(title: "编辑", style: .Plain, target: self, action:"rightBtnClick")
self.navigationItem.rightBarButtonItem = nextItem
}
func rightBtnClick(){
tabV!.setEditing(!tabV!.editing, animated: true)
if (tabV!.editing == true ) {
self.navigationItem.leftBarButtonItem!.enabled = true
self.navigationItem.rightBarButtonItem!.title = "完成"
}else{
self.navigationItem.leftBarButtonItem!.enabled = false
self.navigationItem.rightBarButtonItem!.title = "编辑"
}
}
func leftBtnClick(){
let row = self.dataArr.count
let indexPath = NSIndexPath(forRow:row,inSection:0)
self.dataArr.addObject("杭州")
self.tabV?.insertRowsAtIndexPaths([indexPath], withRowAnimation: UITableViewRowAnimation.Left)
}
private func initTab(){
tabV = UITableView(frame: self.view.frame, style: .Plain)
tabV!.delegate = self;
tabV!.dataSource = self;
tabV!.setEditing(false, animated: false)
view .addSubview(tabV!)
}
//总行数
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return dataArr.count
}
//加载数据
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let Id = "cellId"
var cell = tableView.dequeueReusableCellWithIdentifier(Id)
if cell == nil{
cell = UITableViewCell(style: .Default, reuseIdentifier: Id)
}
cell?.textLabel?.text = dataArr[indexPath.row] as? String
cell?.accessoryType = UITableViewCellAccessoryType.DetailDisclosureButton
return cell!
}
//选中行
func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
tableView.deselectRowAtIndexPath(indexPath, animated: true)
next()
}
func next(){
let testVC = ZHZTestViewController()
testVC.view.backgroundColor = UIColor.grayColor()
self.navigationController? .pushViewController(testVC, animated: true)
}
func tableView(tableView: UITableView, canEditRowAtIndexPath indexPath: NSIndexPath) -> Bool {
return true
}
func tableView(tableView: UITableView, commitEditingStyle editingStyle: UITableViewCellEditingStyle, forRowAtIndexPath indexPath: NSIndexPath) {
let index = indexPath.row as Int
self.dataArr.removeObjectAtIndex(index)
self.tabV?.deleteRowsAtIndexPaths([indexPath], withRowAnimation: .Right)
}
func tableView(tableView: UITableView, editingStyleForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCellEditingStyle {
return UITableViewCellEditingStyle.Insert
}
func tableView(tableView: UITableView, titleForDeleteConfirmationButtonForRowAtIndexPath indexPath: NSIndexPath) -> String? {
return "删除"
}
/**
标题
*/
func tableView(tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
return self.titles[section]
}
/**
索引数组
*/
func sectionIndexTitlesForTableView(tableView: UITableView) -> [String]? {
return self.titles
}
func tableView(tableView: UITableView, willDisplayCell cell: UITableViewCell, forRowAtIndexPath indexPath: NSIndexPath) { let rotationTransform = CATransform3DTranslate(CATransform3DIdentity,200, 50, 0) cell.layer.transform = rotationTransform UIView.animateWithDuration(0.5, delay: 0, options: UIViewAnimationOptions.AllowUserInteraction, animations: { () -> Void in cell.layer.transform = CATransform3DIdentity }, completion: nil) }