Multiple delegates for UITableView in fast

I think it is best to illustrate what I am trying to achieve with a concrete example:

Say I'm making a cocopod. I have a table view inside an example project, and I want the tableview delegate methods to scroll through it to be called inside my cocoapod . I know how to pass a tableView to my cocoapod module, but I'm not sure how I can listen for delegation methods inside it, since I want my ViewController in the sample application to also be a delegate.

The end result: I want to show a simple view moving up and down using the scroll bar in the View table as it scrolls.

First of all, which should be used for scrollview delegate methods, so can I constantly update the y position of my custom view to align the center of the y scrollbar position ?

Secondly, how can I listen to them (delegate methods of scrollview / tableview) inside my cocoapod module ?

Note. I am using Swift 2.2

Edit: here is the final product, thanks to the help of your guys, in case anyone is interested: https://github.com/xtrinch/MRTableViewCellCountScrollIndicator

+5
source share
1 answer

You can achieve both goals using Key-Value Observing (KVO) to track contentOffset and contentSize view scrolling regardless of the table view delegate.

contentOffset is the scrollView's scroll count. The y value is the amount scrollable in the vertical direction.

contentSize is the total height of all rows in the table.

KVO allows you to write code that is called when a property changes on another object. You can use KVO to track changes in contentSize and contentOffset and update the user view when these values ​​change.

Here's how you can implement it in your CocoaPod:

 private var ContentOffsetKVO = 0 private var ContentSizeKVO = 0 public class ScrollController: NSObject { public var customView: UIView? { didSet { updateScrollPosition() } } public var scrollView: UIScrollView? { didSet { if let view = oldValue { removeKVO(view) } if let view = scrollView { addKVO(view) updateScrollPosition() } } } deinit { if let scrollView = scrollView { removeKVO(scrollView) } } private func removeKVO(scrollView: UIScrollView) { scrollView.removeObserver( self, forKeyPath: "contentSize", context: &ContentSizeKVO ) scrollView.removeObserver( self, forKeyPath: "contentOffset", context: &ContentOffsetKVO ) } private func addKVO(scrollView: UIScrollView) { scrollView.addObserver( self, forKeyPath: "contentSize", options: [.Initial, .New], context: &ContentSizeKVO ) scrollView.addObserver( self, forKeyPath: "contentOffset", options: [.Initial, .New], context: &ContentOffsetKVO ) } public override func observeValueForKeyPath(keyPath: String?, ofObject object: AnyObject?, change: [String : AnyObject]?, context: UnsafeMutablePointer<Void>) { switch keyPath { case .Some("contentSize"), .Some("contentOffset"): self.updateScrollPosition() default: super.observeValueForKeyPath(keyPath, ofObject: object, change: change, context: context) } } private func updateScrollPosition() { guard let scrollView = scrollView, let customView = customView else { return } // Update with the new scroll position. let viewSize = scrollView.bounds.size.height let scrollLimit = scrollView.contentSize.height - viewSize let scrollOffset = scrollView.contentOffset.y let scrollDelta = scrollOffset / scrollLimit let trackerFrame = customView.frame let trackerTravel = viewSize - trackerFrame.size.height let trackerOffset = scrollOffset + (trackerTravel * scrollDelta) customView.frame = CGRect( origin: CGPoint( x: 0, y: trackerOffset ), size: trackerFrame.size ) } } 

To use this:

 let scrollController = ScrollController() override func viewDidLoad() { super.viewDidLoad() // UITableView is a sub-class of UIScrollView so we can assign it directly. scrollController.scrollView = tableView let customView = UIView( frame: CGRect( origin: CGPointZero, size: CGSize( width: 44, height: 44 ) ) ) customView.backgroundColor = UIColor.magentaColor() tableView.addSubview(customView) scrollController.customView = customView } 
0
source

All Articles