【问题标题】:Connecting a PFUITableView and Detail View using Parse and Swift in Xcode在 Xcode 中使用 Parse 和 Swift 连接 PFUITableView 和 Detail View
【发布时间】:2015-06-29 05:20:50
【问题描述】:

我正在尝试从 Parse 数据库设置表视图。每个单元格应包含关于一个对象的多条数据(在 Parse 中编码为 what 和 where)。轻按时,它应该转换到一个新页面,显示有关该对象的所有可用数据(包括用户名和时间)。

这是我的表格视图代码:

import UIKit
import Parse
import ParseUI

class TableView: PFQueryTableViewController {

override init(style: UITableViewStyle, className: String!) {
    super.init(style: style, className: className)
}

required init(coder aDecoder: NSCoder) {
    super.init(coder: aDecoder)

    // Configure the PFQueryTableView
    self.parseClassName = "ItemRequest"
    self.textKey = "what"
    self.pullToRefreshEnabled = true
    self.paginationEnabled = false
}

    override func queryForTable() -> PFQuery {
        var query = PFQuery(className: "ItemRequest")
        query.orderByAscending("what")
        return query
}

//override func tableView(tableView: UITableView, cellForRowAtIndexPath       
indexPath: NSIndexPath) -> UITableViewCell
override func tableView(tableView: UITableView, cellForRowAtIndexPath     
indexPath: NSIndexPath, object: PFObject?) -> PFTableViewCell {

    var cell = tableView.dequeueReusableCellWithIdentifier("Cell") as! PFTableViewCell!
    if cell == nil {
        cell = PFTableViewCell(style: UITableViewCellStyle.Default, reuseIdentifier: "Cell")
    }

    // Extract values from the PFObject to display in the table cell
    if let what = object?["what"] as? String {
        cell?.textLabel?.text = what
    }
    if let `where` = object?["`where`"] as? String {
        cell?.detailTextLabel?.text = `where`
    }

    return cell
}

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

    func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: 
    NSIndexPath) {
        self.performSegueWithIdentifier("showDetails", sender: tableView)
    }


    var detailScene = segue.destinationViewController as! DetailViewController


    if let indexPath = self.tableView.indexPathForSelectedRow() {
        let row = Int(indexPath.row)
        detailScene.currentObject = (objects?[row] as! PFObject)
    }

}

}

以及详细视图的代码:

import UIKit
import Parse


class DetailViewController: TableView {

var currentObject : PFObject?

@IBOutlet var whatis: UILabel!

@IBOutlet var whereis: UILabel!

@IBOutlet var whenis: UILabel!

@IBOutlet var whois: UILabel!

@IBOutlet var priceis: UILabel!

override func viewDidLoad() {
    super.viewDidLoad()

        if let object = currentObject {
            whatis.text = (object["what"] as! String)
            whereis.text = (object["`where`"] as! String)
            priceis.text = (object["price"] as! String)
            whenis.text=(object["when"] as! String)
        }
    }
        // Do any additional setup after loading the view.

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

}

当我运行时,表格只显示“什么”数据点,没有其他内容。当我尝试选择一行时,它不会转到下一个屏幕(我确实在情节提要中设置了 segue)。不会出现错误。任何帮助将不胜感激 - Swift 和一般编程的新手!

