-
Notifications
You must be signed in to change notification settings - Fork 372
/
LoadDataViewController.swift
89 lines (72 loc) · 3.25 KB
/
LoadDataViewController.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
//
// LoadDataViewController.swift
// JXSegmentedView
//
// Created by jiaxin on 2019/1/7.
// Copyright © 2019 jiaxin. All rights reserved.
//
import UIKit
import JXSegmentedView
class LoadDataViewController: UIViewController {
var segmentedDataSource: JXSegmentedTitleDataSource!
var segmentedView: JXSegmentedView!
var listContainerView: JXSegmentedListContainerView!
override func viewDidLoad() {
super.viewDidLoad()
view.backgroundColor = .white
//1、初始化JXSegmentedView
segmentedView = JXSegmentedView()
//2、配置数据源
//segmentedViewDataSource一定要通过属性强持有!!!!!!!!!
segmentedDataSource = JXSegmentedTitleDataSource()
segmentedDataSource.titles = getRandomTitles()
segmentedDataSource.isTitleColorGradientEnabled = true
segmentedView.dataSource = segmentedDataSource
//3、配置指示器
let indicator = JXSegmentedIndicatorLineView()
indicator.indicatorWidth = JXSegmentedViewAutomaticDimension
indicator.lineStyle = .lengthen
segmentedView.indicators = [indicator]
//4、配置JXSegmentedView的属性
view.addSubview(segmentedView)
//5、初始化JXSegmentedListContainerView
listContainerView = JXSegmentedListContainerView(dataSource: self)
view.addSubview(listContainerView)
//6、将listContainerView.scrollView和segmentedView.contentScrollView进行关联
segmentedView.listContainer = listContainerView
navigationItem.rightBarButtonItem = UIBarButtonItem(title: "刷新数据", style: UIBarButtonItem.Style.plain, target: self, action: #selector(reloadData))
}
@objc func reloadData() {
segmentedDataSource.titles = getRandomTitles()
segmentedView.defaultSelectedIndex = 1
segmentedView.reloadData()
}
override func viewDidLayoutSubviews() {
super.viewDidLayoutSubviews()
segmentedView.frame = CGRect(x: 0, y: 0, width: view.bounds.size.width, height: 50)
listContainerView.frame = CGRect(x: 0, y: 50, width: view.bounds.size.width, height: view.bounds.size.height - 50)
}
func getRandomTitles() -> [String] {
let titles = ["猴哥", "青蛙王子", "旺财", "粉红猪", "喜羊羊", "黄焖鸡", "小马哥", "牛魔王", "大象先生", "神龙"]
//随机title数量,4~n
let randomCount = Int(arc4random()%7 + 4)
var tempTitles = titles
var resultTitles = [String]()
for _ in 0..<randomCount {
let randomIndex = Int(arc4random()%UInt32(tempTitles.count))
resultTitles.append(tempTitles[randomIndex])
tempTitles.remove(at: randomIndex)
}
return resultTitles
}
}
extension LoadDataViewController: JXSegmentedListContainerViewDataSource {
func numberOfLists(in listContainerView: JXSegmentedListContainerView) -> Int {
return segmentedDataSource.dataSource.count
}
func listContainerView(_ listContainerView: JXSegmentedListContainerView, initListAt index: Int) -> JXSegmentedListContainerViewListDelegate {
let vc = LoadDataListViewController()
vc.typeString = segmentedDataSource.titles[index]
return vc
}
}