2016-11-17 68 views
0

请看截图。 ViewControllerrepliesTableView, replyTextField and replyButtonrepliesTableView cell被称为ReplyCell。在ReplyCell中,有一个commentTableView列出该答复的所有评论,并添加textfField, a commentButton以添加新评论。如何从viewcontroller访问tableview单元格的属性?

添加新回复和新评论时出现问题。我想我需要使comments arrayReplyCell为空时点击Reply button。我怎样才能做到这一点?我不知道如何从根ViewController访问comments array

确切的问题:单击commentButton时,每个单元格中的所有注释都加倍。点击replyButton后,评论去了错误的单元格。

enter image description hereenter image description here

Code: 



import UIKit 
import Firebase 

class TopicForumVC: UIViewController, UITableViewDelegate, UITableViewDataSource, UITextFieldDelegate { 

@IBOutlet weak var topicNameLabel: UILabel! 
@IBOutlet weak var replyNumberLabel: UILabel! 

@IBOutlet weak var repliesTableView: UITableView! 

@IBOutlet weak var replyTextField: UITextField! 


var topicName:String? 

var firstKey:String? 

var secondKey:String? 

var replies = [String]() 

var replyButtonTapped = false 

override func viewDidLoad() { 
    super.viewDidLoad() 

    repliesTableView.delegate = self 
    repliesTableView.dataSource = self 

    replyTextField.delegate = self 

} 

override func viewDidAppear(_ animated: Bool) { 

    topicNameLabel.text = self.topicName 

    loadReplies() 
} 

func loadReplies() { 

    self.replies = [] 

    DataService.ds.Categories_Base.child(self.firstKey!).child("Topics").observe(.value, with:{(snapshot) in 

     if let snapshots = snapshot.children.allObjects as? [FIRDataSnapshot] { 

      for snap in snapshots { 

       if let topicDict = snap.value as? Dictionary<String,AnyObject> { 

        if let topic = topicDict["text"] as? String { 

         if topic == self.topicName { 

          self.secondKey = snap.key 

          UserDefaults.standard.setValue(snap.key, forKey: Key_SecondKey) 

          if let replyDict = topicDict["replies"] as? Dictionary<String,AnyObject> { 

           for eachDict in replyDict { 

            if let textDict = eachDict.value as? Dictionary<String,AnyObject> { 

             if let reply = textDict["text"] as? String { 

              self.replies.append(reply) 
              self.replyNumberLabel.text = String(self.replies.count) 
             } 
            } 
           } 
          } 
         } 
        } 
       } 
      } 

      self.repliesTableView.reloadData() 
     } 

    }) 

} 



func numberOfSections(in tableView: UITableView) -> Int { 
    return 1 
} 

func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { 

    return replies.count 
} 

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { 

    if let cell = tableView.dequeueReusableCell(withIdentifier: "ReplyCell") as? ReplyCell { 

     let reply = replies[indexPath.row] 

      cell.configureReplyCell(reply: reply) 

      return cell 

    } else { 

     return UITableViewCell() 
    } 

} 


@IBAction func replyButtonTapped(_ sender: Any) { 

    replyButtonTapped = true 

    if let reply = replyTextField.text, reply != "" { 

     self.replies = [] 

     DataService.ds.Categories_Base.child(self.firstKey!).child("Topics").child(self.secondKey!).child("replies").childByAutoId().child("text").setValue(reply) 

     self.repliesTableView.reloadData() 

     let i = replies.count 

     for n in 0..<i { 

      let indexPath = IndexPath(row: n, section: 1) 

      let cell = repliesTableView.cellForRow(at: indexPath) as! ReplyCell 

      cell.comments = [] 

      cell.repliesToReplyTableView.reloadData() 


     } 

     self.replyTextField.text = "" 

     self.replyButtonTapped = false 
    } 

    } 

}

import UIKit 
import Firebase 


