删除UITableViewCell并与Plist同步

时间:2015-06-08 03:54:10

标签: ios uitableview swift plist

我有一个班级

.galleria-thumbnails-container {
  bottom: 0 !important;
  height: 149px !important;
}/*on line 1771*/

我有一个class DropOffFrequentVC: UIViewController { @IBOutlet weak var tblView: UITableView! var googleDicCount : Int? var foursquareDicCount : Int? var favoriteGooglelocations = [Int:GoogleItems]() var favoriteFourSquarelocations = [Int:FourSquareItems]()

UITableViewCell

在那些favoriteBtn动作上,我想删除单元格,代码为

 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

    }

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

 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)


}

我在行上有以下错误

   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)

            }

        }

    }
  

GrabTaxi [603:9745] - [__ NSCFNumber行]:无法识别的选择器发送到   instance 0xb000000000000003 2015-06-08 09:30:18.045 GrabTaxi [603:9745]   ***由于未捕获的异常终止应用程序' NSInvalidArgumentException',原因:' - [__ NSCFNumber行]:   无法识别的选择器发送到实例0xb000000000000003'

1 个答案:

答案 0 :(得分:0)

您的错误在这里

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

sender.tag不是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()

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