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

标签 ios swift uisearchcontroller

在 IOS 应用程序(swift 1.2、Xcode 6.3)上工作时,我在 ViewController 中实现 UISearchController,当我尝试为表格 View 返回单元格时,数据源方法 (cellForRowAtIndexPath) 出现错误,因为我不知道从字典中获取 indexpath.row。错误是 Cannot subscript a value of type [String : AnyObject] with an index of type Int View Controller 的代码是:

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]
  }
}

最佳答案

字典没有索引,它们有键。您可能需要考虑一种解析字典数据并将其放入有序列表(如数组)的方法,这样您的数据就会出现在同一个地方。 (字典没有排序)。

关于ios - 无法使用 Int 类型的索引下标字典类型的值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31663722/

相关文章:

ios - 为什么 return 语句不会停止程序?

iphone - 每当我们需要单击一个单元格时如何显示表格 View 单元格

ios - 无法将 searchBar 设置为 firstResponder

iphone - 警告 : ld: warning: unexpected srelocation type 9 while building on device

ios - MPVolumeView 改变 Airplay 图标的大小

ios - 当我们从 iOS url 方案打开 gmail 应用程序时,邮件正文上有空格

swift - 按钮不响应 ScrollView 中的触摸

swift - 开发 macOS 的辅助应用程序,apiDisabled

uitableview - 带有 scopeBar 奇怪行为的 searchBar

ios - 如何在键盘上单击搜索按钮时进行搜索?