使用NSUserDefaults中的自定义对象保存多个选定的tableViewCell

时间:2015-05-01 20:20:16

标签: ios arrays uitableview swift nsuserdefaults

我创建了一个UITableViewController,其中包含一个团队列表。这些都可以使用accessoryView选择,可以在下图中看到。我们的想法是,每次选择一个单元格时,它都会从teamArray中的(自定义类)添加特定的Team Object到teamSelected数组,当您再次选择它时,它将删除它。当我比较CREATE TABLE RIDERS (RIDER_ID VARCHAR2(4) CONSTRAINT RIDERS_RIDER_ID_PK PRIMARY KEY, FIRSTRIDER VARCHAR2(20) CONSTRAINT RIDERS_FIRSTRIDER_NN NOT NULL, LASTRIDER VARCHAR2(20) CONSTRAINT RIDERS_LASTRIDER_NN NOT NULL, ADDRESS VARCHAR2(25), CITY VARCHAR2(15), STATE CHAR(2), ZIP VARCHAR2(15), PHONE VARCHAR2(25) DATEJOINED DATE, AGENT_ID VARCHAR2(4) CONSTRAINT RIDERS_AGENT_ID_FK REFERENCES AGENTS(AGENT_ID), TEAM_ID VARCHAR2(4) CONSTRAINT RIDERS_TEAM_ID_FK REFERENCES TEAMS(TEAM_ID)); 时,这开始很好,但是当我返回到viewController并从NSUserDefaults加载teamSelected时,它没有用(value == team)标记它们。我发现原因是每次进入此TableViewController时对象都会更改,并从JSON文件加载teamObjects。因此我决定选择cell.accessoryCheck其中id是每个团队的唯一ID。这很有效,现在用(value.id == team.id)正确标记它们。但是现在它似乎没有从teamSelected中删除选定的团队对象?它只是让它超出范围?

我的观点的插图

enter image description here

这是我的代码:

我的阵列

cell.accessoryCheck

自定义团队对象

var teamArray = Array<Team>()
var teamSelected = [Team]()

在viewDidLoad

class Team: NSObject{
var id: Int!
var name: NSString!
var shortname: NSString!


init(id: Int, name:NSString, shortname: NSString) {
    self.id = id
    self.name = name
    self.shortname = shortname

}


required convenience init(coder aDecoder: NSCoder) {
    let id = aDecoder.decodeIntegerForKey("id")
    let name = aDecoder.decodeObjectForKey("name") as! String
    let shortname = aDecoder.decodeObjectForKey("shortname") as! String
    self.init(id: id, name: name, shortname: shortname)
}

func encodeWithCoder(aCoder: NSCoder) {
    aCoder.encodeInteger(id, forKey: "id")
    aCoder.encodeObject(name, forKey: "name")
    aCoder.encodeObject(shortname, forKey: "shortname")
}


}
var userDefaults = NSUserDefaults.standardUserDefaults() if let data = userDefaults.objectForKey("teams") as? NSData { let teamObjects = NSKeyedUnarchiver.unarchiveObjectWithData(data) as! [Team] self.teamSelected = teamObjects self.tableView.reloadData() }

中的

CellForRowAtIndexPath

override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { let cell = tableView.dequeueReusableCellWithIdentifier("teamCell", forIndexPath: indexPath) as! TeamCell if (self.teamSearchController.active) { cell.textLabel?.text = filteredTableData[indexPath.row].name } else { cell.textLabel?.font = UIFont(name: "HelveticaNeue-Light", size: 20) cell.textLabel?.text = self.teamArray[indexPath.row].name as String } let team = self.teamArray[indexPath.row] as Team var removed = false for (index, value) in enumerate(self.teamSelected) { if (value.id == team.id) { cell.accessoryView = cell.accessoryCheck removed = true } } if (!removed) { cell.accessoryView = cell.accessoryUncheck } cell.selectionStyle = UITableViewCellSelectionStyle.None return cell }

didSelectRowAtIndexPath

0 个答案:

没有答案