gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-28 07:33:00 24 4
gpt4 key购买 nike

我刚开始学习 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/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com