gpt4 book ai didi

swift - 快速将 TableviewCell 转移到 AnotherTableView

转载 作者:行者123 更新时间:2023-11-28 08:42:52 25 4
gpt4 key购买 nike

我的项目中有两个 tableView(FavouriteViewController 和 MainViewController) Controller ,ViewController 通过带按钮的 segue 连接。我有一些数据在我的 MainViewController 和 FavouriteViewController 中运行,基本上没有数据。当单元格从 MainViewController 单击到 favouriteViewController 时,我尝试使用 tableViewCell 滑动操作方法或任何其他建议方法传输 tableViewCell。

我的代码如下。

我的主视图 Controller

var arrays = ["Alpha","Beta","Gamma","Phill","Below","Above","Clean",]

var sendSelectedData = NSString()

override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.

title = "FavTV"

let titlebutton: UIButton = UIButton(frame: CGRectMake(0, 0, 100, 32))
titlebutton.setTitle("Quote", forState: UIControlState.Normal)
titlebutton.titleLabel?.font = UIFont(name: "PartyLetPlain", size: 35)
titlebutton.setTitleColor(UIColor.redColor(), forState: UIControlState.Normal)
titlebutton.addTarget(self, action: "titlePresed", forControlEvents: UIControlEvents.TouchUpInside)
self.navigationItem.titleView = titlebutton

self.tableView.registerClass(UITableViewCell.self, forCellReuseIdentifier: "Cell1")
self.tableView.reloadData()

}

func titlePressed() {

func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
self.performSegueWithIdentifier("ShowDetails", sender: nil)
segue.destinationViewController as! favTableViewController
}
self.performSegueWithIdentifier("ShowDetails", sender: nil)
}

override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {

let indexPath = tableView.indexPathForSelectedRow!
let currentCell = tableView.cellForRowAtIndexPath(indexPath) as UITableViewCell!

sendSelectedData = (currentCell.textLabel?.text)! as String

//self.arrays.append(String(indexPath.row)
// performSegueWithIdentifier("ShowDetails", sender: self)

}

override func tableView(tableView: UITableView,commitEditingStyle editingStyle: UITableViewCellEditingStyle,forRowAtIndexPath indexPath: NSIndexPath) {

}

///////////////////////
override func tableView(tableView: UITableView,commitEditingStyle editingStyle: UITableViewCellEditingStyle,forRowAtIndexPath indexPath: NSIndexPath) {
switch editingStyle {
case .Delete:
// remove the deleted item from the model
self.arrays.removeAtIndex(indexPath.row)

performSegueWithIdentifier("ShowDetails", sender: self)

tableView.reloadData()


default:
return
}
}
/////////////////////

override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {

if (segue.identifier == "ShowDetails") {

// initialize new view controller and cast it as your view controller
let viewController = segue.destinationViewController as! favTableViewController
// your new view controller should have property that will store passed value
viewController.arrayx = [sendSelectedData as String]

///////////////////////
self.arrays.append(sendSelectedData as String)
/////////////////////


}

我最喜欢的表格 View :

var arrayx = []
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.

title = "Fav"

}

override func numberOfSectionsInTableView(tableView: UITableView) -> Int {

return 1
}


override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return arrayx.count
}


override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {

let cell = tableView.dequeueReusableCellWithIdentifier("Cell", forIndexPath: indexPath) as UITableViewCell

cell.textLabel?.text = arrayx[indexPath.row] as? String

return cell

}
}

当我按下 MainViewController 中的单元格然后按下 titlePressed() 按钮时,上面的代码以一种简单的方式工作。它将按下的单元格数据传递给 FavouriteTableView....

我需要一些关于如何将单元格永久转移到我的 FavouriteTableView 的建议。我不确定我的逻辑是否正确。请有人给我指出正确的方向。

提前致谢...

最佳答案

表中显示的内容由以下因素驱动:

var arrayx = [] // <-- CHANGE THE CONTENTS OF THIS ARRAY

更改该数组中的数据,然后重新加载表。

dispatch_async(dispatch_get_main_queue()) {
self.tableView.reloadData()
}

这是获取数据并将其放入单元格的函数。

override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {

let cell = tableView.dequeueReusableCellWithIdentifier("Cell", forIndexPath: indexPath) as UITableViewCell
// arrayx is the data that drives the table.
// changing the data in arrayx will cause the table to show different data.


// **********************************************
// ** The table will show whatever is in arrayx
// ** therefore, if you change arrayx then table
// ** will have that new data.
// **********************************************
cell.textLabel?.text = arrayx[indexPath.row] as? String
// **********************************************


return cell

}

关于swift - 快速将 TableviewCell 转移到 AnotherTableView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36011562/

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