我有一个带有 [头像图像 - 名称] 的联系人列表 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/