2016-07-02 18 views
2

Hallo Ich versuche, Tableview Höhe nach der Bearbeitung von Text ..Wie kann ich Tableview Höhe nach Bearbeiten von Text zurückgesetzt

SCHRITT 1. Tab die Eingabetextview

SCHRITT 2. Geben Sie Kommentar Nachricht über das 1 zurückgesetzt .. Linien (zB 2 Zeilen, 3 Zeilen ..)

SCHRITT 3. Tippen Sie auf senden-Taste

STEP 4. Reset UI

Meine Frage is..when ich eintrat Kommentar als 1 line ... Es funktioniert gut ..

Aber ich Kommentar 2 Zeilen oder mores eingegeben haben ... dann Tableview die Höhe wird nicht zurückgesetzt ...

Können Sie mir helfen?

enter image description here

import UIKit 
import Parse 

var commentUUID = [String]() 
var commentOwner = [String]() 

class CommentViewController: UIViewController, UITextViewDelegate, UITableViewDelegate, UITableViewDataSource{ 

    //UI Objects 
    @IBOutlet weak var tableView: UITableView! 
    @IBOutlet weak var commentTextView: UITextView! 
    @IBOutlet weak var sendButton: UIButton! 
    @IBOutlet weak var bottomConstraint: NSLayoutConstraint! 
    var refresher = UIRefreshControl() 

    //values for reseting UI to default 
    var tableViewHeight : CGFloat = 0 
    var commentY : CGFloat = 0 
    var commentHeight : CGFloat = 0 
    var tableDiff : CGFloat = 0 

    //arryas to hold server data 
    var usernameArray = [String]() 
    var profileArray = [PFFile]() 
    var commentArray = [String]() 
    var dateArray = [NSDate?]() 


    //variable to hold keyboard frame 
    var keyboard = CGRect() 

    //page size 
    var page : Int32 = 15 

