[Swift linkage]: linkage between the two TableView, TableView and CollectionView linkage between

Preface

Before Objective-C wrote an article linking demo and articles, then I have no private letter buddy Swfit language, while the last evening and weekend to learn about the Swift 3 syntax, to write a Swift demo.
ideas and Objective-C version of the linkage article, the same effect is achieved. First look at the effect diagram.

[Swift linkage]: linkage between the two TableView, TableView and CollectionView linkage between
linkage.Gif

text

First, the linkage between TableView and TableView

The following is the main idea of the linkage between the two TableView:
parsing data loading model.

Guard let / / data validation path = Bundle.main.path ("forResource: meituan", ofType: "JSON" else) {return} guard let data = NSData (contentsOfFile: path) as? Data else {return} guard let anyObject = try? JSONSerialization.jsonObject (with: data, options:.MutableContainers else) {return} guard let dict = anyObject as? [String: Any] else {return} guard let datas = dict["data" as [String Any]?: else {return} guard let foods = datas["food_spu_tags" as "]? String: Any]] else {return} for {model = food in foods let CategoryModel (dict: food) categoryData.append (model) guard let spus model.spus else = {continue} = [FoodModel] (VaR datas) for fModel in spus datas.append (fModel) {foodData} .append (datas)}

Define two TableView:LeftTableView and RightTableView.

Fileprivate lazy var leftTableView: UITableView {let = leftTableView = UITableView (leftTableView.delegate) = self leftTableView.dataSource = self leftTableView.frame = CGRect (x: 0, y: 0, width: 80, height: ScreenHeight) leftTableView.rowHeight = 55 leftTableView.showsVerticalScrollIndicator = false leftTableView.separatorColor = UIColor.clear leftTableView.register (LeftTableViewCell.self forCellReuseIdentifier: kLeftTableViewCell) return leftTableView lazy var rightTableView (fileprivate)}: = UITableView {let rightTableView = UITableView (rightTableView.delegate) = self rightTableView.dataSource = self rightTableView.frame = CGRect (x: 80, y: 64, width: ScreenWidth - 80, height: - ScreenHeight 64) rightTableView.rowHeight = 80 rightTableView.showsVerticalScrollIndicator = false rightTableView.register (RightTableViewCell.self forCellReuseIdentifier: kRightTableViewCell) return rightTableView (func)} tableView (tableView: UITableView cellForRowAt indexPath: IndexPath _, -> UITableViewCell) {if leftTableView = = tableView {let cell = tableView.dequeueReusableCell (withIdentifier: kLeftTableViewCell, for: indexPath) as LeftTableViewCell let model! = categoryData[indexPath.row] cell.nameLabel.text = model.name return cell} else {let cell = tableView.dequeueReusableCell (withIdentifier: kRightTableViewCell, for: indexPath) as RightTableViewCell let model = food! Data[indexPath.section][indexPath.row] cell.setDatas (model) return cell}}

First TableView on the left side of the right TableView: click on the left side of the TableViewCell, the right side of the TableView jump to the corresponding partition list header.

Func tableView (tableView: UITableView viewForHeaderInSection section: _, Int) -> UIView? LeftTableView = = tableView {if {return} nil let headerView = TableViewHeaderView (frame: CGRect (x: 0, y: 0, width: ScreenWidth height:, 20)) let model = categoryData[section] headerView.nameLabel.text = model.name return headerView}

The TableView associated with left and right TableView: Mark rolling direction of the RightTableView, and then in the TableView partition is on the show and display the title agent function ends inside the processing logic.

  • In 1 TableView partition title will be presented, determine the current tableView is RightTableView, RightTableView sliding direction, RightTableView is a user generated drag roll (mainly about whether RightTableView is the user to drag, or click the LeftTableView roll), if three are established, then LeftTableView section RightTableView is currently selected.
  • In 2 TableView partition title display end, determine the current tableView is RightTableView, the direction of slip down, RightTableView is the user drag from rolling, if the three are set up, then LeftTableView section-1 RightTableView is currently selected.
Mark RightTableView / / rolling direction, up or down func scrollViewDidScroll (_ scrollView: UIScrollView) {let tableView = scrollView as! UITableView if rightTableView tableView {isScrollDown = lastOffsetY = < scrollView.contentOffset.y = lastOffsetY scrollView.contentOffset.y}} / / TableView func tableView (partition title will show _ tableView: UITableView, willDisplayHeaderView view: UIView, forSection section: Int) {/ / the current tableView is RightTableView, RightTableView rolling direction, RightTableView is the user drag from rolling the ((mainly judge RightTableView users to drag the rolling and rolling, or click on the LeftTableView if (rightTableView) = tableView & &);! IsScrollDown & & rightTableView.isDragging (index: section) {selectRow}} / / TableView func tableView (end title display partition _ tableView: UITableView, didEndDisplayingHeaderView view: UIView, forSection section: Int) {/ / the current tableView is RightTableView, RightTableView rolling downward direction, RightTableView is the user drag from rolling the ((mainly judge RightTableView users to drag the scroll, or click on the LeftTableView if (rightTableView) and rolling = = tableView) & & isScrollDown; & & rightTableView.isDragging {selectRow (index: section + 1)}} / / time when dragging right TableView, left TableView private func selectRow processing (index: Int {leftTableView.selectRow (at: IndexPa) Th (row: index, section: 0), animated: true, scrollPosition:.Top)}

This will achieve a linkage between the two TableView, is not very simple.

Two, the linkage between TableView and CollectionView

The linkage between TableView and CollectionView is similar to that of linkage logic between two TableView.
below that the main idea of the linkage between TableView and CollectionView:
is the same, the first analytical data loading model.

Guard let / / data validation path = Bundle.main.path ("forResource: liwushuo", ofType: "JSON" else) {return} guard let data = NSData (contentsOfFile: path) as? Data else {return} guard let anyObject = try? JSONSerialization.jsonObject (with: data, options:.MutableContainers else) {return} guard let dict = anyObject as? [String: Any] else {return} guard let datas = dict["data" as [String Any]?: else {return} guard let categories = datas["categories" as [[String?: Any]] else for category in {return} categories {let model = CollectionCategoryModel (dict: category) dataSource.append (model) guard let = subcategories model.subcategories else {continue} var datas = [SubCategoryModel] (for) subcategory in subcategor Ies {datas.append (subcategory)} collectionDatas.append (datas)}

Define a TableView, a CollectionView.

Fileprivate lazy var tableView: UITableView {let = tableView = UITableView (tableView.delegate) = self tableView.dataSource = self tableView.frame = CGRect (x: 0, y: 0, width: 80, height: ScreenHeight) tableView.rowHeight = 55 tableView.showsVerticalScrollIndicator = false tableView.separatorColor = UIColor.clear tableView.register (LeftTableViewCell.self forCellReuseIdentifier: kLeftTableViewCell) return tableView lazy var flowlayout (fileprivate)}: = LJCollectionViewFlowLayout {let flowlayout = LJCollectionViewFlowLayout (flowlayout.scrollDirection) =.Vertical flowlayout.minimumLineSpacing = 2 flowlayout.minimumInteritemSpacing = 2 flowlayout.itemSize = CGSize (width: (ScreenWidth - 80 - 4 - 4 / 3), height: (ScreenWidth - 80 - 4 - 4) / 3 + 30) flowlayout.headerReferenceSize = CGSize (width: ScreenWidth, height: 30) return flowlayout lazy var collectionView (fileprivate)}: = {UICollectionView let collectionView = UICollectionView (frame: CGRect.init x: y: (2 + 80, 2 + 64. Width: ScreenWidth - 80 - 4 - 64 - 4 height:, ScreenHeight), collectionViewLayout: self.flowlayout collectionView.delegate) = self collectionView.dataSource = self collectionView.showsVerticalScrollIndicator = false collectionView.showsHorizontalScrollIndicator = false collectionView.backgroundColor = UIColor.clear collectionView.register (CollectionViewCell.self forCellWithReuseIdentifier: kCollectionViewCell) Co LlectionView.register (CollectionViewHeaderView.self, forSupplementaryViewOfKind: UICollectionElementKindSectionHeader, withReuseIdentifier: kCollectionViewHeaderView) return collectionView (func)} tableView (tableView: UITableView cellForRowAt indexPath: IndexPath _, -> UITableViewCell) {let cell = tableView.dequeueReusableCell (withIdentifier: kLeftTableViewCell, for: indexPath) as LeftTableViewCell let model! = dataSource[indexPath.row] cell.nameLabel.text = model.name return cell func collectionView collectionView: UICollectionView (_}, cellForItemAt indexPath: IndexPath -> UICollectionViewCell) {let cell = collectionView.dequeueReusableCell (withReuseIdentifier: kCollectionViewCell, for: indexPath) as Collec! TionViewCell let model = collectionDatas[indexPath.section][indexPath.row] cell.setDatas (model) return cell}

First TableView Association CollectionView, click TableViewCell, the right of the CollectionView jump to the corresponding partition list header.

Func tableView (tableView: UITableView didSelectRowAt _, indexPath: IndexPath) {selectIndex = indexPath.row (collectionView.scrollToItem at: IndexPath (row: 0, section: selectIndex), at:.Top, animated: true tableView.scrollToRow (at:) IndexPath (row: selectIndex, section: 0), at:.Top, animated: true)}

Then the CollectionView Association TableView, mark the direction of the RightTableView scroll, and then in the CollectionView partition will be displayed and the end of the end of the proxy function to deal with logic.

  • In 1 CollectionView partition title will be presented, determine the current CollectionView rolling direction, CollectionView is a user generated drag roll (mainly about whether CollectionView is the user to drag and rolling, or click TableView and rolling), if the two are set up, then TableView section CollectionView is currently selected.
  • In 2 CollectionView partition title display end, determine the current direction of CollectionView rolling down, CollectionView is the user drag from rolling, if the two are set up, then TableView section-1 CollectionView is currently selected.
Mark CollectionView / / rolling direction, up or down func scrollViewDidScroll (_ scrollView: UIScrollView) {if collectionView = = scrollView {isScrollDown = lastOffsetY < scrollView.contentOffset.y lastOffsetY = scrollView.contentOffset.y}} / / CollectionView func collectionView (section headings will show _ collectionView: UICollectionView, willDisplaySupplementaryView view: UICollectionReusableView, forElementKind elementKind: String, at indexPath: IndexPath) {/ / the CollectionView rolling direction, CollectionView is a user generated drag roll (mainly about whether CollectionView is the user to drag the rolling and rolling, or click the TableView if isScrollDown &); & collect! IonView.isDragging (index: indexPath.section) {selectRow}} / / CollectionView func collectionView (end title display partition _ collectionView: UICollectionView, didEndDisplayingSupplementaryView view: UICollectionReusableView, forElementOfKind elementKind: String, at indexPath: IndexPath) {/ / CollectionView the rolling direction is downward, CollectionView users to drag from rolling the (mainly about whether CollectionView is the user to drag and rolling, or click on the TableView and if isScrollDown) rolling & & collectionView.isDragging {selectRow (index: indexPath.section + 1)}} / / when drag CollectionView, TableView private func selectRow (index: Int {tableView.selectRow (at: IndexPath) (row: index, section: 0), animated: true, scrollPosition:.Middle)}

Linkage between TableView and CollectionView to achieve this, is not very simple.

Linkage between [Swift linkage]: linkage between the two TableView, TableView and CollectionView linkage between
TableView and CollectionView

Last

Due to the limited level of the author, if there are errors in the text, or there is a better way, but also hope that God pointed out.
attached to all the demo download link, [GitHub – Swift version], [GitHub – OC version], with demo to see the article, the effect will be better.
if you feel that you have to help after reading, but also hope to point on the star GitHub. You share rose get fun。