ios - didSelectRowAtIndexPath 对搜索结果的处理不正确

标签 ios swift uitableview uisearchdisplaycontroller

在搜索结果之前,我的 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/

相关文章:

ios - 加速 - 降低对比度拉伸(stretch)的阈值

ios - 在我的 cocoapod 中包含一个 plist 文件

ios - 使用核心数据进行应用程序设置

ios - Xcode中获取所有系统图标的方法

SwiftMailgun 如何向 bodyHTML 添加变量

ios - 使用 swift 和 Parse 作为后端实现电子邮件或用户名登录?

iphone - 同时编辑和删除 UITableView 中的多行

ios - 过渡时的 fatal error

ios - UITableView后台查看平移手势

ios - 在 HeightForRowAt 中获取行高