ios - 在 tableview 的 swift 文件之间快速传递数组

标签 ios arrays swift uitableview

我在一个 viewcontroller 中有一个按钮操作连接到另一个 tableviewcontroller。我需要通过单击按钮传递一些文本字段值以显示在另一个表格 View 中。在第一个 View 中,我有一个全局数组:

var estRent = [AnyObject]()

在 buttonAction 中附加值:

@IBAction func calculateButtonAction(sender: UIButton) {
   ...
   estRent.append(weekRent)
   estRent.append(monthRent)
   estRent.append(yearRent)
   estRent.append(interestRate)
   estRent.append(loanAmount)
   estRent.append(monthInterest)
}

覆盖 prepareForSegue:

override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject!) {
    if (segue.identifier == "Load View") {
        let svc = segue.destinationViewController as! NextTableViewController
        svc.rentArray = self.estRent
    }
}

在NextTableViewController中,有一个全局数组:

var rentArray = [AnyObject]()

下一个 TableView 无法看到数组中的值。感谢任何帮助。

最佳答案

第一个 View Controller :

import UIKit

class ViewController: UIViewController {

    @IBOutlet weak var nameTextField: UITextField!
    @IBOutlet weak var ageTextField: UITextField!
    @IBOutlet weak var classTextField: UITextField!

    var estRent = [AnyObject]()

    @IBAction func goButtonAction(sender: UIButton) {
        estRent.append(nameTextField.text!)
        estRent.append(ageTextField.text!)
        estRent.append(classTextField.text!)
    }

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

    override func viewWillAppear(animated: Bool) {
        super.viewWillAppear(animated)
        [estRent .removeAll()]
    }
    override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject!) {
        print(estRent)
        if (segue.identifier == "LoadView") {
            let svc = segue.destinationViewController as! RentTableViewController
            svc.rentArray = self.estRent
        }
    }

}

RentTableViewController:

import UIKit

class RentTableViewController: UITableViewController {

    var rentArray = [AnyObject]()

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

    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }

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

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

        tableView.registerClass(UITableViewCell.self, forCellReuseIdentifier: "studentCell")
        let tableCell: UITableViewCell = tableView.dequeueReusableCellWithIdentifier("studentCell", forIndexPath: indexPath)
        let rentLabel: UILabel = UILabel.init(frame: CGRectMake(10, 10, 100, 25))
        rentLabel.text = rentArray[indexPath.row] as? String

        tableCell.addSubview(rentLabel)
        return tableCell


    }

}

Click here for Main.storyboard image

您必须在 Storyboard中提供一个 segue 标识符作为“LoadView”

Click here to view screen short for "How to give segue identifier"

关于ios - 在 tableview 的 swift 文件之间快速传递数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38886152/

相关文章:

c# - 为 RESTful 服务格式化 DateTime 的最佳方式?

ios - 通过 cocoa 绑定(bind)将数组与字典连接起来

c++ - 如何在 C++ 中创建带有引用成员的数组?

ios - 无法将类型 'UITableViewCell' 的值转换为指定类型

ios - 如何在 Xcode 中使用 Sprite Kit 添加粒子 sks 文件作为背景图像的叠加层?

ios - UICollectionView 重用单元格问题

php - (PHP) 如何对包含与变量相关的字符串的多维数组进行排序?

php - 如果子键存在则求和多维数组

iOS Google Maps API - View 不会快速覆盖 map

ios - 如果按得太久,快速长按将停止工作