UIScrollView inside UITableViewCell touch detect

后端 未结 5 1475
清歌不尽
清歌不尽 2020-12-04 20:16

I have a tableview with 8 custom cells. in the 8th cell I added a scrollView with paging enabled so I can show page 1 and page 2 (or 3, 4... 10) without have a very high cel

5条回答
  •  轻奢々
    轻奢々 (楼主)
    2020-12-04 20:24

    I found the simplest solution for my needs:

    subclass UIScrollView touchesEnded method and post a notification.

    In the UITableview add an observer in viewdidAppear (remove it in viewdiddisappear) to call a function that call tableview didSelectRowForIndexPath.

    Something like this (swift version)

    // myScrollView.swift
    
    import UIKit
    
    class myScrollView: UIScrollView {
        override func touchesEnded(touches: Set, withEvent event: UIEvent?) {
                NSNotificationCenter.defaultCenter().postNotificationName("selectTVRow", object: nil)
        }
    }
    

    In your tableView:

    // ItemsList.swift
    
        override func viewDidAppear(animated: Bool) {
            super.viewDidAppear(animated)
            NSNotificationCenter.defaultCenter().addObserver(self, selector: "selectFourthRow", name: "selectTVRow", object: nil)
        }
    
        override func viewDidDisappear(animated: Bool) {
            super.viewDidDisappear(animated)
            NSNotificationCenter.defaultCenter().removeObserver(self, name: "selectfourthrow", object: nil)
        }
    
        func selectFourthRow() {
            let rowToSelect:NSIndexPath = NSIndexPath(forRow: 4, inSection: 0);
            self.tableView(self.tableView, didSelectRowAtIndexPath: rowToSelect);
        }
    
        /*
        .... rest of your tableview Datasource and Delegate methods...
        numberOfSectionsInTableView, numberOfRowsInSection, cellForRowAtIndexPath
        */
    

提交回复
热议问题