【问题标题】:Swift- passing data from a multiple-section tableview to ViewControllerSwift-将数据从多节表视图传递到 ViewController
【发布时间】:2015-08-03 14:04:26
【问题描述】:

我有一个两部分的表格视图(见下图)

当我点击“A”行时,另一个表格视图会显示字母“A”

当我单击“B”行时,会出现另一个带有字母“B”的表格视图

当我单击 C 行时,如何将字母“C”传递给另一个 tableview?

这是我在 TableView 中的代码: 导入 UIKit

类 manhinh1: UITableViewController {

var UserDefault:NSUserDefaults!
var array:[[String]]!
override func viewDidLoad() {
    super.viewDidLoad()

    UserDefault = NSUserDefaults()
    array = [["A","B"],["C","D","E"]]

}

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

// MARK: - Table view data source

override func numberOfSectionsInTableView(tableView: UITableView) -> Int {
    // #warning Potentially incomplete method implementation.
    // Return the number of sections.
    return array.count
}

override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    // #warning Incomplete method implementation.
    // Return the number of rows in the section.
    if section == 0 {
    return array[0].count
    }
    if section == 1 {
    return array[1].count
    }
    return 1
}


override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCellWithIdentifier("Cell", forIndexPath: indexPath) as! UITableViewCell

    if indexPath.section == 0 {
    cell.textLabel?.text = array[0][indexPath.row]
    }
    if indexPath.section == 1 {
    cell.textLabel?.text = array[1][indexPath.row]
    }
    // Configure the cell...

    return cell
}

override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
    var row = self.tableView.indexPathForSelectedRow()!.row

    UserDefault.setObject(array[0][row], forKey: "selected")
}

}

这是第二个视图中的代码: 导入 UIKit

类视图控制器:UIViewController {

var UserDefault:NSUserDefaults!
@IBOutlet weak var lbldata: UILabel!
override func viewDidLoad() {
    super.viewDidLoad()
    UserDefault = NSUserDefaults()
    // Do any additional setup after loading the view, typically from a     }
    lbldata.text = UserDefault.objectForKey("selected") as! String
}
override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated.
}

}

我确实在 prepareForSegue 中尝试了一些代码,但没有成功。我希望你们能给我一个方法。谢谢

【问题讨论】:

    标签: ios iphone uitableview


    【解决方案1】:

    您必须获取行和部分才能访问您的多维数组。然后获取 segue 的目标视图控制器,并将数组中的相应值分配给 UserDefault 属性:

    override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
        var row = self.tableView.indexPathForSelectedRow()!.row
        var section = self.tableView.indexPathForSelectedRow()!.section
    
        var destinationViewController = segue.destinationViewController as! ViewController 
    
        destinationViewController.UserDefault.setObject(array[section][row], forKey: "selected")
    }
    

    【讨论】:

    • 是的,这就是我一直在寻找的。我一直在寻找类似 DidSelectSection 的东西,但 swift 没有。谢谢丹尼尔。顺便说一句,我确实为你的答案投票,但它说“一旦你总共获得 15 声望,你的投票将改变公开显示的帖子分数”。投票是在stackoverflow中表达感谢的方式吗?我是新来的。
    • 没问题 Tuan,点赞并将答案标记为正确有助于其他人将相关答案与不正确答案区分开来。如果您发布了正确的答案并被标记,那么您将获得声誉,这将为您提供更多权利,例如评论答案等。
    猜你喜欢
    • 2017-11-08
    • 2016-01-05
    • 1970-01-01
    • 1970-01-01
    • 2021-01-09
    • 2015-03-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多