在Tableview中动态设置行高仅适用于某些单元格

时间:2015-04-08 18:07:25

标签: uitableview swift row-height

我有一个表视图,其中行使用数组中的数据填充。 通过调用此函数在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加载到数组aysnrounscly。 一旦数据加载到数组中,我就调用:

self.tableView.reloadData();

self.configureTableView();

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

问题有两个:

  1. 只有一些行动态设置了高度,其他行的行高似乎是固定的,如果有多行,则无法查看注释
  2. 当用户滚动tableview时,刷新的视图所有行都是静态的,似乎没有动态设置高度,只能看到一行注释。
  3. 赞赏任何输入

    我也在使用一个页脚视图,该视图是使用代码创建的,允许显示和隐藏键盘,我认为这可能与它有关....

    // viewDidlOad

     /* 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();
        }
    

0 个答案:

没有答案