编辑tableView,其中行是不可变值

时间:2016-04-23 16:31:01

标签: ios swift uitableview

我试图在不可变的值上编辑tableView行。

当我允许在桌面上进行编辑时,我一直得到以下信息:

  

不能在不可变值上使用变异成员:' dataSet'是一个只获得属性

查看Controller.swift

import UIKit

class TableVC: UIViewController, UITableViewDataSource, UITableViewDelegate {

@IBOutlet weak var table: UITableView!

override func viewDidLoad() {
    super.viewDidLoad()

    table.dataSource = self
    table.delegate = self

    NSNotificationCenter.defaultCenter().addObserver(self, selector: #selector(TableVC.reloadTable(_:)), name: "reloadData", object: nil)

    DataHandler.inst.loadData()
   self.navigationItem.leftBarButtonItem = self.editButtonItem()
}

func reloadTable(notif: AnyObject) {
    table.reloadData()
}

func numberOfSectionsInTableView(tableView: UITableView) -> Int {
    return 1
}

func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    return DataHandler.inst.dataSet.count
}

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
    let cellOld = tableView.dequeueReusableCellWithIdentifier("PicsCell") as? PicsCell

    let cell = cellOld != nil ? cellOld! : PicsCell()

    cell.initializeCell(DataHandler.inst.dataSet[indexPath.row])

    return cell
}

func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
    let selectedPic = DataHandler.inst.dataSet[indexPath.row]
    let view = ImageVC(pic: selectedPic)

    presentViewController(view, animated: true, completion: nil)
}



func tableView(tableView: UITableView, commitEditingStyle editingStyle: UITableViewCellEditingStyle, forRowAtIndexPath indexPath: NSIndexPath){


    if editingStyle == UITableViewCellEditingStyle.Delete{


     //Error here saying Cannot use mutating member on immutable value: 'dataSet' is a get-only property   
    //    DataHandler.inst.dataSet.removeAtIndex(indexPath.row)


        tableView.reloadData()


     }


   }

 }

DataHandler.swift

static let inst = DataHandler()

private var _dataSet = [Pics]()

var dataSet: [Pics] {
    return _dataSet
}

func addPic(img: UIImage, title: String, description: String) {

    let imgName = saveMyImage(img)
    let entry = Pics(img: imgName, title: title, description: description)
    _dataSet.append(entry)

    saveData()
    loadData()
}

private func saveData() {
    let data = NSKeyedArchiver.archivedDataWithRootObject(_dataSet)
    NSUserDefaults.standardUserDefaults().setObject(data, forKey: "data")
    NSUserDefaults.standardUserDefaults().synchronize()
}

func loadData() {
    guard let data = NSUserDefaults.standardUserDefaults().objectForKey("data") as? NSData else {
        return
    }
    guard let array = NSKeyedUnarchiver.unarchiveObjectWithData(data) as? [Pics] else {
        return
    }

    _dataSet = array

    NSNotificationCenter.defaultCenter().postNotificationName("reloadData", object: nil)
}

private func saveMyImage(img: UIImage) -> String {
    let imgData = UIImagePNGRepresentation(img)
    let imgPath = "img\(NSDate.timeIntervalSinceReferenceDate()).png"
    let fullPath = getPathForImage(imgPath)

    imgData?.writeToFile(fullPath, atomically: true)
    return imgPath
}

private func getPathForImage(name: String) -> String {
    let paths = NSSearchPathForDirectoriesInDomains(.DocumentDirectory, .UserDomainMask, true)
    let fullPath = paths[0] as NSString
    return fullPath.stringByAppendingPathComponent(name)
}

func imagePathName(name: String) -> UIImage? {
    let path = getPathForImage(name)
    let img = UIImage(named: path)

    return img
   }
}

1 个答案:

答案 0 :(得分:2)

您在此处将dataSet声明为get only属性:

var dataSet: [Pics] {
    return _dataSet
}

如果你想改变它,添加一个setter:

var dataSet: [Pics] {
    get {
        return _dataSet
    }

    set(newSet) {
        _dataSet = newSet
    }

}

或者更改您的体系结构以使_dataSet公开。