因此,我有一个显示课程的tableView。用户可以在这些课程(单元格)上设置复选标记,并将它们保存在他的PFUser对象中,作为与课程类(所有课程存储在其中)的关系。 我的问题是,如何勾选用户之前已经保存的课程。解析和Swift。从解析关系设置tableViewCell附件类型
这是我的尝试,但我不知道如何继续。我如何获得具有特定标签的单元格? (或者是有一个更好的办法?)
let courseRel = PFUser.currentUser()?.relationForKey("usercourses")
let query = courseRel!.query()
let qObjects :Array = query!.findObjects()!
println(qObjects)
for var qObjectsCount = qObjects.count; qObjectsCount > 0; --qObjectsCount {
var qAnObject: AnyObject = qObjects[qObjectsCount - 1]
var courseName = qAnObject["coursename"]
println(courseName)
if let cell: AnyObject? = tableView.dequeueReusableCellWithIdentifier("courseCell"){
}
}
编辑:该代码是我重写viewDidLoad中
EDIT2:符合
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath, object: PFObject?) -> PFTableViewCell {
var cell = tableView.dequeueReusableCellWithIdentifier("courseCell") as! PFTableViewCell!
if cell == nil {
cell = PFTableViewCell(style: UITableViewCellStyle.Default, reuseIdentifier: "courseCell")
}
let courseRel = PFUser.currentUser()?.relationForKey("usercourses")
let query = courseRel!.query()
let qObjects :Array = query!.findObjects()!
// Extract values from the PFObject to display in the table cell
if let courseName = object?["coursename"] as? String {
cell?.textLabel?.text = courseName
if contains(qObjects, object) {
cell?.accessoryType = UITableViewCellAccessoryType.Checkmark
}
}
return cell
}
错误,如果包含(QObject对象,对象){ '
通用参数'S.Generator.Element'不能绑定到非@objc协议类型'AnyObject'
EDIT3:
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath, object: PFObject?) -> PFTableViewCell {
var cell = tableView.dequeueReusableCellWithIdentifier("courseCell") as! PFTableViewCell!
if cell == nil {
cell = PFTableViewCell(style: UITableViewCellStyle.Default, reuseIdentifier: "courseCell")
}
let courseRel = PFUser.currentUser()?.relationForKey("usercourses")
let query = courseRel!.query()
let qObjects :Array = query!.findObjects()!
// Extract values from the PFObject to display in the table cell
if let courseName = object?["coursename"] as? String {
cell?.textLabel?.text = courseName
cell.tintColor = UIColor.blackColor()
}
if contains(qObjects, { $0 === object }) {
cell?.accessoryType = UITableViewCellAccessoryType.Checkmark
self.selectedRows.addIndex(indexPath.row)
}else{
cell?.accessoryType = UITableViewCellAccessoryType.None
}
return cell
}
EDIT4:(工作代码)
在类:
// Initializing qObject variable
var qObjects :Array<AnyObject> = []
在我objectsDidLoad:
// Get PFObjects for the checkmarks on courses the currentUser has already selected before
let courseRel = PFUser.currentUser()?.relationForKey("usercourses")
let query = courseRel!.query()
qObjects = query!.findObjects()!
在我的tableView( cellForRowAtIndex路径):
// Extract values from the PFObject to display in the table cell
if contains(qObjects, { $0 === object }) {
cell.accessoryType = UITableViewCellAccessoryType.Checkmark
self.selectedRows.addIndex(indexPath.row)
} else {
cell.accessoryType = UITableViewCellAccessoryType.None
}
感谢您的回答。事情是,我正在使用PFQueryTableViewController类,它为我完成所有配置。任何想法我怎么能在这个班上做到这一点?我知道,我可以用对象获得tableview中的对象!,但我不知道如何获取对象的单元格... – ppgcc74
相同,它的全部索引路径 – Wain
好吧,所以我需要使用'dequeueReusableCellWithIdentifier(“ courseCell“,forIndexPath:indexPath)',对吗?但是,我如何获得我的索引路径?我有我的tableview中的部分。所以'NSIndexPath(forItem ...)',但我怎么得到运行它在每一节的每一行? (我需要将它们与我的qObjects进行比较,对吧?) – ppgcc74