2015-06-04 33 views
1

因此,我有一个显示课程的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 
    } 

回答

0

不要尝试搜索单元格。那么,你可以,但不是你想要的 - 我会回来的。

您当前的代码正在创建新的单元格,而没有找到现有的单元格,因此无法工作。

你真正应该做的是存储返回对象数组qObjects,然后当你配置单元格显示检查数组是否包含当前单元格的对象。如果是,请勾选它,否则删除勾号。现在

,如果qObjects负载发生时显示的视图之后,你有2种选择:

  1. 重载表视图
  2. 更新只是有形的物品

选项2的显然会更好,特别是如果用户可能正在滚动列表。要做到这一点,您希望使用在表视图上调用indexPathsForVisibleRows返回的数组。然后,迭代该列表,获取关联的对象并检查它是否在qObjects中,然后使用cellForRowAtIndexPath:获取显示的单元格并更新它。

+0

感谢您的回答。事情是,我正在使用PFQueryTableViewController类,它为我完成所有配置。任何想法我怎么能在这个班上做到这一点?我知道,我可以用对象获得tableview中的对象!,但我不知道如何获取对象的单元格... – ppgcc74

+0

相同,它的全部索引路径 – Wain

+0

好吧,所以我需要使用'dequeueReusableCellWithIdentifier(“ courseCell“,forIndexPath:indexPath)',对吗?但是,我如何获得我的索引路径?我有我的tableview中的部分。所以'NSIndexPath(forItem ...)',但我怎么得到运行它在每一节的每一行? (我需要将它们与我的qObjects进行比较,对吧?) – ppgcc74