从特定行设置动态自定义单元格
Set dynamic Custom Cell from certain row
我正在开发一个社交功能,人们可以在其中对图片发表评论。
唯一的动态单元格也是评论单元格。我正在为此使用 Parse。
如何在每个评论单元格上获得不同的评论?
我尝试访问 indexPath.row,但出现错误:'*** -[__NSArrayM objectAtIndex:]: index 2 beyond bounds [0 .. 1]'
现在使用自定义 NSIndexPath
但我只能手动访问 forRow
方法。结果评论都一样
- 第 0 行和第 1 行正在运行。
- 使用故事板。
userComments
是正在加载评论的可变数组。
println(comments)
给了我 2 个相同的对象。
override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int{
return userComments.count + 2
}
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
if indexPath.row == 0 {
let Postcell:PostTableViewCell = tableView.dequeueReusableCellWithIdentifier("imageCell") as PostTableViewCell
....
return Postcell
}
if indexPath.row == 1 {
let likeCell:likedTableViewCell = tableView.dequeueReusableCellWithIdentifier("likeCell") as likedTableViewCell
....
return likeCell
}else {
let commentCell:commentTableViewCell = tableView.dequeueReusableCellWithIdentifier("commentCell") as commentTableViewCell
let commentIndex:NSIndexPath = NSIndexPath(forRow: 0, inSection: 0)
let comment:PFObject = userComments.objectAtIndex(commentIndex.row) as PFObject
println(comment)
// Comment Label
commentCell.commentLabel.text = comment.objectForKey("content") as String!
commentCell.userImageView.image = UIImage(named: "dummy")
return commentCell
}
}
发生这种情况是因为在你的最后一点中你总是在解析中请求第 0 行第 0 节,你将需要这样的东西:
else {
let commentCell:commentTableViewCell = tableView.dequeueReusableCellWithIdentifier("commentCell") as commentTableViewCell
//indexPath.row is the actual row of the table,
//so you will have for table row 2 parse row 0, for 3 row 1 and so on
let commentIndex:NSIndexPath = NSIndexPath(forRow: indexPath.row-2, inSection: 0)
let comment:PFObject = userComments.objectAtIndex(commentIndex.row) as PFObject
println(comment)
// Comment Label
commentCell.commentLabel.text = comment.objectForKey("content") as String!
commentCell.userImageView.image = UIImage(named: "dummy")
return commentCell
}
我正在开发一个社交功能,人们可以在其中对图片发表评论。 唯一的动态单元格也是评论单元格。我正在为此使用 Parse。
如何在每个评论单元格上获得不同的评论?
我尝试访问 indexPath.row,但出现错误:'*** -[__NSArrayM objectAtIndex:]: index 2 beyond bounds [0 .. 1]'
现在使用自定义 NSIndexPath
但我只能手动访问 forRow
方法。结果评论都一样
- 第 0 行和第 1 行正在运行。
- 使用故事板。
userComments
是正在加载评论的可变数组。println(comments)
给了我 2 个相同的对象。override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int{ return userComments.count + 2 } override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { if indexPath.row == 0 { let Postcell:PostTableViewCell = tableView.dequeueReusableCellWithIdentifier("imageCell") as PostTableViewCell .... return Postcell } if indexPath.row == 1 { let likeCell:likedTableViewCell = tableView.dequeueReusableCellWithIdentifier("likeCell") as likedTableViewCell .... return likeCell }else { let commentCell:commentTableViewCell = tableView.dequeueReusableCellWithIdentifier("commentCell") as commentTableViewCell let commentIndex:NSIndexPath = NSIndexPath(forRow: 0, inSection: 0) let comment:PFObject = userComments.objectAtIndex(commentIndex.row) as PFObject println(comment) // Comment Label commentCell.commentLabel.text = comment.objectForKey("content") as String! commentCell.userImageView.image = UIImage(named: "dummy") return commentCell } }
发生这种情况是因为在你的最后一点中你总是在解析中请求第 0 行第 0 节,你将需要这样的东西:
else {
let commentCell:commentTableViewCell = tableView.dequeueReusableCellWithIdentifier("commentCell") as commentTableViewCell
//indexPath.row is the actual row of the table,
//so you will have for table row 2 parse row 0, for 3 row 1 and so on
let commentIndex:NSIndexPath = NSIndexPath(forRow: indexPath.row-2, inSection: 0)
let comment:PFObject = userComments.objectAtIndex(commentIndex.row) as PFObject
println(comment)
// Comment Label
commentCell.commentLabel.text = comment.objectForKey("content") as String!
commentCell.userImageView.image = UIImage(named: "dummy")
return commentCell
}