删除 UITableViewCell 并与 Plist 同步

Delete UITableViewCell and synchronize with Plist

我有一个 class 作为

class DropOffFrequentVC: UIViewController {

    @IBOutlet weak var tblView: UITableView!

    var googleDicCount : Int?
    var foursquareDicCount : Int?
    var favoriteGooglelocations  = [Int:GoogleItems]()
    var favoriteFourSquarelocations = [Int:FourSquareItems]()

我有一个 UITableViewCell 作为

 func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {

     .....

        if indexPath.section == 0 {

            cell.placeLbl.text = favoriteGooglelocations[indexPath.row]!.address
            cell.distanceLbl.text = favoriteGooglelocations[indexPath.row]!.distance
            cell.cityLbl.text =  favoriteGooglelocations[indexPath.row]!.name
            cell.favoriteBtn.tag = indexPath.row;
            cell.favoriteBtn.addTarget(self, action: "DeleteGoogleFavorite:", forControlEvents: UIControlEvents.TouchUpInside)
            cell.favoriteBtn.backgroundColor = UIColor.greenColor()


        } else if indexPath.section == 1 {

            cell.cityLbl.text = favoriteFourSquarelocations[indexPath.row]!.name
            cell.placeLbl.text = favoriteFourSquarelocations[indexPath.row]!.address
            cell.distanceLbl.text = favoriteFourSquarelocations[indexPath.row]!.distance
            cell.favoriteBtn.tag = indexPath.row;
            cell.favoriteBtn.addTarget(self, action: "DeleteFourSquareFavorite:", forControlEvents: UIControlEvents.TouchUpInside)
            cell.favoriteBtn.backgroundColor = UIColor.greenColor()

        }

        return cell

    }

关于那些我想删除单元格的 favoriteBtn 操作,代码如下

 func DeleteGoogleFavorite(sender:UIButton){

  favoriteGooglelocations.removeValueForKey(sender.tag)
    let paths = NSSearchPathForDirectoriesInDomains(NSSearchPathDirectory.DocumentDirectory, NSSearchPathDomainMask.UserDomainMask, true)
    let documentDirectory = paths[0] as! String
    let path = documentDirectory.stringByAppendingPathComponent("Favorites.plist")
    let succeed = NSKeyedArchiver.archiveRootObject(favoriteGooglelocations, toFile: path)
    println(succeed)
    loadFromPlist()
    self.tblView.deleteRowsAtIndexPaths([sender.tag], withRowAnimation:UITableViewRowAnimation.Automatic)
  }

func DeleteFourSquareFavorite(sender:UIButton){

    favoriteFourSquarelocations.removeValueForKey(sender.tag)
    let paths = NSSearchPathForDirectoriesInDomains(NSSearchPathDirectory.DocumentDirectory, NSSearchPathDomainMask.UserDomainMask, true)
    let documentDirectory = paths[0] as! String
    let path = documentDirectory.stringByAppendingPathComponent("FourSquareFavorites.plist")
    let succeed = NSKeyedArchiver.archiveRootObject(favoriteFourSquarelocations, toFile: path)
    println(succeed)
    loadFromPlist()
    self.tblView.deleteRowsAtIndexPaths([sender.tag], withRowAnimation:UITableViewRowAnimation.Automatic)


}

可能是我对上述方法有问题..我不知道如何删除 UITableViewCell 并根据需要更新 plist..我如何实现这个?剩下的代码是如下

   func loadFromPlist(){


        let paths = NSSearchPathForDirectoriesInDomains(NSSearchPathDirectory.DocumentDirectory, NSSearchPathDomainMask.UserDomainMask, true)

        let documentDirectory = paths[0] as! String

        let  googleFavouritepath = documentDirectory.stringByAppendingPathComponent("Favorites.plist")

        var googleDic = NSKeyedUnarchiver.unarchiveObjectWithFile(googleFavouritepath) as? [Int:GoogleItems]

        googleDicCount = googleDic?.count

        if(googleDic != nil){
            for (key,value) in googleDic!{
                let item = value

                println(item.name)
                println(item.address)

                var newLocation = GoogleItems()
                newLocation.name = item.name
                newLocation.address = item.address
                newLocation.distance = item.distance

                favoriteGooglelocations[key] = newLocation
               // self.locations.append(newLocation)

            }

        }

        let  foursquareFavouritepath = documentDirectory.stringByAppendingPathComponent("FourSquareFavorites.plist")
        var fourSquareDic = NSKeyedUnarchiver.unarchiveObjectWithFile(foursquareFavouritepath) as? [Int:FourSquareItems]
        foursquareDicCount = fourSquareDic?.count
        if(fourSquareDic != nil){
            for (key,value) in fourSquareDic!{
                let item = value
                println(item.name)
                println(item.address)
                println(item.isStar)

                var newLocation = FourSquareItems()
                newLocation.name = item.name
                newLocation.address = item.address
                newLocation.distance = item.distance
                favoriteFourSquarelocations[key] = newLocation
                //self.foursquarelocations.append(newLocation)

            }

        }

    }

我在行上有如下错误如

func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int{

        if section == 0{

            if let temp = googleDicCount{

                return googleDicCount! //this line gives me crash

            }else{

                return 0

            }

        }else if section == 1{
            if let temp = foursquareDicCount{

                return foursquareDicCount!

            }else{

                return 0

            }

        }

        return 1
    }

GrabTaxi[603:9745] -[__NSCFNumber row]: unrecognized selector sent to instance 0xb000000000000003 2015-06-08 09:30:18.045 GrabTaxi[603:9745] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSCFNumber row]: unrecognized selector sent to instance 0xb000000000000003'

你的错误在这里

self.tblView.deleteRowsAtIndexPaths([sender.tag], withRowAnimation:UITableViewRowAnimation.Automatic)

sender.tag is not type of NSIndexPath

正确的代码应该是

    let indexPath = NSIndexPath(forRow: sender.tag, inSection: yoursection)//Here yoursection maybe 0 or 1,it depends on your function
    self.tableView.beginUpdates()
    self.tableView.deleteRowsAtIndexPaths([indexPath], withRowAnimation: UITableViewRowAnimation.Automatic)
    self.tableView.endUpdates()

不确定您是否还有其他错误。试试