class ReplyCell: UITableViewCell,UITableViewDataSource,UITableViewDelegate, UITextFieldDelegate { 


@IBOutlet weak var replyTextView: UITextView! 

@IBOutlet weak var repliesToReplyTableView: UITableView! 

@IBOutlet weak var commentTextField: UITextField! 

var reply:String? 

var comments = [String]() 

var replyKey:String? 

override func awakeFromNib() { 
    super.awakeFromNib() 

    self.comments = [] 

    repliesToReplyTableView.delegate = self 
    repliesToReplyTableView.dataSource = self 

    commentTextField.delegate = self 

     loadComments() 

} 

func configureReplyCell(reply:String) { 

    self.reply = reply 

    self.replyTextView.text = self.reply 
} 

func loadComments() { 

    self.comments = [] 

    if let firstKey = UserDefaults.standard.value(forKey: Key_FirstKey) as? String, let secondKey = UserDefaults.standard.value(forKey: Key_SecondKey) as? String { 

     DataService.ds.Categories_Base.child(firstKey).child("Topics").child(secondKey).child("replies").observe(.value, with:{(snapshot) in 

      if let snapshots = snapshot.children.allObjects as? [FIRDataSnapshot] { 

       for snap in snapshots { 

        if let replyDict = snap.value as? Dictionary<String,AnyObject> { 

         if let reply = replyDict["text"] as? String { 

          if reply == self.reply { 

           self.replyKey = snap.key 

           DataService.ds.Categories_Base.child(firstKey).child("Topics").child(secondKey).child("replies").child(snap.key).child("comments").observe(.value, with: { (commentSnapshot) in 

            if let commentSnapshots = commentSnapshot.children.allObjects as? [FIRDataSnapshot] { 

             for commentSnap in commentSnapshots { 

              if let commentDict = commentSnap.value as? Dictionary<String,AnyObject> { 

               if let comment = commentDict["text"] as? String { 

                self.comments.append(comment) 

               } 
              } 
             } 
            } 

             self.repliesToReplyTableView.reloadData() 

           }) 
          } 
         } 
        } 
       } 

      } 

     }) 

    } 

} 


func numberOfSections(in tableView: UITableView) -> Int { 
    return 1 
} 

func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { 
    return comments.count 
} 

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { 

    let commentCell = tableView.dequeueReusableCell(withIdentifier:"CommentCell") 

    commentCell?.textLabel?.text = comments[indexPath.row] 

    return commentCell! 
} 

override func setSelected(_ selected: Bool, animated: Bool) { 
    super.setSelected(selected, animated: animated) 


} 


@IBAction func commentBtnPressed(_ sender: Any) { 

    if let comment = commentTextField.text, comment != "" { 

     self.comments = [] 

     if let firstKey = UserDefaults.standard.value(forKey: Key_FirstKey) as? String, let secondKey = UserDefaults.standard.value(forKey: Key_SecondKey) as? String { 

     DataService.ds.Categories_Base.child(firstKey).child("Topics").child(secondKey).child("replies").child(self.replyKey!).child("comments").childByAutoId().child("text").setValue(comment) 

      if let myViewController = parentViewController as? TopicForumVC { 

      // myViewController.repliesTableView.reloadData() 

       myViewController.replies = [] 
      } 


      self.repliesToReplyTableView.reloadData() 

      self.commentTextField.text = "" 
      self.replyKey = "" 
    } 


    } 

} 

回答

0

我真的不知道确切的情况下你正在建设,但有两个想法,可能会提供一些指导。

1)如果您的表格显示来自数据源的内容,那么您可能会有某种参考。例如。当加载单元格时(在这种情况下为CustomCell),您将执行类似get the index of the cellget the same index from the dataput that data in the cells content。如果是这种情况,您只需点击按钮就可以将tableview.cellForRowAtIndexPath与您的发件人对象一起使用,然后从数据源中删除该数组,例如, tableDataSource[index] = nil并重新加载tableView。 2)如果你在专门为存储这个数组而添加的CustomCell上有一个存储属性,那么你会将发送者对象转换为CustomCell并删除该属性,就像在Kim的回答中一样。

希望这有帮助,但没有更多的信息,这是很难分辨。

+0

感谢您的详细解释。我添加了一个截图并修改了我的问题以使其更清晰。你能花一分钟时间来回顾并提出你的意见吗?感谢您的时间。 – developermike

+0

@developermike你能让图像变大一点吗? – Mackey18

+0

@developermike他们在我的浏览器中看起来很模糊,很难弄清楚发生了什么。 – Mackey18

0
let cell = tableview.cellForRowAtIndexPath(...) as? CustomCell 
if cell != nil { 
    let arr = cell.array 
} 

BTW:我会重新考虑你的存储在单元阵列..

+0

嗨,金,感谢您的时间。我添加了截图并修改了我的问题。我想让每个ReplyCell中的注释数组为空,而不是特定哪个indexPath。所以你目前的答案没有帮助。你能回顾并提供建议吗? – developermike

相关问题