Ios 从特定行设置动态自定义单元格

Ios 从特定行设置动态自定义单元格,ios,uitableview,swift,parse-platform,nsindexpath,Ios,Uitableview,Swift,Parse Platform,Nsindexpath,我正在制作一个社交功能,人们可以在其中评论图片。 唯一的动态单元格也是注释单元格。我正在使用Parse进行此操作 如何在每个注释单元格上获得不同的注释? 我尝试访问indexPath.row,但它给了我一个错误:“***-[\uu nsarray-objectAtIndex:]:索引2超出边界[0..1]” 现在正在玩一个自定义的nsindepath,但我只设法手动访问forRow方法。结果是评论都是一样的 override func tableView(tableView: UITableVi

我正在制作一个社交功能,人们可以在其中评论图片。 唯一的动态单元格也是注释单元格。我正在使用Parse进行此操作

如何在每个注释单元格上获得不同的注释? 我尝试访问indexPath.row,但它给了我一个错误:
“***-[\uu nsarray-objectAtIndex:]:索引2超出边界[0..1]”

现在正在玩一个自定义的
nsindepath
,但我只设法手动访问
forRow
方法。结果是评论都是一样的

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行和第1行正在工作
  • 使用故事板
  • userComments
    是加载注释的可变数组
  • println(comments)
    给了我两个相同的对象

    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
}