didSelectRowAtIndexPath对搜索结果的工作不正确

时间:2015-11-19 11:15:29

标签: ios swift uitableview uisearchdisplaycontroller

在搜索结果出来之前,我的didSelectRowAtIndexPath效果很好:

override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
    performSegueWithIdentifier("fromContactsToChat", sender: OneRoster.userFromRosterAtIndexPath(indexPath: indexPath))
}

但是当我搜索某些内容并点击结果时,它会将我的联系人视为旧的tableView单元格indexPath。例如:

起初我的tableView看起来像:

Third User
First User
Second User

当我搜索First时,它会显示我

First User

但是当我点击它时,它会从我的旧表中看到我的First UserThird User,所以

OneRoster.userFromRosterAtIndexPath(indexPath: indexPath))

我获得了Third User个凭据。我该如何更新并修复?当我从我的搜索显示中单击该单元格时,它将完全打开此搜索的联系人。

如果您有任何疑问,请询问我

更新

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
}

1 个答案:

答案 0 :(得分:2)

你的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))
    }
}

即使在搜索结果中,您正在使用原始数据源,这就是问题