swift / parse:将解析数据设置为标签

时间:2016-01-04 23:27:18

标签: swift parse-platform uilabel

我要做的是按照对象顺序从列中检索Parse中的数据。所有标签都连接到各自的插座,所有输出都会检索到正确的数据。

enter image description here

enter image description here

当我在桌面视图中运行并打开一个单元格时,它会崩溃并在此行上显示主题1:EXC_BAD_INSTRUCTION(代码= EXC> I386_INVOP,子代码= 0x0)self.navBar.topItem?.title = output1如果我选择第一个单元格,然后选择此行:self.navBar.topItem?.title = output1b如果我选择第二个单元格。

这是完整的功能:

firstObject正在抓取"eventsdetail"

中的第一个对象

secondObject正在抓取"eventsdetail"列中的第二个对象

func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {

    var query = PFQuery(className: "eventsdetail")
    let runkey = query.orderByAscending("ID")
    runkey.findObjectsInBackgroundWithBlock {
        (objects: [PFObject]?, error : NSError?) -> Void in

        if error == nil {

            if let objects = objects as [PFObject]! {
                for object in objects {

                    var firstObject = objects[0]
                    var secondObject = objects[1]

                    let output1 = firstObject.objectForKey("navTitle") as! String!
                    let output2 = firstObject.objectForKey("articleTitle") as! String!
                    let output3 = firstObject.objectForKey("written") as! String!
                    let output4 = firstObject.objectForKey("date") as! String!
                    let output5 = firstObject.objectForKey("article") as! String!

                    let output1b = secondObject.objectForKey("navTitle") as! String!
                    let output2b = secondObject.objectForKey("articleTitle") as! String!
                    let output3b = secondObject.objectForKey("written") as! String!
                    let output4b = secondObject.objectForKey("date") as! String!
                    let output5b = secondObject.objectForKey("article") as! String!

                    if indexPath.row == 0 {
                        self.performSegueWithIdentifier("0a", sender: nil)
                        self.tableview.deselectRowAtIndexPath(indexPath, animated: true)
                        self.navBar.topItem?.title = output1
                        self.articleTitle.text = output2
                        self.writtenBy.text = output3
                        self.date.text = output4
                        self.article.text = output5

                    } else if indexPath.row == 1 {
                        self.performSegueWithIdentifier("0a", sender: nil)
                        self.tableview.deselectRowAtIndexPath(indexPath, animated: true)
                        self.navBar.topItem?.title = output1b
                        self.articleTitle.text = output2b
                        self.writtenBy.text = output3b
                        self.date.text = output4b
                        self.article.text = output5b

                    }

                }
            }

        }

    }

}

如果有更简单的方法,请提及它,如果不是尝试解决此方法的问题。我知道这不是最干净的做事方式。

1 个答案:

答案 0 :(得分:0)

我不确定您如何解析数据,但如果它可以帮助您,请按照以下方式进行操作:

//
//  Test.swift
//  Test
//
//  Created by Charles-Olivier Demers on 16-01-04.
//

import UIKit
//import Parse

class EventViewController: UIViewController, UITableViewDelegate {
    private var _info = [EventDetails]()

    override func viewDidLoad() {
        fetchData()
    }

    func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
        self.performSegueWithIdentifier("0a", sender: nil)
        self.tableview.deselectRowAtIndexPath(indexPath, animated: true)
        self.navBar.topItem?.title.text = _info[indexPath.row].navTitle()
        self.articleTitle.text = _info[indexPath.row].articleTitle()
        self.writtenBy.text = _info[indexPath.row].writtenBy()
        self.date.text = _info[indexPath.row].date()
        self.article.text = _info[indexPath.row].article()
    }


    func fetchData() {
        let query = PFQuery(className: "eventsDetails")

        query.orderByAscending("ID")
        query.findObjectsInBackgroundWithBlock { (objects: [PFObject]?, error: NSError?) -> Void in

            if error == nil {

                if let objects = objects {

                    for object in objects {
                        let navTitleObject = object["navTitle"] as! String
                        let articleTitleObject = object["articleTitle"] as! String
                        let writtenByObject = object["writtenByObject"] as! String
                        let dateObject = object["dateObject"] as! String
                        let articleObject = object["articleObject"] as! String

                        _info.append(EventDetails(navTitle: navTitleObject, articleTitle: articleTitleObject, writtenBy: writtenByObject, date: dateObject, article: articleObject))

                    }

                }
            }

            else {
                print("Error #\(error!.code)")
            }
        }
    }
}

class EventDetails {

    private var _navTitle: String!
    private var _articleTitle: String!
    private var _writtenBy: String!
    private var _date: String!
    private var _article: String!

    init(navTitle: String, articleTitle: String, writtenBy: String, date: String, article: String) {
        self._navTitle = navTitle
        self._article = articleTitle
        self._writtenBy = writtenBy
        self._date = date
        self._article = article
    }

    func navTitle() -> String {
        return _navTitle
    }

    func articleTitle() -> String {
        return _articleTitle
    }

    func writtenBy() -> String {
        return _writtenBy
    }

    func date() -> String {
        return _date
    }

    func article() -> String {
        return _article
    }

}

首先,我将创建一个名为EventDetails的类。该类将获取Parse上EventsDetails类的所有属性。因此,当您获取数据时,可以将您在Swift中的EventDetails类数组中提取的数据附加到其中。

之后在

func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) 

使用indexPath.row获取EventDetails数组中的值,然后填充表视图。

我就是这样做的。