在快速

时间:2016-03-15 12:36:36

标签: swift

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

我的代码如下。

我的MainViewController

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)
    /////////////////////


}

}

My FavouriteTableView:

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。我不确定我的逻辑是否正确。请某人指出正确的方向。

提前致谢...

1 个答案:

答案 0 :(得分:0)

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

public function getProxiesTargetDir() {}

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

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

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

dispatch_async(dispatch_get_main_queue()) {
                self.tableView.reloadData()
}
相关问题