使用结构和数组在表视图之间传递数据(Swift:Xcode)

时间:2016-07-18 08:05:04

标签: ios xcode

我是代码初学者,实际上几周前我已经加入了这个世界。 我试图通过使用Xcode和Swift为Ios构建我的第一个应用程序。 我想创建不同的表视图并在这些表之间传递数据。

现在,我编写了代码,但我不断收到#34;预期声明"错误。我真的不明白如何解决它。有人可以帮帮我吗? 谢谢。

//
//  ViewController.swift
//  Tot_Forum
//
//  Created by Fausto Saltetti on 18/07/16.
//  Copyright (c) 2016 Fausto Saltetti. All rights reserved.
//

import UIKit

class FirtTableViewController: UITableViewController {

var FirstTableArray = [String]()

var SecondArray = [SecondTable]()

override func viewDidLoad() {
    super.viewDidLoad()
    // Do any additional setup after loading the view, typically from a nib.
    FirstTableArray = ["Focus on learning", "Participate and network", "Access and build knowledge", "Assess, reflect, evaluate", "Inspire and generate ideas", "Problem solve and plan", "Map ideas and relationships"]

    SecondArray =
        [SecondTable(SecondTitle: ["After Action Review","Audience Response Systems","Blogs","Case Studies", "Discussion Forums UPDATE","Jigsaw","Peer Assist", "Podcasting","Presentations", "Role Play", "Screencasting", "Social Networking", "Sociometrics"]),
            SecondTable(SecondTitle: ["After Action Review","Audience Response Systems","Blogs","Case Studies", "Discussion Forums UPDATE","Jigsaw","Peer Assist", "Podcasting","Presentations", "Role Play", "Screencasting", "Social Networking", "Sociometrics"]),
            SecondTable(SecondTitle: ["After Action Review","Audience Response Systems","Blogs","Case Studies", "Discussion Forums UPDATE","Jigsaw","Peer Assist", "Podcasting","Presentations", "Role Play", "Screencasting", "Social Networking", "Sociometrics"])]

}

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

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

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

    var Cell = self.tableView.dequeueReusableCellWithIdentifier("Cell", forIndexPath: indexPath) as! UITableViewCell

    Cell.textLabel?.text = FirstTableArray[indexPath.row]

    return Cell

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

        var indexPath : NSIndexPath = self.tableView.indexPathForSelectedRow()!

        var DestViewController = segue.destinationViewController as! SecondTableViewController

        var SecondTableArrayTwo : SecondTable

        SecondTableArrayTwo = SecondArray[indexPath.row]

        DestViewController.SecondArray = SecondTableArrayTwo.SecondTitle
        }

}   **//Error here: !Expected declaration**

2 个答案:

答案 0 :(得分:1)

这里的错误是prepareForSegue函数中的cellForRowAtIndexPath函数。

它们应该在全球范围内

  

var SecondTableArrayTwo:SecondTable?应该用其初始化解包或可选。

override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
    var Cell = self.tableView.dequeueReusableCellWithIdentifier("Cell", forIndexPath: indexPath) as! UITableViewCell
    Cell.textLabel?.text = FirstTableArray[indexPath.row]
    return Cell
}  

func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
        var indexPath : NSIndexPath = self.tableView.indexPathForSelectedRow()!
        var DestViewController = segue.destinationViewController as! SecondTableViewController
        var SecondTableArrayTwo : SecondTable?
        SecondTableArrayTwo = SecondArray[indexPath.row]
        DestViewController.SecondArray = SecondTableArrayTwo!.SecondTitle
}

答案 1 :(得分:1)

从您收到的错误开始,“预期声明”错误是针对您班级的缺失括号“}”。

解决此错误会给您另一个警告,因为在返回语句之后编写的代码永远不会被执行。您已在return语句之后编写了prepareForSegue函数。

现在主要问题,prepareForSegue必须在全局范围内 @ÖzgürErsil在他的回答中提到。因此为您的班级添加一个结束括号。从cellForRowAtIndexPath函数中删除prepareForSegue函数,并将其写入类中的全局范围。

希望这会对你有所帮助:)。