2015-07-20 23 views
0

試圖從我的UITableViewController到具有這些PFObjects的詳細視圖控制器...在此先感謝!準備segue ..從UITableView到DetailView與解析對象

錯誤我似乎無法調和...

「不能與類型‘字符串’的索引下標類型‘字符串’的值」我要被查詢的對象呈現細節視圖控制器上...

這裏是我的查詢和我的賽格瑞準備......我似乎無法訪問該對象的SEGUE準備...

var customerName = [String]() 
    var customerAddress = [String]() 

    var query = Pfuser.query 
    query.whereKey("userId",equalTo:adminFollowingUser) 
    query.findObjectsInBackgroundWithBlock({ (adminObjects, error) -> Void in 

      if let objects = adminObjects { 

      for object in objects { 

    self.customerName.append(object["customerName"] as! String) 
    self.customerAddress.append(object["customerStreetAddress"] as! String) 

//下面是準備賽格......

 override func prepareForSegue(segue: UIStoryboardSegue, sender: 
     AnyObject?) 
     { 
     if (segue.identifier == "thesePools") 
     { 

     let employeeDetailVC: EmployeeDetailViewController = segue.destinationViewController 
    as! EmployeeDetailViewController 
    // indexPath is set to the path that was tapped 


let indexPath = self.tableView.indexPathForSelectedRow 

    let customerNameLabel = self.customerName[indexPath!.row] 
    let customerAddressLabel = self.customerAddress[indexPath!.row] 
employeeDetailVC.customerString = customerNameLabel 
    employeeDetailVC.addressString = customerAddressLabel 

這裏是我的詳細視圖控制器接收字符串。

 //DetailViewController 

    var customerString = String() 
    var addressString = String() 

    override func viewDidLoad() { 
super.viewDidLoad() 

self.customerLabel.text = customerString 
self.addressLabel.text = addressString 

回答

1

VAR currentObject =字符串()是一個字符串,將其設置爲在prepareForSegue.This字符串應該做的伎倆: self.customerTextField.text = curentObject 並刪除所有其他的東西。

+0

我得到了所有的錯誤,但當我點擊單元格它仍然不會繼續..我三重檢查了segue名稱,刪除和redid它三倍檢查目標視圖控制器的類名稱...不知道還有什麼做..我會說這是答案...但它仍然不工作:( –

+0

你有沒有在故事板中的segue?你是怎麼做到的?另外,如果你使用「didSelectRowAtIndexPath」,確保它不是「 didDeSelectRowAtIndexPath「,這是一個經常犯的錯誤,你也必須在表視圖上」選擇單擊「,並將其繪製到視圖控制器,以用於數據源和委託,並且必須實現tableviewcontroller委託。 /中。com/@ ronm333/a-simple-table-view-example-in-swift-cbf9a405f975 – user1700737

+0

這個文章太棒了!用那篇文章想出來了!非常感謝網址:) –

1

試一下

let nav = segue.destinationViewController as! CustomerDetailViewController 
    var indexPath :NSIndexPath = self.tableview.indexPathForSelectedRow()! 
     var object = self.CustomerName[indexPath.row] as! String 
      nav.currentobject = object 
+0

獲得與您的代碼相同的錯誤...「沒有成員命名對象」...出於某種原因,我無法訪問我的查詢中的對象....我可以嵌套準備在我的查詢segue,所以它知道它獲得什麼對象? –

+0

對象是你已經保存從解析中查詢的東西的數組,你知道這是正確的。在你的情況下,它應該是** customerName **或** customAddress ** – Lamar

+0

我更新了代碼,它應該是您正在使用的數組的名稱 – Lamar

0

我會推薦使用PFQueryTableViewController。 這是一個由Parse提供的UI對象,可以更快地從類50x中加載數據。

下面是如何創建它的一個例子:

import UIKit 

class YourTableViewController: PFQueryTableViewController { 

// Initialise the PFQueryTable tableview 
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 = "yourClass" 

    self.textKey = "yourObject" 
    self.pullToRefreshEnabled = true 
    self.paginationEnabled = false 
} 

// Define the query that will provide the data for the table view 
override func queryForTable() -> PFQuery! { 
    var query = PFQuery(className: "yourClass") 
    query.orderByAscending("yourObject") 


    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 CustomTableViewCell! 
    if cell == nil { 
     cell = CustomTableViewCell(style: UITableViewCellStyle.Default, reuseIdentifier: "Cell") 
    } 

    // Extract values from the PFObject to display in the table cell 

    cell.info.text = object["info"] as String 


    // Date for cell subtitle 
    var dateFormatter = NSDateFormatter() 
    dateFormatter.dateFormat = "yyyy-MM-dd" 
    let dateForText = object["date"] as NSDate 
    cell.date.text = dateFormatter.stringFromDate(dateForText) 

    return cell 
} 

// 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]. 
    var detailScene = segue.destinationViewController as YourDetailViewController 

    // Pass the selected object to the destination view controller. 
    if let indexPath = self.tableView.indexPathForSelectedRow() { 
     let row = Int(indexPath.row) 
     detailScene.currentObject = objects[row] as? PFObject 
    } 
} 

在年底一定要還創建了一個自定義單元格類。