使用结构和数组在表视图之间传递数据(Swift : Xcode) [英] Passing Data Between Table Views using Structs and Arrays (Swift : Xcode)

查看:18
本文介绍了使用结构和数组在表视图之间传递数据(Swift : Xcode)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I'm a code beginner, actually I've joined this world a few weeks ago. I'm trying to build up my first app for Ios by using Xcode and Swift. I want to create different table views and passing data between these tables.

现在,我编写了代码,但我不断收到预期声明"错误.我真的不明白如何修复它.有人可以帮帮我吗?谢谢.

Now, I made up the code, but I keep getting an "Expected Declaration" error. I really don't understand how to fix it. Can someone please help me? Thanks.

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

推荐答案

从你得到的错误开始,预期声明"错误是因为你的类缺少大括号}".

Starting with the error you are getting, "Expected Declaration" error is for the missing brace "}" of your class.

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

Solving this error will give you another warning because code written after a return statement never get executed. You have written prepareForSegue function after the return statement.

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

Now the main issue, prepareForSegue must be in global scope as @Özgür Ersil mentioned in his answer. Thus add a closing brace for your class. Remove the prepareForSegue function from the cellForRowAtIndexPath function and write it in global scope inside your class.

希望对你有帮助:)

这篇关于使用结构和数组在表视图之间传递数据(Swift : Xcode)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