0

このチュートリアルを使用して UISearchBar アプリを作成しました。すべてが正常に機能し、セルが正しく構成され、ユーザー名で検索できます。

今、各セルに (チェックマーク ✔︎) を追加しようとしています。これにより、リスト内の複数のユーザーを (選択 ✔︎) できるようになります。

機能は正常に動作しますが、リストを検索して (✔︎ を選択) ユーザーを選択し、メインのテーブルビューに戻ると、ユーザーは選択されたままになりません。

UISearchBar を使用する前後に複数のユーザー (チェックマーク ✔︎) を保持し、そのチェックマークを維持するにはどうすればよいですか?

class InviteViewController: UITableViewController, UITableViewDataSource, UITableViewDelegate, UISearchBarDelegate, UISearchDisplayDelegate {

  var allFriends = [Friend]()
  var filteredFriends = [Friend]()

  override func viewDidLoad() {
      super.viewDidLoad()

      ###Call to get all Friends
      getFriends()

  }

  override func numberOfSectionsInTableView(tableView: UITableView) -> Int {
      return 1
  }

  override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
      if tableView == self.searchDisplayController!.searchResultsTableView {
          return self.filteredFriends.count
      } else {
          return self.allFriends.count
      }
  }

  var selectedFriendIndex:Int? = nil

  override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
      let cell = self.tableView.dequeueReusableCellWithIdentifier("Cell") as! UITableViewCell

      var friend : Friend

      if tableView == self.searchDisplayController!.searchResultsTableView {
          friend = filteredFriends[indexPath.row]
      } else {
          friend = allFriends[indexPath.row]
      }

      ###Configure the cell
      cell.textLabel!.text = friend.username
      cell.accessoryType = UITableViewCellAccessoryType.DisclosureIndicator

      if (indexPath.row == selectedFriendIndex) {
          cell.accessoryType = UITableViewCellAccessoryType.Checkmark
      } else {
          cell.accessoryType = UITableViewCellAccessoryType.None
      }

      return cell
  }

  override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
      tableView.deselectRowAtIndexPath(indexPath, animated: true)

      selectedFriendIndex = indexPath.row
      let cell = tableView.cellForRowAtIndexPath(indexPath)

      if let index = selectedFriendIndex {
          if (cell?.accessoryType == .Checkmark) {
              cell!.accessoryType = .None            
          } else {
              cell!.accessoryType = .Checkmark
          }
      }
  }

  func filterContentForSearchText(searchText: String, scope: String = "All") {
      self.filteredFriends = self.allFriends.filter({( friend : Friend) -> Bool in
        var usernameMatch = (scope == "All") || (friend.username == scope)
        var stringMatch = friend.username.lowercaseString.rangeOfString(searchText.lowercaseString)
        return usernameMatch && (stringMatch != nil)
      })
  }

  func searchDisplayController(controller: UISearchDisplayController, shouldReloadTableForSearchString searchString: String!) -> Bool {
      self.filterContentForSearchText(searchString)
      return true
  }

  func searchDisplayController(controller: UISearchDisplayController, shouldReloadTableForSearchScope searchOption: Int) -> Bool {
      self.filterContentForSearchText(self.searchDisplayController!.searchBar.text)
      return true
  }

  func searchDisplayController(controller: UISearchDisplayController, willHideSearchResultsTableView tableView: UITableView) {
      self.tableView.reloadData()
  }

}
4

2 に答える 2