This is the sixth day of my participation in the August More text Challenge. For details, see:August is more challenging

preface

In the last article, we processed the navigation bar, banner and arc background of the home page. This article, we will start to process the interface data

The request data

When encapsulating the network request, we have already written the interface of the home page, but now we need to add age to the request parameter, so we can obtain data according to the age

extension Network {

    enum Home {
        case list(path: String, age: Int)
    }
}
Copy the code

Define an enumeration in RecViewController, where path is the different parts of the request interface

Var var var var var var var var var var var var var var var var var var String { switch self { case .rec: return "tj3" case .song: return "eg3" case .animation: return "dh3" case .book: return "hb3" case .story: return "gs3" } } }Copy the code

Data request

private func requestData() {
    Network.Home
        .list(path: from.path, age: 1)
        .request()
        .responseData(RecModel.self) { (model) in

    } failure: { (error) in
        Toast.show(info: error.errorMessage)
    }
}
Copy the code

Drop refresh we use MJRefresh

let header = MJRefreshNormalHeader(refreshingBlock: { [weak self] in guard let `self` = self else { return } self.requestData() }) header.lastUpdatedTimeLabel? .isHidden = true header.stateLabel? .isHidden = true collectionView.mj_header = header collectionView.mj_header? .beginRefreshing()Copy the code

Does it feel kind of gross to write this everywhere, and import MJRefresh every time? Both UICollectionView and UITableView inherit from UIScrollView, so we’re going to give UIScrollView an extension to make it as simple as possible

extension UIScrollView { func mj_headerRefresh(_ handler: @escaping () -> Void) { let headerRefresh = MJRefreshNormalHeader() headerRefresh.lastUpdatedTimeLabel? .isHidden = true headerRefresh.stateLabel? .isHidden = true headerRefresh.refreshingBlock = hander mj_header = headerRefresh } func mj_footerRefresh(_ handler: @escaping () -> Void) { let footerRefresh = MJRefreshBackStateFooter() footerRefresh.refreshingBlock = hander mj_footer = footerRefresh } func beginRefreshing() { mj_header? .beginRefreshing() } func endHeaderRefresh() { if let mjHeader = mj_header, mjHeader.isRefreshing { mj_header? .endRefreshing() } } func endFooterRefresh() { if let mjFooter = mj_footer, mjFooter.isRefreshing { mj_footer? .endRefreshing() } } var isHeaderRefresh: Bool { if let mjHeader = mj_header { return mjHeader.isRefreshing } return false } var isFooterRefresh: Bool { if let mjFooter = mj_footer { return mjFooter.isRefreshing } return false } }Copy the code

In the RecViewController, you can use it like this, so it’s a lot easier

collectionView.mj_headerRefresh { [weak self] in
    guard let `self` = self else { return }
    self.requestData()
}
collectionView.beginRefreshing()
Copy the code

Next, the data of recommendation, singing children’s songs, watching sports, reading picture books and listening to stories were obtained respectively. Top_items is the banner’s data. You can see that when you have top_items, you have banners.

So here’s what I’m going to do: The number of UICollectionView groups is equal to more_items.count+2, and top_items are grouped together with icon_items, so +2

func numberOfSections(in collectionView: UICollectionView) -> Int { return dataSource.count + 2 } func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {/// banner if section == 0 {return 1} /// Icon under banner if section == 1 {return 1} /// Other cell switch dataSource[section - 2].layoutType { case .slide: return 1 default: return dataSource[section - 2].viewP.itemCount } } func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell { switch indexPath.section { case 0: /// banner let cell = collectionView.dequeueReusableCell(withClass: RecBannerCell.self, for: indexPath) cell.banners = banners return cell case 1: / / / under the banner of five icon let cell = collectionView. DequeueReusableCell (withClass: RecHeaderItemCell. Self, for: indexPath) cell.items = items return cell default: let model = dataSource[indexPath.section - 2] switch model.layoutType { case .big: let cell = collectionView.dequeueReusableCell(withClass: RecCell.self, for: indexPath) cell.update(big: model.data.items[indexPath.item]) return cell case .big_list: let cell = collectionView.dequeueReusableCell(withClass: RecBigListCell.self, for: indexPath) cell.update(model.data.items[indexPath.item]) return cell case .slide: let cell = collectionView.dequeueReusableCell(withClass: RecSlideCell.self, for: indexPath) cell.items = model.data.items.prefix(model.viewP.itemCount).map({ $0 }) return cell case .book: let cell = collectionView.dequeueReusableCell(withClass: RecBigListCell.self, for: indexPath) cell.update(slide: model.data.items[indexPath.item]) return cell case .store: let cell = collectionView.dequeueReusableCell(withClass: RecStoreCell.self, for: indexPath) cell.update(model.data.items[indexPath.item]) return cell case .store_list: let cell = collectionView.dequeueReusableCell(withClass: RecStoreListCell.self, for: indexPath) cell.update(model.data.items[indexPath.item]) return cell case .hot: let cell = collectionView.dequeueReusableCell(withClass: RecCell.self, for: indexPath) cell.update(hot: model.data.items[indexPath.item]) return cell default: let cell = collectionView.dequeueReusableCell(withClass: RecCell.self, for: indexPath) cell.update(model.data.items[indexPath.item]) return cell } } }Copy the code

That’s basically the end of the home page, so let’s take a look at the results: