在下面的代码我实现了一个由PFObjects(parse.com)填充的UITablView控制器一切正常但现在我想在我的uitableview中添加搜索功能,因为我使用了UISearchController方法并且能够找到它搜索,但问题是,在点击搜索结果单元格后,它无法导航或打开详细视图控制器,它给了我一个空白的详细信息视图控制器,我知道它,因为这个功能:
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
let upcoming: AddNoteTableViewController = segue.destinationViewController as! AddNoteTableViewController
if (segue.identifier == "openStory"){
let indexPath = self.tableView.indexPathForSelectedRow!
if self.resultSearchController.active
{
// how can i implement the same for filteredNotes as below (on else ) ???
let object: PFObject = self.filteredNotes.objectAtIndex(indexPath.row) as! PFObject
upcoming.object = object
}
else{
let object: PFObject = self.noteObjects.objectAtIndex(indexPath.row) as! PFObject
upcoming.object = object
self.tableView.deselectRowAtIndexPath(indexPath, animated: true)
}}}
当我试图使这个功能可行时,在声明'对象'时会出现一些错误 错误:
Value of type '[String]' has no member 'objectAtIndex'
我怎样才能使其正确无论如何有人知道,请告诉我,谢谢
完整代码:
import UIKit
class MasterTableViewController: UITableViewController, PFLogInViewControllerDelegate, PFSignUpViewControllerDelegate, UISearchResultsUpdating {
//var searchNotes: PFObject = PFObject()
var searchNotes = PFObject(className: "myClass")
var filteredNotes = [String]()
var resultSearchController = UISearchController()
var searchActive: Bool = false
// creating array for holding ojects
var noteObjects: NSMutableArray! = NSMutableArray()
var v = 0
override func viewDidLoad() {
super.viewDidLoad()
self.resultSearchController = UISearchController(searchResultsController: nil)
self.resultSearchController.searchResultsUpdater = self
self.resultSearchController.dimsBackgroundDuringPresentation = false
self.resultSearchController.searchBar.sizeToFit()
self.tableView.tableHeaderView = self.resultSearchController.searchBar
self.tableView.reloadData()
self.definesPresentationContext = true //self takes priority over the searchController presentation
self.resultSearchController.hidesNavigationBarDuringPresentation = false
}
override func viewDidAppear(animated: Bool) {
super.viewDidAppear(animated)
if v == 0 {
self.fetchAllObjectsFromLocalDataStore()
//self.fetchAllObjects()
}
}
// fetching data from local datastore and from parse
func fetchAllObjectsFromLocalDataStore(){
let query: PFQuery = PFQuery(className: "myClass")
query.orderByDescending("createdAt")
query.fromLocalDatastore()
query.findObjectsInBackgroundWithBlock { ( objects, error) -> Void in
if (error == nil) {
let temp: NSArray = objects as NSArray!
self.noteObjects = temp.mutableCopy() as! NSMutableArray
self.tableView.reloadData()
}else {
print(error!.userInfo)
}
}
}
func fetchAllObjects(){
let query: PFQuery = PFQuery(className: "myClass")
query.orderByDescending("createdAt")
query.findObjectsInBackgroundWithBlock { (objects, error) -> Void in
if (error == nil) {
PFObject.pinAllInBackground(objects, block: nil )
self.fetchAllObjectsFromLocalDataStore()
// self.tableView.reloadData()
} else {
print(error?.userInfo)
}
}
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
// MARK: - Table view data source
override func numberOfSectionsInTableView(tableView: UITableView) -> Int {
// #warning Incomplete implementation, return the number of sections
return 1
}
override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
// #warning Incomplete implementation, return the number of rows
if self.resultSearchController.active
{
return self.filteredNotes.count
}else{
return self.noteObjects.count
}}
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = self.tableView.dequeueReusableCellWithIdentifier("cell", forIndexPath: indexPath) as! MasterTableViewCell
if self.resultSearchController.active
{
cell.textLabel?.text = self.filteredNotes[indexPath.row] as? String
searchNotes = self.noteObjects.objectAtIndex(indexPath.row) as! PFObject
cell.MasterTitleLabel?.text = searchNotes["Title"] as? String
cell.MasterTextLabel.text = searchNotes["Fstory"] as? String
cell.MasterTimeLabel.text = searchNotes["Time"] as? String
cell.MasterLocationLabel.text = searchNotes["Location"] as? String
return cell
} else {
let object : PFObject = self.noteObjects.objectAtIndex(indexPath.row) as! PFObject
cell.MasterTitleLabel?.text = object["Title"] as? String
cell.MasterTextLabel.text = object["Fstory"] as? String
cell.MasterTimeLabel.text = object["Time"] as? String
cell.MasterLocationLabel.text = object["Location"] as? String
return cell
}}
override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
var object :AnyObject?
if self.resultSearchController.active{
object = filteredNotes[indexPath.row]
print(filteredNotes[indexPath.row])
self.performSegueWithIdentifier("openStory", sender: self)
} else {
object = self.noteObjects[indexPath.row]
print(noteObjects[indexPath.row])
self.performSegueWithIdentifier("openStory", sender: self)
}}
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
let upcoming: AddNoteTableViewController = segue.destinationViewController as! AddNoteTableViewController
if (segue.identifier == "openStory"){
let indexPath = self.tableView.indexPathForSelectedRow!
if self.resultSearchController.active
{
// how can i implement the same for filteredNotes as below (on else ) ???
let object: PFObject = self.filteredNotes.objectAtIndex(indexPath.row) as! PFObject
upcoming.object = object
}
else{
let object: PFObject = self.noteObjects.objectAtIndex(indexPath.row) as! PFObject
upcoming.object = object
self.tableView.deselectRowAtIndexPath(indexPath, animated: true)
}}}
@IBAction func btnReload(sender: AnyObject) {
fetchAllObjects()
}
override func tableView(tableView: UITableView, canEditRowAtIndexPath indexPath: NSIndexPath) -> Bool {
return true
}
override func tableView(tableView: UITableView, commitEditingStyle editingStyle: UITableViewCellEditingStyle, forRowAtIndexPath indexPath: NSIndexPath) {
if (editingStyle == UITableViewCellEditingStyle.Delete ){
if self.resultSearchController.active
{
}else{
let object : PFObject = self.noteObjects.objectAtIndex(indexPath.row) as! PFObject
// the below for deleting the selected cell's object from server's database
// object.deleteInBackground()
//the below for deleting the selected cell's object from localstorage
object.unpinInBackground()
self.noteObjects.removeObjectAtIndex(indexPath.row)
}
tableView.deleteRowsAtIndexPaths([indexPath], withRowAnimation: UITableViewRowAnimation.Automatic)
}
}
func updateSearchResultsForSearchController(searchController: UISearchController) {
// self.filteredNotes.removeAll(keepCapacity: false)
self.filteredNotes.removeAll(keepCapacity: false)
// let searchPredicate = NSPredicate(format: "SELF CONTAINS[c] %@", searchController.searchBar.text!)
//
// let array = (self.noteObjects as NSArray).filteredArrayUsingPredicate(searchPredicate)
//
// self.filteredNotes = array as! [String]
let searchPredicate = NSPredicate(format: "SELF CONTAINS[c] %@", searchController.searchBar.text!)
var array = (self.noteObjects as! [PFObject]).map { (obj) -> String in
obj["Title"] as! String
}
array = (array as NSArray).filteredArrayUsingPredicate(searchPredicate) as! [String]
self.filteredNotes = array as! [String]
self.tableView.reloadData()
}
}
答案 0 :(得分:2)
要在swift中访问数组元素,您应该使用以下语法:
array[n]
其中array是您的数组,n是您要访问的元素的索引。
答案 1 :(得分:0)
您的错误是objectAtIndex方法在数组中不存在。
所以当你宣布你的FilteredNotes时,你会这样做 var filteredNotes = String
如果它是NSArray ,objectAtIndex会起作用
相反,请尝试
让对象:PFObject = self.filteredNotes [indexPath.row]为! PFObject