实现单元格的插入与删除

在sb中将原来的vc删除 随后拖入navigation controller 将textField拖入 不是拖入到主界面上 而是拖到上面去 如图所示


随后实现插入与删除 在单元格中 限时一个navigation controller 随后设置左按钮的状态 只有编辑和保存的两种状态,随后声明插入和删除该作用于哪些行 随后再实现插入和删除 数组声明为NSMutableArray 数组和列表同步刷新 随后再刷新列表即可 贴上代码

//
//  ViewController.swift
//  NSPredicate的使用
//
//  Created by 金阳 on 16/1/22.
//  Copyright © 2016年 金阳. All rights reserved.
//

import UIKit

class ViewController: UITableViewController,UITextFieldDelegate {

    
    @IBOutlet var txtField: UITextField!
    var listTeam:NSMutableArray!
    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.
//设置导航栏的属性 确定导航栏的编辑按钮 当被点击时 会调用setEditing
        self.navigationItem.rightBarButtonItem = self.editButtonItem()
        self.navigationItem.title = "插入和删除"
        //设置文本框
        self.txtField.hidden = true
        self.txtField.delegate = self
        listTeam = ["浙江","宁波","溪口"];
    }
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
    override func setEditing(editing: Bool, animated: Bool) {
        super.setEditing(editing, animated: animated)
        self.tableView.setEditing(editing, animated: true)
        if(editing) {
            txtField.hidden = false
        }else{
            txtField.hidden = true
        }
    }
    override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return listTeam.count + 1
    }
    override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
        //判断是不是已经到了最后一行
        var b_addCell = (indexPath.row == listTeam.count)
        var cellId = "id"
        var cell = tableView.dequeueReusableCellWithIdentifier(cellId, forIndexPath: indexPath) as UITableViewCell
      
        if(b_addCell == false) {
            cell.accessoryType = .DisclosureIndicator
            cell.textLabel?.text = listTeam[indexPath.row] as! String
        }else{
            txtField.frame = CGRectMake(10, 5, 300, 44)
            txtField.borderStyle = .None
            txtField.placeholder = "Add"
            txtField.text = ""
            cell.contentView.addSubview(txtField)
        }
        return cell
    }
    //声明每一行插入的类型
    override func tableView(tableView: UITableView, editingStyleForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCellEditingStyle {
        if(indexPath.row == listTeam.count){
            return .Insert
        }else{
            return .Delete
        }
    }
    //实现删除或者插入的代理
    override func tableView(tableView: UITableView, commitEditingStyle editingStyle: UITableViewCellEditingStyle, forRowAtIndexPath indexPath: NSIndexPath) {
        //是indexPath的集合
        var indexPaths = [indexPath]
        if(editingStyle == .Delete) {
            //删除对象
            listTeam.removeObjectAtIndex(indexPath.row)
            tableView.deleteRowsAtIndexPaths(indexPaths, withRowAnimation: .Fade)
        }else if(editingStyle == .Insert){
            listTeam.insertObject(txtField.text!, atIndex: listTeam.count)
            tableView.insertRowsAtIndexPaths(indexPaths, withRowAnimation: .Fade)
        }
        //刷新列表
        tableView.reloadData()
    }
    //选择时是否处于高亮的状态
    override func tableView(tableView: UITableView, shouldHighlightRowAtIndexPath indexPath: NSIndexPath) -> Bool {
        if(indexPath.row == listTeam.count) {
        return true
        }
        else{
        return false
    }
    }
    //设置高度
    override func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
        return 50
    }
}


初始状态

编辑状态


插入状态

删除

删除后保存状态


重用队列的使用 因为tableView 是个滚动视图 所以 当有些单元格划出时 不会显示在界面上 因此 将他们保存到重用队列中,随后用一个标示符来进行标示,当再次回到这个单元格要出现的地方时,就判断该重用标示符对应的单元格是否在,在的话就从队列中取出,显示出来,这事苹果的内存管理机制,所以,照着用就好了 

以下这个博客对于重用机制有着详细的介绍

http://www.cnblogs.com/496668219long/p/4471232.html

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值