    override func viewDidLoad() { 

     super.viewDidLoad() 

     tableView.backgroundColor = UIColor(red: 242.0/255.0, green: 242.0/255.0, blue: 242.0/255.0, alpha: 1) 


     //title at the top 
     self.navigationItem.title = "COMMENTS" 
     self.navigationItem.hidesBackButton = true 
//  let backButton = UIBarButtonItem(title: "back", style: .Plain, target: self, action: #selector(CommentViewController.back(_:))) 
     let backButton = UIBarButtonItem(image: UIImage(named: "backBtn"), style: .Plain, target: self, action: #selector(CommentViewController.back(_:))) 




     self.navigationItem.leftBarButtonItem=backButton 


     //swipe to go back 
     let backSwipe = UISwipeGestureRecognizer(target: self, action: #selector(CommentViewController.back(_:))) 
     backSwipe.direction=UISwipeGestureRecognizerDirection.Right 
     self.view.addGestureRecognizer(backSwipe) 

     // catch notification if the keyboard is shown or hidden 
     NSNotificationCenter.defaultCenter().addObserver(self, selector: #selector(CommentViewController.keyboardWillShow(_:)), name: UIKeyboardWillShowNotification, object: nil) 



     NSNotificationCenter.defaultCenter().addObserver(self, selector: #selector(CommentViewController.keyboardWillHide(_:)), name: UIKeyboardWillHideNotification, object: nil) 

     //disable button from the beginning 
     sendButton.enabled = false 

     loadComments() 

     //call function 
     alignment() 

    } 


    //func loading when keyboard is shown 
    func keyboardWillShow(notification : NSNotification){ 

     //define keyboard frame size 
     keyboard = (notification.userInfo?[UIKeyboardFrameEndUserInfoKey]!.CGRectValue)! 

     //move UI up 
     UIView.animateWithDuration(0.4){() -> Void in 


      self.bottomConstraint.constant = self.keyboard.height + 4 

      self.tableDiff = self.tableView.frame.size.height 
     } 
    } 

    //func loading when keyboard is hidden 
    func keyboardWillHide(notification : NSNotification){ 

     //move UI down 
     UIView.animateWithDuration(0.4){() -> Void in 


     } 

    } 

    //alignment function 
    func alignment(){ 


     tableView.rowHeight = UITableViewAutomaticDimension 
     tableView.estimatedRowHeight = 160.0 

     //set CommentTextView Style 
     commentTextView.layer.cornerRadius = commentTextView.frame.size.width/50 


     //delegates 
     commentTextView.delegate = self 
     tableView.delegate = self 
     tableView.dataSource = self 

     //assign reseting values 
     tableViewHeight = tableView.frame.size.height 
     commentHeight = commentTextView.frame.size.height 
     commentY = commentTextView.frame.origin.y 


    } 



    //while writing something 
    func textViewDidChange(textView: UITextView) { 

     //disable button if entered no text 
     let spacing = NSCharacterSet.whitespaceAndNewlineCharacterSet() 

     //It shown when user entered type 
     if !commentTextView.text.stringByTrimmingCharactersInSet(spacing).isEmpty{ 
      sendButton.enabled = true 
     }else { 

      sendButton.enabled = false 
     } 


     // + paragraph 

     if textView.contentSize.height > textView.frame.size.height && textView.frame.height < 130{ 

      //find difference to add 
      let difference = textView.contentSize.height - textView.frame.size.height 
      self.tableDiff = difference 

      //redefine frame of commentText 
      textView.frame.origin.y = textView.frame.origin.y - difference 
      textView.frame.size.height = textView.contentSize.height 

      //move up tableView 
      if textView.contentSize.height + keyboard.height + commentY >= tableView.frame.size.height { 
       tableView.frame.size.height = tableView.frame.size.height - difference 


      } 

     } 

     // - parapraph 

     else if textView.contentSize.height < textView.frame.size.height { 

      //find difference to deduct 
      let difference = textView.frame.size.height - textView.contentSize.height 
      self.tableDiff = difference 
      //redefine frame of commentText 
      textView.frame.origin.y = textView.frame.origin.y + difference 
      textView.frame.size.height = textView.contentSize.height 

      //move down tableview 
      if textView.contentSize.height + keyboard.height + commentY > tableView.frame.size.height { 

       tableView.frame.size.height = tableView.frame.size.height + difference 
      } 

     } 


    } 

// Senden-Knopf Stampf @IBAction func sendButtonTapped (Sender: ANYOBJECT) {

print("send tapped") 
    //STEP1. Add row in tableView 
    usernameArray.append(PFUser.currentUser()!.username!) 
    profileArray.append(PFUser.currentUser()?.objectForKey("profileImg") as! PFFile) 
    dateArray.append(NSDate()) 
    commentArray.append(commentTextView.text.stringByTrimmingCharactersInSet(NSCharacterSet.whitespaceAndNewlineCharacterSet())) 
    tableView.reloadData() 

    //STEP2. Send comment to server 
    let commentObj = PFObject(className: "comments") 
    commentObj["to"] = commentUUID.last 
    commentObj["username"] = PFUser.currentUser()?.username 
    commentObj["profileImg"] = PFUser.currentUser()?.valueForKey("profileImg") 
    commentObj["comment"] = commentTextView.text.stringByTrimmingCharactersInSet(NSCharacterSet.whitespaceAndNewlineCharacterSet()) 
    commentObj.saveEventually() 

    //Scroll to bottom 
    self.tableView.scrollToRowAtIndexPath(NSIndexPath(forItem: commentArray.count - 1, inSection: 0), atScrollPosition: UITableViewScrollPosition.Bottom, animated: false) 

    //STEP 3. Reset UI 
    sendButton.enabled = false 
    commentTextView.text = "" 
    commentTextView.frame.size.height = commentHeight 
    commentTextView.frame.origin.y = sendButton.frame.origin.y 


    //TableView Reset....Is Not working. 
    tableView.frame.size.height = tableView.frame.size.height 

} 

Antwort

1

Es aussehen wie Sie Auto-Layout verwenden, so dass anstelle der tableView.frame der Aktualisierung , sollten Sie die Einschränkung aktualisieren. Erstellen Sie eine IBOutlet für Ihre tableView heightConstraint und legen Sie die aktualisierte Höhe darauf fest.

IBOutlet weak var tableHeightConstraint: NSLayoutConstraint! 

//calculate the height and update the constant 
tableHeightConstraint.constant = updatedTableHeight 

auch aus dem Code, es aussehen wie Sie gerade die tableView.frame.height mit sich selbst zu aktualisieren. Sie sollten den Wert berechnen und wie oben auf die Einschränkung festlegen.

+0

Können Sie mir helfen? Ich habe IBOutlet der Tischhöhe hinzugefügt. Wie ändere ich meine Funktion von textViewDidChange. –

+0

Wenn ich das automatische Layout ... textView.frame sollte ändern, um zu verengen? // Frame des Kommentars neu definierenText textView.frame.origin.y = textView.frame.origin.y - Unterschied textView.frame.size.height = textView.contentSize.height –

+0

And .. // + paragraph, // - Absatz ... Wie lege ich tableHeightConstraint.constant fest? Ich habe versucht, ableViewConstraintH.constant = tableView.frame.size.height - Unterschied. Es hat gerade frame.size.height in constraint geändert. aber es scheint nicht zu funktionieren. Sorry für zu viel fragen..für dich –