Table View Swift Xcode

1

I am new programming for iOS so I do not know how to solve this problem, I have a view where I have 2 tableview one where you select which chat you want to open and the other table is where the messages are displayed. Now I have 3 problems

  • At the time of selecting the chat my messages are not automatically shown (Refresh) if not until selecting the tableview chat

  • When sending a message does not show the gray balloon and the same try to refresh it and even then it only deletes me the messages

  • That every time a message is sent or shows you the chat the last message is displayed

  • This is my Table View code

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
         var cell:UITableViewCell?
        if tableView == self.tableView2 {
                let tu = ((identificador[indexPath.row])as NSString).integerValue
                if tu == 2{
                        let cell = tableView.dequeueReusableCell(withIdentifier: "Messages",  for: indexPath) as! ChatTableViewCell
                        cell.emisor.isHidden = true
                        cell.receptortxt.text = self.chats[indexPath.row]
                        return cell
                }
                else if tu == 1{
                        let cell = tableView.dequeueReusableCell(withIdentifier: "Messages",  for: indexPath) as! ChatTableViewCell
                        cell.receptor.isHidden = true
                        cell.emisortxt.text = self.chats[indexPath.row]
                        return cell
                }
        }
        if tableView == self.tableView {
                cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath)
                cell?.textLabel?.text=separated[indexPath.row]
        }
        return cell!
    }
    

    This is the code when you select chat

     func peticionhttp()
    {
       /// Codigo que manda a traer cual chat
                 self.refresh()
                 self.boton.setTitle("\(self.separated[0])", for: .normal)
                 }
            }
            task.resume()
    }
    func refresh(){
        print("refreshed" , tu2)
        self.tableView2.reloadData()
        self.tableView2.separatorStyle = .none
    }
    

    Code when sending message

    @IBAction func onClicEnviar(_ sender: Any) {
       // dismissKeyboard()
        peticionhttpcuatro()
    
        refresher = UIRefreshControl()
        refresher.attributedTitle = NSAttributedString(string: "Pulsa para refrescar")
        refresher.addTarget(self, action: #selector(ChatIndividual.refrescar), for: UIControlEvents.valueChanged)
    
        tableView2.addSubview(refresher)
    }
    
    
     @objc func refrescar () {
        chats.append(textfield.text!)
        identificador += ["1"]
    
        tableView2.reloadData()
        refresher.endRefreshing()
        print(chats,identificador,"Heeeeeeeeeeeeeeeeeeey ")
    }
    

    P.d: I'm not using Firebase

        
    asked by Milan 05.06.2018 в 19:27
    source

    1 answer

    1

    Try to call your function self.refresh in the main thread since all the operations that try to modify UI must execute in this thread. 1 -

    DispatchQueue.main.async { 
      self.refresh()
    }
    

    2- In this case all your messages must be in an arrangement, when you send the message insert that message in your directory and in the table:

    tableView.beginUpdates()
    tableView.insertRows(at: <#T##[IndexPath]#>, with: <#T##UITableViewRowAnimation#>)
     tableView.endUpdates()
    

    Example:

    import UIKit

    class ChatCell: UITableViewCell {     @IBOutlet weak var senderMessage: UILabel!     @IBOutlet weak var receiverMessage: UILabel!

    static let idenfitier: String = String(describing: ChatCell.self)
    
    var message: Message?{
        didSet{
            if let unwrappedMessage = message{
                if unwrappedMessage.sender == 1{
                    senderMessage.text = unwrappedMessage.message
                    receiverMessage.text = ""
                }else{
                    receiverMessage.text = unwrappedMessage.message
                    senderMessage.text = ""
                }
            }
        }
    }
    }
    
    struct Message {
        var message: String?
        var sender: Int?
    //    2 = receptor, 1 = emisor
    }
    
    class ChatViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {
    
    @IBOutlet weak var chatView: UITableView!
    @IBOutlet weak var messageTextField: UITextField!
    
    var messages = [Message(message: "Hola", sender: 2), Message(message: "Hola, ¿Comó estas?", sender: 1)]
    
    
    override func viewDidLoad() {
        super.viewDidLoad()
        chatView.delegate = self
        chatView.dataSource = self
        chatView.estimatedRowHeight = 80
        chatView.keyboardDismissMode = .onDrag
        chatView.separatorStyle = .none
    }
    
    func numberOfSections(in tableView: UITableView) -> Int {
        return 1
    }
    
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return messages.count
    }
    
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: ChatCell.idenfitier) as! ChatCell
    
        cell.message = messages[indexPath.item]
    
        return cell
    }
    
    @IBAction func sendMessage(_ sender: UIButton){
        if let text = messageTextField.text{
            let newMessage = Message(message: text, sender: 1)
            messages.append(newMessage)
            chatView.reloadData()
        }
    }
    }
    
        
    answered by 05.06.2018 / 20:58
    source