2017-05-03 7 views
0

막혔습니다. 검색 할 때FIRBase 데이터베이스 하위 키용 UISearchBar 문자열

는 [문자열] "FIRSTNAME는"올바른 값을 반환

작업을 searchbars를 처음. "firstname"이 "G"로 시작하는 3 명이 있으면 (예를 들어) 테이블에 3 개의 셀이 다시로드됩니다.

문제점 :

"FIRSTNAME"users.append (사용자)를위한 적절한 셀 값 테이블 리로드가 전무 복귀하고 틀린 이름이있는 TableView에로드되지만.

지원 :

내 코드 일부 조력을받을 수 있습니다하시기 바랍니다. 검색이 완료된 후 어떻게 테이블 뷰에 정확한 이름을로드 할 수 있습니까?

func searchBar(_ searchBar: UISearchBar, textDidChange textSearched: String)->Void { 

    FIRDatabase.database().reference().child("users").queryOrdered(byChild: "firstname").queryStarting(atValue: textSearched).queryEnding(atValue: textSearched+"\u{f8ff}").observe(.value, with: { snapshot in 
       var users = [User]() 
       let user = User() 
        print(user) 
       for _ in snapshot.children.allObjects as! [FIRDataSnapshot] { 
        if let dictionary = snapshot.value as? [String: AnyObject]{ 
         user.lastname = dictionary["firstname"] as? String 
         users.append(user) 
        } 
       } 
     self.users = users 
     let search = searchCell() 
     search.firstName.text = user.firstname 
     self.attempReloadOfTable() 
    }, withCancel: nil) 
} 


override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { 
    let cell = tableView.dequeueReusableCell(withIdentifier: cellId, for: indexPath) as! searchCell 
    var user = User() 
    user = users[indexPath.row] 


    if let id = user.id{ 
     let ref = FIRDatabase.database().reference().child("users").child(id) 
     ref.observe(.value, with: { (snapshot) in 
      cell.lastName.text = user.lastname 
      cell.firstName.text = user.firstname 
     }) 
    } 
    return cell 
} 

답변

0

귀하의 문제가이 블록의 사용자 데이터와 결합되기 전에 셀 반환 당신을 감사합니다

여기 내 코드입니다. return cell이 실행 된 후 FIRBase 결과 쿼리 블록의 코드가 수행되기 때문입니다.

나는 다음과 같은 코드를 편집 :
func searchBar(_ searchBar: UISearchBar, textDidChange textSearched: String)->Void { 

    FIRDatabase.database().reference().child("users").queryOrdered(byChild: "firstname").queryStarting(atValue: textSearched).queryEnding(atValue: textSearched+"\u{f8ff}").observe(.value, with: { snapshot in 
     var users = [User]() 
     for _ in snapshot.children.allObjects as! [FIRDataSnapshot] { 
      if let dictionary = snapshot.value as? [String: AnyObject] { 
       let user = User() 
       user.lastname = dictionary["firstname"] as? String 
       print(user) 
       users.append(user) 
      } 
     } 
     self.users = users 
     let search = searchCell() 
     search.firstName.text = user.firstname 
     self.attempReloadOfTable() 
    }, withCancel: nil) 
} 


override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { 
    let cell = tableView.dequeueReusableCell(withIdentifier: cellId, for: indexPath) as! searchCell 
    let user = users[indexPath.row] 

    cell.lastName.text = user.lastname 
    cell.firstName.text = user.firstname 

    return cell 
} 

이 당신을 위해 작동 바랍니다.

+0

이제 오류를 수정했지만 검색 창에서 "firstname"값을 가져 오지 않았습니다. 0을 말하고 0 셀을 반환합니다. ** let dictionary = child.value as? [String : AnyObject] {let user = User() ** <-이 줄은 제가 아는 범인입니다. (type nsfastenumerationiterator.element any ...) – Ketone

+0

자, 코드를'textDidChange' 함수에 보관해야합니다.'cellForRowAt' 함수 만 변경하면됩니다. – Lionking

+0

나는 내 대답을 편집했다. – Lionking