无法在Swift

时间:2016-07-03 10:36:23

标签: ios swift uitableview

我想在另一个viewController上显示一个表行的详细信息。但是它显示一个错误,说'致命错误:在解开一个Optional值时意外发现nil'

我的VC代码如下:

import UIKit

class ViewController: UIViewController, UITextFieldDelegate, UITableViewDelegate,UITableViewDataSource, UICollectionViewDataSource,   UICollectionViewDelegate {

@IBOutlet var nameForUser: UITextField!
@IBOutlet var loginButton: UIButton!

@IBOutlet var tableView: UITableView!


let allEvents = Events.allEvents
var nextScreenRow: Events!
override func viewDidLoad() {
    super.viewDidLoad()
    //nameForUser.text! = "Please Enter Name Here"
    // Do any additional setup after loading the view, typically from a nib.



}

func textFieldDidBeginEditing(textField: UITextField) {
    textField.text = ""
}



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

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

    let cell = tableView.dequeueReusableCellWithIdentifier("eventsCell")!
    let event = self.allEvents[indexPath.row]

    cell.textLabel?.text = event.eventName
    cell.imageView?.image = UIImage(named: event.imageName)
    cell.detailTextLabel?.text = event.entryType
    //cell.textLabel?.text = allEvents[indexPath.row]

    return cell
}

func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
    tableView.deselectRowAtIndexPath(indexPath, animated: true)

    nextScreenRow = allEvents[indexPath.row]

    performSegueWithIdentifier("tryToConnect", sender:self)

}

func tableView(tableView: UITableView, canEditRowAtIndexPath indexPath: NSIndexPath) -> Bool {
    return true
}

func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
    return self.allEvents.count
}

func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
    let sec = collectionView.dequeueReusableCellWithReuseIdentifier("eventsSec", forIndexPath: indexPath) as! GridCollectionViewCell

    let event = self.allEvents[indexPath.row]


     sec.imageView.image = UIImage(named: event.imageName)

     sec.caption.text = event.entryType

    return sec
}
func collectionView(collectionView: UICollectionView, didSelectItemAtIndexPath indexPath: NSIndexPath) {
    performSegueWithIdentifier("tryToConnect2", sender: self)
}

override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
        if (segue.identifier == "successfulLogin"){
        segue.destinationViewController as! TabController
        //let userName = nameForUser.text
        //controller.userName = userName

    }

    else if (segue.identifier == "tryToConnect"){
            let dest = segue.destinationViewController as! DetailedEventViewController
            dest.deatiledEvent.text = nextScreenRow.eventName
            dest.eventType.text = nextScreenRow.entryType
            dest.imageView.image = UIImage(named: nextScreenRow.imageName)
            }


}



@IBAction func loginButtonWhenPressed(sender: UIButton) {
    let userName = nameForUser.text

    if userName == "" {
        let nextController = UIAlertController()
        nextController.title = "Error!"
        nextController.message = "Please enter a name"

        let okAction = UIAlertAction(title: "okay", style: UIAlertActionStyle.Default) {
            action in self.dismissViewControllerAnimated(true, completion: nil)
        }

        nextController.addAction(okAction)
        self.presentViewController(nextController, animated: true, completion: nil)
    }
}  

}

当我运行它时,它会显示错误。我还为表视图分配了委托。 'DetailedEventVC'的代码是:

import UIKit

class DetailedEventViewController: UIViewController {

@IBOutlet weak var imageView: UIImageView!
@IBOutlet weak var deatiledEvent: UILabel!
@IBOutlet weak var eventType: UILabel!


override func viewDidLoad() {
    super.viewDidLoad()

    // Do any additional setup after loading the view.
}

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


/*
// MARK: - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
    // Get the new view controller using segue.destinationViewController.
    // Pass the selected object to the new view controller.
}
*/

}

为什么显示值为零? 非常感谢帮助。 提前致谢。 具有事件细节的'EventsDetails.swift'文件是一种结构。我称之为价值观的方式有什么不对吗?

import Foundation
import UIKit

struct Events {
let eventName: String
let entryType: String
let imageName: String

static let EventKey = "NameKey"
static let EntryTypeKey = "EntryType"
static let ImageNameKey = "ImageNameKey"

init(dictionary:[String : String]) {
    self.eventName = dictionary[Events.EventKey]!
    self.entryType = dictionary[Events.EntryTypeKey]!
    self.imageName = dictionary[Events.ImageNameKey]!
}
}

extension Events {

static var allEvents: [Events] {

    var eventsArray = [Events]()

    for d in Events.localEventsData(){
        eventsArray.append(Events(dictionary: d))
    }

    return eventsArray
}

static func localEventsData()-> [[String: String]] {

    return [

        [Events.EventKey:"Metallica Concert in Palace Grounds", Events.EntryTypeKey: "Paid Entry", Events.ImageNameKey:"Metallica"],
        [Events.EventKey:"Saree Exhibition in Malleswaram Grounds", Events.EntryTypeKey: "Free Entry", Events.ImageNameKey:"SareeExhibition"],
        [Events.EventKey:"Wine tasting event in Links Brewery", Events.EntryTypeKey: "Paid Entry", Events.ImageNameKey:"WineTasting"],
        [Events.EventKey:"Startups Meet in Kanteerava Stadium", Events.EntryTypeKey: "Paid Entry", Events.ImageNameKey:"StartupMeet"],
        [Events.EventKey:"Summer Noon Party in Kumara Park", Events.EntryTypeKey: "Paid Entry", Events.ImageNameKey:"SummerNoonParty"],
        [Events.EventKey:"Rock and Roll nights in Sarjapur Road", Events.EntryTypeKey: "Paid Entry", Events.ImageNameKey:"RockNRollNight"],
        [Events.EventKey:"Barbecue Fridays in Whitefield", Events.EntryTypeKey: "Paid Entry", Events.ImageNameKey:"BBQFriday"],
        [Events.EventKey:"Summer workshop in Indiranagar", Events.EntryTypeKey: "Free Entry", Events.ImageNameKey:"SummerWorkshop"],
        [Events.EventKey:"Impressions & Expressions in MG Road", Events.EntryTypeKey: "Free Entry", Events.ImageNameKey:"ImpressionAndExpression"],
        [Events.EventKey:"Italian carnival in Electronic City", Events.EntryTypeKey: "Free Entry", Events.ImageNameKey:"ItalianCarnival"]

    ]
}
}

1 个答案:

答案 0 :(得分:1)

在目标viewController中创建属性并使用它们,如下所示

import UIKit

class DetailedEventViewController: UIViewController {

@IBOutlet weak var imageView: UIImageView!
@IBOutlet weak var deatiledEvent: UILabel!
@IBOutlet weak var eventType: UILabel!
var myImage = UIImage?
var eventDetails = ""
var typeOfEvent = ""

override func viewDidLoad() {
super.viewDidLoad()
imageView.image = myImage
deatiledEvent.text = eventDetails
eventType.text = typeOfEvent
// Do any additional setup after loading the view.
}

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

然后在你的第一个viewController中你可以像

一样访问它们
let dest = segue.destinationViewController as! DetailedEventViewController
        dest.eventDetails = nextScreenRow.eventName
        dest.typeOfEvent = nextScreenRow.entryType
        dest.myImage = UIImage(named: nextScreenRow.imageName)