如何定位我的footerView以使其位于UITableView的底部?

时间:2015-08-24 03:06:24

标签: ios swift uitableview tableview

below is the image. I want the footerView to be located at the bottom and when it is tapped, the keyboard to be appearing.

我希望footerView显示在屏幕的底部。我是如何制作的,以便footerView位于底部,当它被轻敲时,键盘出现了?任何帮助,将不胜感激。谢谢!

protocol CommentsTableViewControllerDelegate: class {

func pop()
func reloadComments()


}

class CommentsTableViewController: UITableViewController, UITextViewDelegate, UITableViewDataSource{

weak var delegate: CommentsTableViewControllerDelegate?


var commentView: UITextView?
var footerView: UIView?
var contentHeight: CGFloat = 0

var comments: [String]?


var postObject: PFObject! {

    didSet {
        println(postObject)
    }

}

private var myComments: [PFObject]? {

    didSet {
        tableView.reloadData()
    }

}

private var header_label: UILabel!

override func viewDidLoad() {
    super.viewDidLoad()
    self.edgesForExtendedLayout = UIRectEdge.None
    println(postObject?.objectForKey("comments"))

    NSNotificationCenter.defaultCenter().addObserver(self, selector: "handlePostingComment:", name: postCommentNotification, object: nil)

    NSNotificationCenter.defaultCenter().addObserver(self, selector: "handleComments:", name: queryCommentNotification, object: nil)

    // Query for comments
    Downloader.sharedDownloader.queryForComments(postObject)

    header_label = UILabel(frame: .zeroRect)

    header_label.text = postObject["post"] as? String

    header_label.sizeToFit()

    header_label.frame.origin = .zeroPoint



    tableView.tableHeaderView = header_label


    // Uncomment the following line to preserve selection between presentations
    // self.clearsSelectionOnViewWillAppear = false

    // Uncomment the following line to display an Edit button in the navigation bar for this view controller.
    // self.navigationItem.rightBarButtonItem = self.editButtonItem()

    /* Setup the datasource delegate */
    tableView.delegate = self
    tableView.dataSource = self

    /* Setup the keyboard notifications */
    NSNotificationCenter.defaultCenter().addObserver(self, selector: "keyBoardWillShow:", name: UIKeyboardWillShowNotification, object: nil)
    NSNotificationCenter.defaultCenter().addObserver(self, selector: "keyBoardWillHide:", name: UIKeyboardWillHideNotification, object: nil)


    /* Setup the contentInsets */
    self.tableView.contentInset = UIEdgeInsetsZero
    self.tableView.scrollIndicatorInsets = UIEdgeInsetsZero


    self.edgesForExtendedLayout = UIRectEdge.None
    /* Make sure the content doesn't go below tabbar/navbar */
    self.extendedLayoutIncludesOpaqueBars = true

    self.automaticallyAdjustsScrollViewInsets = false

    if(postObject?.objectForKey("comments") != nil) {
        comments = postObject?.objectForKey("comments") as? [String]
    }
    println(postObject)
    println(postObject?.objectForKey("text"))

}

func handleComments(notification: NSNotification) {

    let comments = notification.object as? [PFObject]

    if let comments = comments {

        myComments = comments

    }
}

func handlePostingComment(notification: NSNotification) {



    if let success = notification.object as? Bool {

        if success {

            delegate?.reloadComments()


        } else {


        }


    }


}


override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated.
}

// MARK: - Table view data source

override func numberOfSectionsInTableView(tableView: UITableView) -> Int {
    // #warning Potentially incomplete method implementation.
    // Return the number of sections.
    return 1
}

override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    // #warning Incomplete method implementation.
    // Return the number of rows in the section.
    return myComments?.count ?? 0
}


override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCellWithIdentifier(reuseIdentifier, forIndexPath: indexPath) as! UITableViewCell

    // Configure the cell...
    if let myComments = myComments {

        let comment = myComments[indexPath.row]
        cell.textLabel?.text = comment["text"] as? String

    }

    return cell
}

func keyBoardWillShow(notification: NSNotification) {
    var info:NSDictionary = notification.userInfo!
    var keyboardSize = (info[UIKeyboardFrameBeginUserInfoKey] as! NSValue).CGRectValue()

    var keyboardHeight:CGFloat =  keyboardSize.height - 40

    var animationDuration:CGFloat = info[UIKeyboardAnimationDurationUserInfoKey] as! CGFloat

    var contentInsets: UIEdgeInsets = UIEdgeInsetsMake(0.0, 0.0, keyboardHeight, 0.0);
    self.tableView.contentInset = contentInsets
    self.tableView.scrollIndicatorInsets = contentInsets

}

func keyBoardWillHide(notification: NSNotification) {

    self.tableView.contentInset = UIEdgeInsetsZero
    self.tableView.scrollIndicatorInsets = UIEdgeInsetsZero
}