更新:尝试了下面建议的代码并收到 Thread1 Signal Sigabrt 错误。错误日志如下:

Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: '-[UITableViewController loadView] loaded the "uB8-vY-KsT-view-Ky8-TR-c7F" nib but didn't get a UITableView.'
*** First throw call stack:
(
    0   CoreFoundation                      0x00c24746 __exceptionPreprocess + 182
    1   libobjc.A.dylib                     0x0295ea97 objc_exception_throw + 44
    2   CoreFoundation                      0x00c2466d +[NSException raise:format:] + 141
    3   UIKit                               0x01aeb772 -[UITableViewController loadView] + 289
    4   ParseStarterProject                 0x0010c41b -[PFQueryTableViewController loadView] + 51
    5   UIKit                               0x018e5aef -[UIViewController loadViewIfRequired] + 78
    6   UIKit                               0x018e6095 -[UIViewController view] + 35
    7   UIKit                               0x01f472cc -[_UIFullscreenPresentationController _setPresentedViewController:] + 75
    8   UIKit                               0x018ba4f1 -[UIPresentationController initWithPresentedViewController:presentingViewController:] + 113
    9   UIKit                               0x018f36cc -[UIViewController _presentViewController:withAnimationController:completion:] + 2134
    10  UIKit                               0x018f6382 __62-[UIViewController presentViewController:animated:completion:]_block_invoke + 345
    11  UIKit                               0x018f61d4 -[UIViewController presentViewController:animated:completion:] + 224
    12  UIKit                               0x018fb02b -[UIViewController _showViewController:withAction:sender:] + 213
    13  UIKit                               0x01b38d32 -[UIStoryboardShowSegue perform] + 143
    14  UIKit                               0x01d8c297 -[UIStoryboardSegueTemplate _perform:] + 217
    15  UIKit                               0x018e8330 -[UIViewController performSegueWithIdentifier:sender:] + 72
    16  ParseStarterProject                 0x00106dec _TFC19ParseStarterProject9LendItems9tableViewfS0_FTCSo11UITableView23didSelectRowAtIndexPathCSo11NSIndexPath_T_ + 268
    17  ParseStarterProject                 0x00106e99 _TToFC19ParseStarterProject9LendItems9tableViewfS0_FTCSo11UITableView23didSelectRowAtIndexPathCSo11NSIndexPath_T_ + 89
    18  UIKit                               0x0189c2a6 -[UITableView _selectRowAtIndexPath:animated:scrollPosition:notifyDelegate:] + 1559
    19  UIKit                               0x0189c451 -[UITableView _userSelectRowAtPendingSelectionIndexPath:] + 285
    20  UIKit                               0x018a1795 __38-[UITableView touchesEnded:withEvent:]_block_invoke + 43
    21  UIKit                               0x017ab862 ___afterCACommitHandler_block_invoke + 15
    22  UIKit                               0x017ab80d _applyBlockToCFArrayCopiedToStack + 415
    23  UIKit                               0x017ab622 _afterCACommitHandler + 549
    24  CoreFoundation                      0x00b4586e __CFRUNLOOP_IS_CALLING_OUT_TO_AN_OBSERVER_CALLBACK_FUNCTION__ + 30
    25  CoreFoundation                      0x00b457b0 __CFRunLoopDoObservers + 400
    26  CoreFoundation                      0x00b3b1ea __CFRunLoopRun + 1226
    27  CoreFoundation                      0x00b3aa5b CFRunLoopRunSpecific + 443
    28  CoreFoundation                      0x00b3a88b CFRunLoopRunInMode + 123
    29  GraphicsServices                    0x03fea2c9 GSEventRunModal + 192
    30  GraphicsServices                    0x03fea106 GSEventRun + 104
    31  UIKit                               0x01781106 UIApplicationMain + 1526
    32  ParseStarterProject                 0x000fe524 main + 180
    33  libdyld.dylib                       0x030a2ac9 start + 1
)
libc++abi.dylib: terminating with uncaught exception of type NSException
(lldb)   

【问题讨论】:

    标签: swift xcode uitableview parse-platform segue


    【解决方案1】:

    试试这个:

    override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
        self.performSegueWithIdentifier("showDetails", sender: tableView)
    }
    
    override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
        var detailScene = segue.destinationViewController as! DetailViewController
    
        if let indexPath = self.tableView.indexPathForSelectedRow() {
            let row = Int(indexPath.row)
            detailScene.currentObject = (objects?[row] as! PFObject)
        }
    
    }
    

    【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多