【发布时间】:2016-03-03 23:23:10
【问题描述】:
我有一个 tableView,我想在其中显示我的 cmets,例如 instagram 和许多其他社交网络的表现。帖子下方3 cmets,来一张图解释一下:
正如您从该图片中看到的那样,在顶部是帖子,然后是标题,最后是 3 个最新的 cmets。
我在我的应用中添加了一个评论系统,当我点击评论图标时,我可以查看所有的 cmets 并被重定向到新的 VC。
当我添加标题时,每个帖子都有一个 UUID,所以我认为这可以在某种程度上帮助将 cmets 连接到帖子!
但现在我正在努力实现这一目标。
到目前为止,我在帖子下方的故事板中添加了 3 个 UIlabels,然后我为这些标签创建了一个集合出口。现在在我的表格视图中,我有一个获取最新 3 cmets 的查询:
var commentUsername = [String]()
var commentArray = [String]()
let Commentquery = PFQuery(className: "Comments")
Commentquery.whereKey("toPost", containedIn: self.uuidArray)
Commentquery.addDescendingOrder("createdAt")
Commentquery.limit = 3
Commentquery.findObjectsInBackgroundWithBlock({ (objects:[PFObject]?, error:NSError?) -> Void in
if error == nil {
self.commentUsername.removeAll(keepCapacity: false)
self.commentArray.removeAll(keepCapacity: false)
for object in objects! {
self.commentArray.append(object.objectForKey("comment") as! String)
self.commentUsername.append(object.objectForKey("username") as! String)
}
tableView.reloadData
}
})
然后这是我在 CellForRowAtIndexPath 中显示它们的方式:
var counter = (indexPath.row * 3)
let cell = tableView.dequeueReusableCellWithIdentifier("cell") as! NewsFeedTableViewCell
for comments in cell.threeComments{
comments.text = commentArray[counter++]
comments.sizeToFit()
}
在“cmets.text = commentArray[counter++]”那一行,我得到数组索引超出范围。
我猜我收到了这个错误,因为它要么返回 nil,因为那张照片没有 cmets,要么没有至少 3 个 cmets 来填充 UILabel ......这就是我有一段时间遇到的困难现在,如果有人知道如何修复或正确可能的“可选”查询或其他东西会很棒!
如需进一步解释,请在下方评论。
提前致以最诚挚的问候和感谢!
完整代码:
var usernameArray = [String]()
var profilePicture = [PFFile]()
var dateArray = [NSDate?]()
var postArray = [PFFile]()
var titleArray = [String]()
var uuidArray = [String]()
var followArray = [String]()
var commentUsername = [String]()
var commentArray = [String]()
override func viewDidLoad() {
super.viewDidLoad()
loadPosts()
}
func loadPosts() {
let followQuery = PFQuery(className: "Follows")
followQuery.whereKey("follower", equalTo: PFUser.currentUser()!.username!)
followQuery.findObjectsInBackgroundWithBlock ({ (objects:[PFObject]?, error:NSError?) -> Void in
if error == nil {
self.followArray.removeAll(keepCapacity: false)
for object in objects! {
self.followArray.append(object.valueForKey("following") as! String)
}
self.followArray.append(PFUser.currentUser()!.username!)
let query = PFQuery(className: "Posts")
query.whereKey("username", containedIn: self.followArray)
query.limit = self.page
query.addDescendingOrder("createdAt")
query.findObjectsInBackgroundWithBlock({ (objects:[PFObject]?, error:NSError?) -> Void in
if error == nil {
self.usernameArray.removeAll(keepCapacity: false)
self.profilePicture.removeAll(keepCapacity: false)
self.dateArray.removeAll(keepCapacity: false)
self.postArray.removeAll(keepCapacity: false)
self.titleArray.removeAll(keepCapacity: false)
self.uuidArray.removeAll(keepCapacity: false)
for object in objects! {
self.usernameArray.append(object.valueForKey("username") as! String)
self.profilePicture.append(object.valueForKey("profilePicture") as! PFFile)
self.dateArray.append(object.createdAt)
self.postArray.append(object.valueForKey("image") as! PFFile)
self.titleArray.append(object.valueForKey("caption") as! String)
self.uuidArray.append(object.valueForKey("uuid") as! String)
}
let Commentquery = PFQuery(className: "Comments")
Commentquery.whereKey("toPost", containedIn: self.uuidArray)
Commentquery.addDescendingOrder("createdAt")
Commentquery.limit = 3
Commentquery.findObjectsInBackgroundWithBlock({ (objects:[PFObject]?, error:NSError?) -> Void in
if error == nil {
self.commentUsername.removeAll(keepCapacity: false)
self.commentArray.removeAll(keepCapacity: false)
for object in objects! {
self.commentArray.append(object.objectForKey("comment") as! String)
self.commentUsername.append(object.objectForKey("username") as! String)
}
self.tableView.reloadData()
}else {
print(error?.localizedDescription)
}
})
} else {
print(error!.localizedDescription)
}
})
} else {
print(error!.localizedDescription)
}
})
}
override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return 1
}
override func numberOfSectionsInTableView(tableView: UITableView) -> Int {
return uuidArray.count
}
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell
{
var counter = (indexPath.row * 3)
let cell = tableView.dequeueReusableCellWithIdentifier("cell") as! NewsFeedTableViewCell
for comments in cell.threeComments{
comments.text = commentArray[counter++]
comments.sizeToFit()
}
}
}
【问题讨论】:
-
更好的方法是使用 2D 数组作为 commentUsername,然后您可以直接遍历事物,而不是假设每个集合中的硬编码数量
标签: ios swift parse-platform tableview