【发布时间】:2015-07-07 09:35:38
【问题描述】:
// import UIKit
import Parse
import Foundation
class EditFriendsController: UITableViewController {
var allUsers:NSArray = [] // NSArray *allUsers
var currentUser = PFUser.currentUser()
var friends:NSMutableArray = [] // used in FriendsController
var profileImages = [PFFile]()
override func viewDidLoad() {
super.viewDidLoad()
}
override func viewWillAppear(animated: Bool) {
super.viewWillAppear(animated)
// PFQuery *query = [PFUser query] in objective C
var query = PFUser.query()
query!.orderByAscending("username")
query!.findObjectsInBackgroundWithBlock {
(objects: [AnyObject]?, error: NSError?) -> Void in
if error != nil {
// Log details of the failure
println("Error: \(error!)\(error!.userInfo)")
} else {
// The find succeeded.
println("Successfully retrieved \(objects!.count) scores.")
self.allUsers = objects!
// println("\(self.allUsers)")
self.animateTable() // include self.tableView.reloadData()
}
}
self.currentUser = PFUser.currentUser()
}
// MARK: - Table view animate function
func animateTable() {
tableView.reloadData()
let cells = tableView.visibleCells()
let tableHeight:CGFloat = tableView.bounds.size.height
for transformBefore in cells {
let cell: UITableViewCell = transformBefore as! UITableViewCell
cell.transform = CGAffineTransformMakeTranslation(0, tableHeight)
}
var index = 0
for transformAfter in cells {
let cell: UITableViewCell = transformAfter as! UITableViewCell
UIView.animateWithDuration(1.5, delay: 0.05 * Double(index), usingSpringWithDamping: 0.8, initialSpringVelocity: 0, options: nil, animations: { () -> Void in
cell.transform = CGAffineTransformMakeTranslation(0, 0)
}, completion: nil)
index += 1
}
}
// MARK: - Table view data source
override func numberOfSectionsInTableView(tableView: UITableView) -> Int {
return 1
}
override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return self.allUsers.count
}
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
var cell = tableView.dequeueReusableCellWithIdentifier("Cell", forIndexPath: indexPath) as! EditFriendsProfileViewCell
var user = self.allUsers[indexPath.row] as! PFUser
cell.usernameLabel.text = user.username!
// cell.profileImageView.image = UIImage(named: profileImages[indexPath.row])
if let userPicture = PFUser.currentUser()?["photo"] as? PFFile {
userPicture.getDataInBackgroundWithBlock({
(imageData:NSData?, error:NSError?) -> Void in
var img:UIImageView = UIImageView()
if (error == nil) {
cell.profileImageView.image = UIImage(data: imageData!)
} else {
}
})
}
// image cornerRadius
cell.profileImageView.layer.cornerRadius = 10
cell.profileImageView.clipsToBounds = true
var myFriend = isFriend(user)
cell.accessoryType = myFriend ? .Checkmark : .None
//cell.checkImageView.image = myFriend ? UIImage(named: "checkedFilled.png") : UIImage(named: "checkedWhite.png")
return cell
}
override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
self.tableView.deselectRowAtIndexPath(indexPath, animated: false)
var cell = tableView.cellForRowAtIndexPath(indexPath) as! EditFriendsProfileViewCell
var user = self.allUsers[indexPath.row] as! PFUser
var friendsRelation = self.currentUser!.relationForKey("friendsRelation")
var myFriend = isFriend(user)
if (myFriend) {
// remove process
// 1. Remove the checkmark
cell.accessoryType = UITableViewCellAccessoryType.None
// cell.imageView?.image = UIImage(named: "checkedWhite.png")
// 2. Remove from array of friend
var friend = PFUser()
for friend in self.friends {
if friend.objectId == user.objectId {
self.friends.removeObject(friend)
break;
}
}
// 3. Remove from the backend
friendsRelation.removeObject(user)
} else {
// add them
cell.accessoryType = UITableViewCellAccessoryType.Checkmark
// cell.imageView?.image = UIImage(named: "checkedFilled.png")
self.friends.addObject(user)
friendsRelation.addObject(user)
}
self.currentUser!.saveInBackgroundWithBlock {
(success: Bool, error: NSError?) -> Void in
if (success) {
// The post has been added to the user's likes relation.
} else {
// There was a problem, check error.description
println("Error: \(error!)\(error!.userInfo)")
}
}
}
// MARK: - Table view Helper Methods
func isFriend(user: PFUser!) -> Bool {
var friend = PFUser()
for friend in self.friends {
if friend.objectId == user.objectId {
return true;
}
}
return false;
}
}
尽管所有不同的商店都存储在 Parse 中,但所有个人资料图片都作为同一张图片加载。
请帮帮我。
【问题讨论】:
-
错误是什么?它出现在您的代码中的什么位置?
-
错误是所有个人资料图片都作为一张图片加载我想我想在查询语句中添加一些东西但不确定..
-
你正在设置当前用户的照片,在 cellForRowAtIndexPath 中设置用户?["photo"]
-
哦,我明白了,是的,我猜图片来自当前用户的图片,但不知道为什么只填充当前用户的图片。谢谢 :D 你节省了我的时间 :D,刚刚更改并完美运行 :D,谢谢,Muhammad Adnan
-
将答案标记为已回答。将来可能会对其他人有所帮助。
标签: ios image swift parse-platform tableview