2016-04-23 43 views
0

我想创建一个tableView的用户从我的Parse数据库是在同一类(在学校)。所有用户都必须拥有用户名,但并不是所有用户都会为其提供全名或设置个人资料图片。我用这个代码:添加项目到特定表格查看行如果可用

let studentsQuery = PFQuery(className:"_User") 
studentsQuery.whereKey("objectId", containedIn: studentsArray! as! [AnyObject]) 

let query2 = PFQuery.orQueryWithSubqueries([studentsQuery]) 

query2.findObjectsInBackgroundWithBlock { 
    (results: [PFObject]?, error: NSError?) -> Void in 

    if error != nil { 

     // Display error in tableview 

    } else if results! == [] { 

     spinningActivity.hideAnimated(true) 

     print("error") 

    } else if results! != [] { 

     if let objects = results { 

      for object in objects { 

       if object.objectForKey("full_name") != nil { 

        let studentName = object.objectForKey("full_name")! as! String 

        self.studentNameResults.append(studentName) 


       } 

       if object.objectForKey("username") != nil { 

        let studentUsername = object.objectForKey("username")! as! String 

        self.studentUsernameResults.append(studentUsername) 

       } 

       if object.objectForKey("profile_picture") != nil { 

        let studentProfilePictureFile = object.objectForKey("profile_picture") as! PFFile 

        studentProfilePictureFile.getDataInBackgroundWithBlock({ (image: NSData?, error: NSError?) in 

         if error == nil { 

          let studentProfilePicture : UIImage = UIImage(data: image!)! 
          self.studentProfilePictureResults.append(studentProfilePicture) 

         } else { 

          print("Can't get profile picture") 

          // Can't get profile picture 

         } 

         self.studentsTableView.reloadData() 

        }) 

        spinningActivity.hideAnimated(true) 

       } else { 

        // no image 

       } 

      } 
     } 
} else { 

    spinningActivity.hideAnimated(true) 

    print("error") 

} 
} 

此代码工作正常,如果所有的用户都有一个用户名,FULL_NAME,和profile_picture。但是,我不知道如何获取用户的用户名的tableView,并且只有当用户有图片时才将用户的姓名或图片添加到用户的相应tableViewCell。这里是我的tableView是如何配置:

func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int { 

     return studentUsernameResults.count 

} 


func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { 

     let cell = tableView.dequeueReusableCellWithIdentifier("studentsCell", forIndexPath: indexPath) as! StudentsInClassInformationTableViewCell 

     cell.studentProfilePictureImageView.layer.cornerRadius = cell.studentProfilePictureImageView.frame.size.width/2 
     cell.studentProfilePictureImageView.clipsToBounds = true 

     cell.studentProfilePictureImageView.image = studentProfilePictureResults[indexPath.row] 

     cell.studentUsernameLabel.text = studentUsernameResults[indexPath.row] 

     cell.studentNameLabel.text = studentNameResults[indexPath.row] 


     return cell 

} 

studentProfilePictureResultsstudentUsernameResults,并提出从解析拉到用户的图片,用户名和姓名的结果阵列studentNameResults。如果用户没有个人资料图片,我会收到错误Index is out of range。显然,这意味着有三个名字,三个用户名和只有两个图片,Xcode不知道如何配置单元格。我的问题:如何设置一个用户的用户名的tableView,并将他们的名字和个人资料图片放在同一个单元格中,只要他们有一个?

+0

不要存储单独的数组。存储一个PFObjects数组,我建议你使用[username(string):UIImage)字典作为照片 – Paulw11

+0

@ Paulw11当我查询用户的用户名,名字和图片时,我得到了所有的结果曾经,就像你正在谈论的那样(将结果存储在一个数组中)。我会如何将用户名,姓名和图片存储在一起?我还没有玩过多少字典,但我认为他们只能存储两(2)个结果。我如何储存所有三(3)? –

回答

1

试图将不同的属性存储在不同的数组中会是一个问题,因为正如您发现的那样,最终会遇到某个用户没有属性的问题。您可以使用一系列可选项,以便您可以将缺少的属性存储为nil,但将PFObject本身存储在单个数组中并访问cellForRowAtIndexPath中的属性比分离属性要简单得多。

由于提取照片需要单独的异步操作,因此可以单独存储它。您可以使用由用户ID编制索引的字典,而不是使用阵列来存储检索到的照片,这会有相同的排序问题;尽管对于大量的学生来说,使用诸如SDWebImage之类的东西来下载cellForRowAtIndexPath中所需的照片可能更有效率。

// these are instance properties defined at the top of your class 
var students: [PFObject]? 
var studentPhotos=[String:UIImage]() 

// This is in your fetch function 
let studentsQuery = PFUser.Query() 
    studentsQuery.whereKey("objectId", containedIn: studentsArray! as! [AnyObject]) 

let query2 = PFQuery.orQueryWithSubqueries([studentsQuery]) 

query2.findObjectsInBackgroundWithBlock { 
    (results: [PFObject]?, error: NSError?) -> Void in 
    guard (error == nil) else { 
     print(error) 
     spinningActivity.hideAnimated(true) 
     return 
    } 

    if let results = results { 
     self.students = results 
      for object in results { 
       if let studentProfilePictureFile = object.objectForKey("profile_picture") as? PFFile { 
        studentProfilePictureFile.getDataInBackgroundWithBlock({ (image: NSData?, error: NSError?) in 
        guard (error != nil) else { 
         print("Can't get profile picture: \(error)") 
         return 
        } 

       if let studentProfilePicture = UIImage(data: image!) { 
        self.studentPhotos[object["username"]!]=studentProfilePicture 
       } 
      } 
    } 
    spinningActivity.hideAnimated(true) 
    self.tableview.reloadData() 
} 

func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int { 
    if self.students != nil { 
     return self.students!.count 
    } 
    return 0 
} 

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { 

    let cell = tableView.dequeueReusableCellWithIdentifier("studentsCell", forIndexPath: indexPath) as! StudentsInClassInformationTableViewCell 

    cell.studentProfilePictureImageView.layer.cornerRadius = cell.studentProfilePictureImageView.frame.size.width/2 
    cell.studentProfilePictureImageView.clipsToBounds = true 

    let student = self.students[indexPath.row] 

    if let studentPhoto = self.studentPhotos[student["username"]!] { 
     cell.studentProfilePictureImageView.image = studentProfilePictureResults[indexPath.row] 
    } else { 
     cell.studentProfilePictureImageView.image = nil 
    } 

    cell.studentUsernameLabel.text = student["username"]! 

    if let fullName = student["full_name"] { 
     cell.studentNameLabel.text = fullName 
    } else { 
     cell.studentNameLabel.text = "" 
    return cell 
} 

其他一些指针;

  • 使用_分隔字段名中的单词在iOS世界中并不真正使用; camelCase是首选,所以fullName而不是full_name
  • 它看起来像您的解析查询可能会更有效率,如果您有一个class字段或引用对象,以便您不需要提供其他类成员的数组。
+0

你是救命恩人,保罗!我必须做一些调试,并根据我遇到的一些问题在几天内编辑您的答案,但总体而言,这是一种干净而简单的方法!再次感谢! –

相关问题