ios - 如何正确显示搜索结果?

标签 ios swift search uisearchdisplaycontroller

我有一个带有 [头像图像 - 名称] 的联系人列表 tableView。我想在这些用户中搜索。为此,我创建了一个结构 [User.swift]:

struct User {
    let name : String
    let image: UIImage
}

我通过以下方式搜索:

func filterContentForSearchText(searchText: String, scope: String = "All") {
    self.filteredUsers = self.users.filter({( user : User) -> Bool in
        let stringMatch = user.name.rangeOfString(searchText)
        return (stringMatch != nil)
    })
}

但它按预期仅按字符串部分(在名称中)进行搜索。现在,如何连接到它的联系人头像图像?

我将所有内容保存在数组 var users = [User]() 中:

self.users.append(User(name: user.displayName, image: UIImage(data: photoData!)!))

那么,我怎样才能在联系人姓名附近显示图片呢?

最佳答案

你应该能够得到你的用户

let userForRow:User = self.filteredUsers[indexPath.row]

然后访问图片

userForRow.image

您可以使用标准单元格来显示图像

cell.imageView.image = userForRow.image

在数据源的 cellForRowAtIndexPath

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
    let user = filteredUsers[indexPath.row]
    let cell = tableView.dequeueReusableCellWithIdentifier("cell", forIndexPath indexPath)

    cell.textLabel.text = user.name
    cell.imageView.image = user.image

    return cell
}

关于ios - 如何正确显示搜索结果?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33798401/

相关文章:

ios - "Any?"类的 Swift3 扩展?

bash - 搜索和追加

language-agnostic - 是否有一个用子树大小注释的二叉搜索树的实现

swift - 垂直 UISlider 约束

swift - iOS 图表中雷达图的问题设置数据

javascript - iOS 5.x 中 js 事件的 safari 浏览器中的声音通知(例如 ajax 响应)

ios - 为什么将 iOS 应用程序移植到 tvOS 不起作用?

search - 是否有用于索引文本搜索的最常见英语单词列表?

iOS 企业内部分发流程

ios - 关闭应用程序后NSTimer是否运行