无法使用Int类型的索引下标类型字典的值

时间:2015-07-27 21:35:07

标签: ios swift uisearchcontroller

使用IOS应用程序(swift 1.2,Xcode 6.3),我在ViewController中实现了UISearchController,当我尝试返回一个单元格时,我在数据源方法(cellForRowAtIndexPath)中收到错误对于表格视图,因为我不知道从字典中获取indexpath.row。错误是Cannot subscript a value of type [String : AnyObject] with an index of type Int,视图控制器的代码是:

import Foundation
import UIKit

class UsersViewController: UIViewController, UITableViewDataSource, UITableViewDelegate {

  @IBOutlet var tableview:UITableView!

  let apiClient = ApiClient()

  var users: [User]!

  var searchArray:[AnyObject] = [AnyObject](){
    didSet  {self.tableview.reloadData()}
  }

  var usersSearchController = UISearchController()

  override func viewDidLoad() {
    super.viewDidLoad()

    self.usersSearchController = ({
      // Two setups provided below:

      // Setup One: This setup present the results in the current view.
      let controller = UISearchController(searchResultsController: nil)
      controller.searchResultsUpdater = self
      controller.hidesNavigationBarDuringPresentation = false
      controller.dimsBackgroundDuringPresentation = false
      controller.searchBar.searchBarStyle = .Minimal
      controller.searchBar.sizeToFit()
      self.tableview.tableHeaderView = controller.searchBar

      return controller
    })()
  }

  override func viewWillAppear(animated: Bool) {
    super.viewWillAppear(animated)
    println("UsersController viewWillAppear")

    apiClient.usersService.getList() { users, error in
      if users != nil {
        self.users = users
        self.tableview?.reloadData()
      } else {
        println("error: \(error)")
      }
    }
  }

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

  func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    if ( self.usersSearchController.active){

      return self.searchArray.count ?? 0

    } else {

      return self.users?.count ?? 0

    }
  }

  func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {

    var cell = tableView.dequeueReusableCellWithIdentifier("userObjectCell") as! UserTableViewCell

    if (self.usersSearchController.active){

      cell.userObject = self.searchArray[indexPath.row] as? User//HERE IS THE ERROR

      return cell

    } else {

      cell.userObject = self.users?[indexPath.row]

      return cell
    }
  }

}

extension UsersViewController: UITableViewDelegate
{
  func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath)
  {
    tableView.deselectRowAtIndexPath(indexPath, animated: true)
  }
}

extension UsersViewController: UISearchResultsUpdating
{
  func updateSearchResultsForSearchController(searchController: UISearchController)
  {
//    self.searchArray.removeAll(keepCapacity: false)
//    
//    let searchPredicate = NSPredicate(format: "SELF CONTAINS[c] %@", searchController.searchBar.text)
//    let array = (self.users as NSArray).filteredArrayUsingPredicate(searchPredicate)
//    self.searchArray = array as! [String: AnyObject]
  }
}

1 个答案:

答案 0 :(得分:1)

字典没有索引,它们有密钥。您可能需要考虑一种解析字典数据并将其放入有序列表(如数组)的方法,以便您的数据显示在同一位置。 (不订购字典)。