简体   繁体   中英

Add a top view to UITableview and scroll it to bottom of navigationbar

I Have created a view and added to subview of UITableview -

 topView = UIView(frame: CGRectMake(0, 250, self.myTableView.frame.width, 44)) 
        topView.backgroundColor = UIColor.redColor() 
        self.view.addSubview(topView) 
        self.view.bringSubviewToFront(topView) 
         
        self.myTableView.contentInset = UIEdgeInsetsMake(294, 0, 0, 0) 

Now I want to scroll this view to the bottom of navigationBar so I have written scrollview delegete method-

 func scrollViewDidScroll(scrollView: UIScrollView){ 
        var rect:CGRect = topView.frame; 
        if myTableView.contentOffset.y <= -98 { 
       rect.origin.y = (myTableView.contentOffset.y * -1) - rect.size.height 

            topView.frame = rect 
        } 
    } 

=> Here try following code..

UIView *myProgView = (UIView *)self.progView; //progView is a method that returns a UIView

[self.tableView insertSubview:myProgView aboveSubview:self.tableView]; 

[self.tableView bringSubviewToFront:myProgView];

=> do tableView Scroll position :

 if numberOfRows > 0 {

 let indexPath = NSIndexPath(forRow: numberOfRows-1, inSection: (numberOfSections-1))

 self.tableView.scrollToRowAtIndexPath(indexPath, atScrollPosition: UITableViewScrollPosition.Bottom, animated: animated)
 }
  1. I found another solution, i have created 2 sections in tableview. In first section, returned 1 cell which size is equal to the top inset of my tableview. I didn't give height to 0th section.
  2. In second section i have added my specific height and some number of rows in my tableview.

Now i am able to scrolling my desired view into the tableview section.

Thanks

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM