swift - 如何快速在两个段中显示一个tableview数据
问题描述
我正在使用XLPagerTabStrip进行分段
我想在两个段中显示一个 tableview 数据并排序,因为我正在使用下面的代码
SegmentViewController 代码:如果我添加child_2.isSorted = false
则错误
'UIViewController' 类型的值没有成员 'isSorted'
import UIKit
import XLPagerTabStrip
class SegmentViewController: ButtonBarPagerTabStripViewController {
@IBOutlet weak var testSeg: ButtonBarView!
override func viewDidLoad() {
super.viewDidLoad()
// change selected bar color
// Sets the background colour of the pager strip and the pager strip item
settings.style.buttonBarBackgroundColor = .white
settings.style.buttonBarItemBackgroundColor = .yellow
// Sets the pager strip item font and font color
settings.style.buttonBarItemFont = UIFont(name: "Helvetica", size: 15.0)!
settings.style.buttonBarItemTitleColor = .gray
changeCurrentIndexProgressive = { [weak self] (oldCell: ButtonBarViewCell?, newCell: ButtonBarViewCell?, progressPercentage: CGFloat, changeCurrentIndex: Bool, animated: Bool) -> Void in
guard changeCurrentIndex == true else { return }
oldCell?.label.textColor = .gray
newCell?.label.textColor = .blue
}
// Do any additional setup after loading the view.
}
override func viewDidLayoutSubviews() {
super.viewDidLayoutSubviews()
self.buttonBarView.frame = testSeg.frame
}
override func viewControllers(for pagerTabStripController: PagerTabStripViewController) -> [UIViewController] {
let child_1 = UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "SegTableViewController")
let child_2 = UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "SegTableViewController")
child_2.isSorted = false
return [child_1, child_2]
}
}
SegTableViewController 代码:在这两个部分中只显示sortedArray
我在第一部分sortedArray
和第二部分中需要namesArray
.. 怎么做.. 任何人都可以建议我
import UIKit
import XLPagerTabStrip
class SegTableViewController: UIViewController, UITableViewDelegate, UITableViewDataSource, IndicatorInfoProvider {
var sortedArray = [String]()
var isSorted = true
func indicatorInfo(for pagerTabStripController: PagerTabStripViewController) -> IndicatorInfo {
if isSorted{
return IndicatorInfo(title: "Sorted")
}else{
return IndicatorInfo(title: "Normal")
}
}
@IBOutlet weak var tableView: UITableView!
var namesArray = ["afsdf","ddsfsdf", "hjhgjh", "trytryr", "nvbmvnb", "yuertyri", "bmvmncb", "jgfhk", "ytuioi", "sdfgsfdsh", "mkhjgijik", "gfuyru"]
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
sortedArray = namesArray.sorted { $0.localizedCaseInsensitiveCompare($1) == ComparisonResult.orderedDescending }
self.tableView.register(UITableViewCell.self, forCellReuseIdentifier: "cell")
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
if isSorted {
return sortedArray.count
}
else{
return namesArray.count
}
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "cell") as! UITableViewCell
if isSorted{
cell.textLabel?.text = sortedArray[indexPath.row]
}
else{
cell.textLabel?.text = namesArray[indexPath.row]
}
return cell
}
}
解决方案
你需要像这样投射你的视图控制器
override func viewControllers(for pagerTabStripController: PagerTabStripViewController) -> [UIViewController] {
let child_1 = UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "SegTableViewController")
let child_2 = UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "SegTableViewController")
(child_2 as? SegTableViewController).isSorted = false
return [child_1, child_2]
}
此外,不需要用于显示数据的数组。namesArray
像这样只使用一个数组
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
if isSorted {
namesArray = namesArray.sorted { $0.localizedCaseInsensitiveCompare($1) == ComparisonResult.orderedDescending }
}
self.tableView.register(UITableViewCell.self, forCellReuseIdentifier: "cell")
}
推荐阅读
- node.js - 如何在nodejs应用程序的swagger-ui中指定多个基本路径
- android - 斑马扫描仪 LI4278 有时会在代码中间输入 enter
- java - 如果列表为空/null,则 IN 子句选择所有元素
- html - 从所有类型的 li 和 ul 中删除黑点
- php - 如何在列中显示“choice()”的控制台提示?
- sql - sql powerapps 创建关系
- abap - ABAP CDS 视图中的 DATS 到字符串转换
- shell - 如何将 awk 与循环结合使用
- mongodb - 使用 Spark 和 MongoDb 按州查找最大和最小的城市
- python - 从循环中的列中减去一个值会得到 NaN