在搜索结果之前,我的 didSelectRowAtIndexPath
工作正常:
override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
performSegueWithIdentifier("fromContactsToChat", sender: OneRoster.userFromRosterAtIndexPath(indexPath: indexPath))
}
但是当我搜索某些内容并点击结果时,它会将我的联系人视为旧的 tableView 单元格索引路径。例如:
起初我的 tableView 看起来像:
Third User
First User
Second User
当我搜索First
时,它会显示我
First User
但是当我点击它时,它会将我的第一个用户
视为旧表中的第三个用户
,因此
OneRoster.userFromRosterAtIndexPath(indexPath: indexPath))
我获得了第三用户
凭据。我该如何更新并修复它?当我单击“搜索”显示中的单元格时,它将完全针对该搜索到的联系人打开。
如果有任何问题,请问我
更新
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> onlineUserCell {
let cell:onlineUserCell = self.tableView.dequeueReusableCellWithIdentifier("Cell", forIndexPath: indexPath) as! onlineUserCell
var us : User
if tableView == self.searchDisplayController!.searchResultsTableView {
us = filteredUsers[indexPath.row]
cell.username.text = us.name
cell.avatarImage.image = us.image
tableView.rowHeight = 60
cell.avatarImage.layer.cornerRadius = cell.avatarImage.frame.size.height / 2
cell.avatarImage.clipsToBounds = true
} else {
let user = OneRoster.userFromRosterAtIndexPath(indexPath: indexPath)
let photoData = OneChat.sharedInstance.xmppvCardAvatarModule?.photoDataForJID(user.jid)
cell.greenIndicator.alpha = 1
if user.isOnline() {
cell.greenIndicator.backgroundColor = UIColor.greenColor()
} else {
cell.greenIndicator.backgroundColor = UIColor.redColor()
// here here
}
cell.username.text = user.displayName
configurePhotoForCell(cell, user: user)
cell.avatarImage.layer.cornerRadius = cell.avatarImage.frame.size.height / 2
cell.avatarImage.clipsToBounds = true
}
return cell
}
更新2
struct User {
let name : String
let image: UIImage
}
更新3
public class func userFromRosterAtIndexPath(indexPath indexPath: NSIndexPath) -> XMPPUserCoreDataStorageObject {
return sharedInstance.fetchedResultsController()!.objectAtIndexPath(indexPath) as! XMPPUserCoreDataStorageObject
}
最佳答案
您的 didselectRowAtIndexPath 实现应该类似于
override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
if tableView == self.searchDisplayController!.searchResultsTableView {
performSegueWithIdentifier("fromContactsToChat", sender: filteredUsers[indexPath.row])
}
else {
performSegueWithIdentifier("fromContactsToChat", sender: OneRoster.userFromRosterAtIndexPath(indexPath: indexPath))
}
}
即使在搜索结果中,您也在使用原始数据源,这就是问题所在
关于ios - didSelectRowAtIndexPath 对搜索结果的处理不正确,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33802367/