我有一个listPicker:UIPickerView!
,其中填充了PFObject数组的clientName:String
属性。此pickerView位于EditViewController内部,用于编辑现有PFObject的详细信息(显示在父TableViewController中)。 listPicker的PFObjects在EditViewController中查询,与父TableViewController的查询分开。
在EditViewController的viewDidLoad内部我试图调用self.listPicker.selectRow(self.dealIndex, inComponent: 0, animated: true)
,以便pickerView中的选定行与父TableViewController的clientName
的现有值匹配。
我正在尝试在listPicker中找到现有clientName
的索引来设置dealIndex
的值。我认为我接近下面的代码,通过创建一个并行的dealArray:NSMutableArray并调用self.dealIndex = dealArray.indexOfObject(dealString)
,但由于某种原因这不起作用。
为了澄清if let
语句,我的Task类有一个指向Deal类的指针,该类有一个指向Client类的指针,该类包含属性“clientName”。这些clientName值填充了listPicker。
以下是代码段以及相关的周围代码。目前,当我运行它时,我收到错误*** Terminating app due to uncaught exception 'NSRangeException', reason: '*** -[NSMutableIndexSet addIndexesInRange:]: Range {9223372036854775807, 1} exceeds maximum index value of NSNotFound - 1'
我认为这是由于self.listPicker.selectRow(self.dealIndex, inComponent: 0, animated: true)
无法正常运行。
for object in dealObjects {
if let client = object["client"] as? PFObject {
if let clientName:NSString = client["clientName"] as? NSString {
dealArray.addObject(clientName)
}
}
}
-
if (self.object != nil) {
if let deal = self.object["deal"] as? PFObject {
if let client = deal["client"] as? PFObject {
if let clientName = client["clientName"] as? String {
self.deal_Btn.setTitle(clientName, forState: .Normal)
self.dealString = clientName as NSString
}
}
}
self.dealIndex = dealArray.indexOfObject(dealString)
self.listPicker.selectRow(self.dealIndex, inComponent: 0, animated: true)
}
上下文代码:
import UIKit
class EditViewController: UITableViewController, UIPickerViewDataSource, UIPickerViewDelegate {
var dealObjects:NSMutableArray!
var newDeal:PFObject!
var dealIndex:Int!
var dealString:NSString!
var dealArray:NSMutableArray = NSMutableArray()
@IBOutlet weak var deal_Btn: UIButton!
@IBOutlet weak var listPicker: UIPickerView!
//Client listPicker Query
func fetchAllDeals() {
var dealQuery:PFQuery = PFQuery(className: "Deal")
dealQuery.whereKey("username", equalTo: PFUser.currentUser()!)
dealQuery.includeKey("client")
dealQuery.orderByAscending("clientName")
self.dealObjects = NSMutableArray()
dealQuery.findObjectsInBackgroundWithBlock { (deals, error) -> Void in
if (deals == nil) {
println(deals)
} else if (error == nil) {
var temp:NSArray = deals! as NSArray
self.dealObjects = temp.mutableCopy() as! NSMutableArray
println(deals)
self.listPicker.reloadAllComponents()
println("Fetched clients from server")
} else {
println(error?.userInfo)
}
}
for object in dealObjects {
if let client = object["client"] as? PFObject {
if let clientName:NSString = client["clientName"] as? NSString {
dealArray.addObject(clientName)
}
}
}
}
//MARK: - Task object data
var object:PFObject!
override func viewDidLoad() {
super.viewDidLoad()
fetchAllDeals()
listPicker.delegate = self
listPicker.dataSource = self
//Dismiss keyboard for touches outside of text field
var gestureRecognizer:UIGestureRecognizer = UITapGestureRecognizer(target: self, action: "hideKeyboard")
self.tableView.addGestureRecognizer(gestureRecognizer)
if (self.object != nil) {
if let deal = self.object["deal"] as? PFObject {
if let client = deal["client"] as? PFObject {
if let clientName = client["clientName"] as? String {
self.deal_Btn.setTitle(clientName, forState: .Normal)
self.dealString = clientName as NSString
}
}
}
self.dealIndex = dealArray.indexOfObject(dealString)
self.listPicker.selectRow(dealIndex, inComponent: 0, animated: true)
} else {
self.object = PFObject(className: "Task")
}
}
//MARK: - Delegates and data sources
//MARK: Data sources
func numberOfComponentsInPickerView(pickerView: UIPickerView) -> Int {
return 1
}
func pickerView(pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
return dealObjects.count
}
func pickerView(pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String! {
let object:PFObject = dealObjects[row] as! PFObject
var rowClientName = String()
if let client = object["client"] as? PFObject {
if let clientName = client["clientName"] as? String {
rowClientName = clientName
}
}
return rowClientName
}
func pickerView(pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
let object:PFObject = dealObjects[row] as! PFObject
self.newDeal = object
if let client = object["client"] as? PFObject {
if let clientName:String = client["clientName"] as? String {
deal_Btn?.setTitle(clientName, forState: .Normal)
}
}
}
}