【问题标题】:How to show one tableview data in two segments in swift如何快速在两个段中显示一个tableview数据
【发布时间】:2021-04-16 03:47:57
【问题描述】:

我正在使用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
}
}

【问题讨论】:

    标签: swift segment


    【解决方案1】:

    你需要像这样投射你的视图控制器

    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")
        
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2023-03-22
      • 2016-03-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-03-15
      • 1970-01-01
      相关资源
      最近更新 更多