0

我有一个表格视图,其中的行填充了数组中的数据。通过调用此函数在 viewDidLoad() 中动态设置行高:

func configureTableView() {          
    /*  When you set the rowHeight as UITableViewAutomaticDimension, the table view knows to use the auto layout constraints to determine each cell’s height. This also reuqires a estimetedRowHeoght, an arbitary number */      
    println("Configure Table View called")

   tableView.estimatedRowHeight = 74.0;
   tableView.rowHeight = UITableViewAutomaticDimension;
   tableView.separatorStyle = UITableViewCellSeparatorStyle.None;
}

填充每一行的数据正在从 Parse 加载到数组中。一旦将数据加载到数组中,我就会调用:

self.tableView.reloadData();

self.configureTableView();

...为了重新加载表行数据,然后根据行内填充标签的注释大小动态重新配置行高。

问题有两个方面:

  1. 只有某些行的高度是动态设置的,其他行的行高似乎是固定的,如果超过一行,您将无法查看评论
  2. 当用户滚动 tableview 时,刷新的视图所有行都是静态的,似乎没有动态设置高度,只能看到一行注释。

任何输入表示赞赏

我还使用了一个页脚视图,它是用代码以编程方式创建的,以允许显示和隐藏键盘,我认为这可能与它有关....

// viewDidlLoad

 /* Setup the keyboard notifications  so does not block textview for adding comments */
        NSNotificationCenter.defaultCenter().addObserver(self, selector: "keyBoardWillShow:", name: UIKeyboardWillShowNotification, object: nil)
        NSNotificationCenter.defaultCenter().addObserver(self, selector: "keyBoardWillHide:", name: UIKeyboardWillHideNotification, object: nil)


        /* Setup the contentInsets fo keyboard  */
        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

提前道歉,但此处分配代码用于设置页脚视图和处理用户输入......

 func tableView(tableView: UITableView, viewForFooterInSection section: Int) -> UIView? {


        footerView = UIView(frame: CGRect(x: 0, y: 0, width: tableView.bounds.width, height: FOOTERHEIGHT))
        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: 40))
        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 tableView(tableView: UITableView, heightForFooterInSection section: Int) -> CGFloat {


        if self.footerView != nil {
            return self.footerView!.bounds.height
        }
        return FOOTERHEIGHT


    func keyBoardWillShow(notification: NSNotification) {

        // Called in viewDidlOad, make sure keyoard doe snot cover add comments textview in table voew footer

        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) {

        //As above

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


    func textViewDidChange(textView: UITextView) {

        // Allow for dynamic changing if TableView Footer textview size when commenst added

        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() {

        // When Add button clicked in custom TabkeView Footer

        println("User added comment: \(commentView?.text)");

        self.commentView?.resignFirstResponder()


        //userComments = commentView!.text;

        //addComment();


        self.tableView.reloadData()

        //self.configureTableView();
    }
4

0 回答 0