2015-02-11 59 views
0

我正在製作一個社交功能,讓人們可以在圖片上發表評論。 唯一的動態單元格也是註釋單元格。我爲此使用了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 
        } 
    } 
    

回答

2

即在最後一位,因爲發生的事情你總是要求在解析行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 
} 
+0

你的要命!感謝您的好解釋! – SwingerDinger 2015-02-11 11:15:58

+0

你很好:) – 2015-02-11 12:04:24