override func tableView(tableView: UITableView, heightForFooterInSection section: Int) -> CGFloat {
    if self.footerView != nil {
        return self.footerView!.bounds.height
    }
    return 50
}


override func tableView(tableView: UITableView, viewForFooterInSection section: Int) -> UIView? {
footerView = UIView(frame: CGRect(x: 0, y: 0, width: tableView.bounds.width, height: 100))
footerView?.backgroundColor = UIColor(red: 243.0/255, green: 243.0/255, blue: 243.0/255, alpha: 1)
commentView = UITextView(frame: CGRect(x: 10, y: 5, width: tableView.bounds.width - 80 , height: 30))
commentView?.backgroundColor = UIColor.whiteColor()
commentView?.textContainerInset = UIEdgeInsetsMake(5, 5, 5, 5)
commentView?.layer.cornerRadius = 2
commentView?.scrollsToTop = true

footerView?.addSubview(commentView!)
let button = UIButton(frame: CGRect(x: tableView.bounds.width - 65, y: 10, width: 60 , height: 30))
button.setTitle("Reply", forState: UIControlState.Normal)
button.backgroundColor = UIColor(red: 155.0/255, green: 189.0/255, blue: 113.0/255, alpha: 1)
button.layer.cornerRadius = 5
button.addTarget(self, action: "reply", forControlEvents: UIControlEvents.TouchUpInside)
footerView?.addSubview(button)
commentView?.delegate = self
return footerView
}




func textViewDidChange(textView: UITextView) {


    if (contentHeight == 0) {
        contentHeight = commentView!.contentSize.height
    }

    if(commentView!.contentSize.height != contentHeight && commentView!.contentSize.height > footerView!.bounds.height) {
        UIView.animateWithDuration(0.2, animations: { () -> Void in
            let myview = self.footerView
            println(self.commentView!.contentSize.height)
            println(self.commentView?.font.lineHeight)
            let newHeight : CGFloat = self.commentView!.font.lineHeight
            let myFrame = CGRect(x: myview!.frame.minX, y: myview!.frame.minY - newHeight , width: myview!.bounds.width, height: newHeight + myview!.bounds.height)
            myview?.frame = myFrame

            let mycommview = self.commentView
            let newCommHeight : CGFloat = self.commentView!.contentSize.height
            let myCommFrame = CGRect(x: mycommview!.frame.minX, y: mycommview!.frame.minY, width: mycommview!.bounds.width, height: newCommHeight)
            mycommview?.frame = myCommFrame

            self.commentView = mycommview
            self.footerView  = myview

            for item in self.footerView!.subviews {
                if(item.isKindOfClass(UIButton.self)){
                    let button = item as! UIButton
                    let newY = self.footerView!.bounds.height / 2 - button.bounds.height / 2
                    let buttonFrame = CGRect(x: button.frame.minX, y: newY , width: button.bounds.width, height : button.bounds.height)
                    button.frame = buttonFrame

                }
            }
        })

        println(self.footerView?.frame)
        println(self.commentView?.frame)
        contentHeight = commentView!.contentSize.height
    }




}


func reply() {

    println(commentView?.text)
    Downloader.sharedDownloader.postingAComment(commentView!.text, post: postObject)
    commentView!.text = ""



}

1 个答案:

答案 0 :(得分:0)

首先创建页脚视图。

func createFooterView() -> UIView {

    self.footerView = UIView(frame: CGRect(x: 0, y: 0, width: self.tblView.bounds.width, height: 100))
    self.footerView?.backgroundColor = UIColor(red: 243.0/255, green: 243.0/255, blue: 243.0/255, alpha: 1)
    self.commentView = UITextView(frame: CGRect(x: 10, y: 5, width: self.tblView.bounds.width - 80 , height: 30))
    self.commentView?.delegate = self
    self.commentView?.backgroundColor = UIColor.whiteColor()
    self.commentView?.textContainerInset = UIEdgeInsetsMake(5, 5, 5, 5)
    self.commentView?.layer.cornerRadius = 2
    self.commentView?.scrollsToTop = true
    self.footerView?.addSubview(self.commentView!)

    let button = UIButton(frame: CGRect(x: self.tblView.bounds.width - 65, y: 10, width: 60 , height: 30))
    button.setTitle("Reply", forState: UIControlState.Normal)
    button.backgroundColor = UIColor(red: 155.0/255, green: 189.0/255, blue: 113.0/255, alpha: 1)
    button.layer.cornerRadius = 5
    button.addTarget(self, action: "reply", forControlEvents: UIControlEvents.TouchUpInside)
    self.footerView?.addSubview(button)
    return self.footerView
}

并在viewDidLoad()

self.tblView.tableFooterView = self.createFooterView();