ios - 当我使用 Swift 4.2 和 iOS12 向左滑动时如何将第二个数组加载到 UITableView 中?

标签 ios swift uitableview ios12

我刚开始学习 Swift 编程。

当应用程序打开时,我正在将一个数组加载到 UITableView 中。 但是,当用户向左滑动时,同一个 UITableView 应该加载第二个数组,而他向右滑动时,UITableView 应该再次加载第一个数组。

我测试了下面的代码,它根据滑动改变颜色。但是,我无法加载第二个数组。

override func viewDidLoad() {
    super.viewDidLoad()

    let rightSwipe = UISwipeGestureRecognizer(target: self, action: #selector(handleSwipe(sender:)))
    view.addGestureRecognizer(rightSwipe)
}

@objc func handleSwipe(sender: UISwipeGestureRecognizer) {
    if sender.state == .ended {
        switch sender.direction {
        case .right:
            view.backgroundColor = .red
            print("Right swipe")
            let newCell = goNext(tblView, cellForRowAt: idxPath)
            print(newCell)
}

func goNext(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCell(withIdentifier: "chapterCell", for: idxPath)
    if currentChapter != totalChapters[0] {
        cell.textLabel?.numberOfLines = 0
        cell.textLabel?.text = genCh2[indexPath.row]
    }
    return cell
}

最佳答案

两件事:您需要一个 tableView 数据源,并且滑动手势有一个方向并且需要进行设置。

class MyViewController : UIViewController , UITableViewDataSource{

    var tblView : UITableView!
    var genCh1 = ["first","second","later"]
    var genCh2 = ["data","data1","data2"]
    var currentData : [String]?

    //TableViewDataSource
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return currentData?.count ?? 0
    }

    func numberOfSections(in tableView: UITableView) -> Int {
        return 1
    }

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "chapterCell", for: indexPath)
        //if currentChapter != totalChapters[0] {
        cell.textLabel?.numberOfLines = 0
        cell.textLabel?.text = currentData?[indexPath.row]
          //          }
                    return cell
    }

    func reloadData(_ array : [String]){
         currentData = array
        tblView.reloadData()
    }






    override func viewDidAppear(_ animated: Bool) {

    // setup viewController
        super.viewDidAppear(animated)

        tblView = UITableView.init(frame: view.bounds)
        tblView.backgroundColor = .red
               view.addSubview(tblView)
        tblView.dataSource = self
        tblView.register(UITableViewCell.self, forCellReuseIdentifier: "chapterCell")
        reloadData(genCh1)


        //add gesture

        let rightSwipe = UISwipeGestureRecognizer(target: self, action: #selector(handleSwipe(sender:)))
        rightSwipe.direction = .right
        view.addGestureRecognizer(rightSwipe)
        let leftSwipe = UISwipeGestureRecognizer(target: self, action: #selector(handleSwipe(sender:)))
        leftSwipe.direction = .left
        view.addGestureRecognizer(leftSwipe)
    }



    @objc func handleSwipe(sender: UISwipeGestureRecognizer) {
        if sender.state == .ended {
            switch sender.direction {
            case .right:
                view.backgroundColor = .red
                print("Right swipe")
               reloadData(genCh1)
            case .left:
                view.backgroundColor = .blue
                print("Left swipe")
             reloadData(genCh2)
            default:
                break;
            }}}
 }

关于ios - 当我使用 Swift 4.2 和 iOS12 向左滑动时如何将第二个数组加载到 UITableView 中?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54134454/

相关文章:

ios - UILocalNotification 会在 iOS 10 上崩溃吗

ios - 如何在选择的 UITableViewCell 行中传递值

php - 使用 PHP PDO 插入表 - 卡在execute() 语句上

ios - 在 Swift 中创建一个 JSON 对象

ios - 核心数据合并政策

ios - 将 UITableViewCell 放在 sibling 下面

ios - 如何使用 PDFKit 获取段落高度

ios - iOS 中包含 UIWebView 的单元格的动态 UITableViewCell 高度

ios - segue 不能与 swift 一起工作

ios - Fabric :/ios/Pods/Fabric/run”: No such file or directory