Ios 当搜索栏成为第一响应者时更改表格-swift

Ios 当搜索栏成为第一响应者时更改表格-swift,ios,swift,tableview,searchbar,first-responder,Ios,Swift,Tableview,Searchbar,First Responder,嗨,当搜索栏成为第一响应者时,我正在尝试更改表格中的单元格。理想情况下,我希望我的AddFriends表最初显示好友请求,当搜索栏成为第一响应者时,它将更改单元格以显示搜索栏中键入内容的搜索结果。这是我目前的代码: class UserFriendRequestController: UIViewController, UITableViewDelegate, UITableViewDataSource, UISearchBarDelegate { let titleCell = "title

嗨,当搜索栏成为第一响应者时,我正在尝试更改表格中的单元格。理想情况下,我希望我的AddFriends表最初显示好友请求,当搜索栏成为第一响应者时,它将更改单元格以显示搜索栏中键入内容的搜索结果。这是我目前的代码:

class UserFriendRequestController: UIViewController, UITableViewDelegate, UITableViewDataSource, UISearchBarDelegate {

let titleCell = "titleCell"
let cellId = "cellId"
let contactSearch = "contactSearch"
var searchBar = UISearchBar()
var addFriendsTable: UITableView = UITableView()
let users = NSMutableArray()

override func viewDidLoad() {
    super.viewDidLoad()

    view.addSubview(addFriendsTable)

    let img = UIImage(named: "white-screen")
    self.navigationController?.navigationBar.shadowImage = img
    self.navigationController?.navigationBar.setBackgroundImage(img, for: UIBarMetrics.default)

    navigationItem.title = "Add Contact"
    self.navigationController?.navigationBar.titleTextAttributes = [ NSFontAttributeName: UIFont(name: "Nunito", size: 18.5)!, NSForegroundColorAttributeName: UIColor(r: 86, g: 214, b: 209)]

    addFriendsTable.frame = CGRect(x: 0, y: 0, width: UIScreen.main.bounds.width, height: UIScreen.main.bounds.height - (70))
    addFriendsTable.delegate = self
    addFriendsTable.dataSource = self
    searchBar.sizeToFit()
    searchBar.searchBarStyle = .minimal
    searchBar.placeholder = "Search Conctacts"
    searchBar.showsCancelButton = true
    searchBar.delegate = self
    addFriendsTable.tableHeaderView = searchBar
    addFriendsTable.contentOffset = CGPoint(x: 0.0, y: 0.0)

    UIBarButtonItem.appearance(whenContainedInInstancesOf: [UISearchBar.self]).setTitleTextAttributes([NSFontAttributeName : UIFont(name: "Nunito", size: 15)!,  NSForegroundColorAttributeName : UIColor(r: 78, g: 78, b: 78)
        ], for: .normal)

    var image = UIImage(named: "back_button_thick")
    image = image?.withRenderingMode(UIImageRenderingMode.alwaysOriginal)
    navigationItem.leftBarButtonItem = UIBarButtonItem(image: image, style: .plain, target: self, action: #selector(handleBack))

    addFriendsTable.register(UserFriendRequestsCell.self, forCellReuseIdentifier: cellId)
    addFriendsTable.register(FriendRequestsTitleCell.self, forCellReuseIdentifier: titleCell)
    addFriendsTable.register(AddFriendRequestCell.self, forCellReuseIdentifier: contactSearch)
    addFriendsTable.separatorColor = UIColor(r: 225, g: 225, b: 225)

    setup()
}

func setup() {
    for i in 1 ..< 20 {
        let user = User()
        users.add(user)
    }
}


func searchBarCancelButtonClicked(_ searchBar: UISearchBar) {
    searchBar.text = nil
    searchBar.resignFirstResponder()
}

func handleBack() {
    dismiss(animated: true, completion: nil)
}


func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    return 10
}

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {

    if self.searchBar.isFirstResponder {
        let cell = tableView.dequeueReusableCell(withIdentifier: contactSearch, for: indexPath) as! AddFriendRequestCell
        cell.selectionStyle = UITableViewCellSelectionStyle.none
        cell.textLabel?.text = "Instagram User Name"
        cell.textLabel?.font = UIFont(name: "Nunito", size: 16)
        cell.textLabel?.textColor = UIColor(r: 51, g: 51, b: 51)
        cell.detailTextLabel?.text = "Instagram Handle"
        cell.detailTextLabel?.font = UIFont(name: "Nunito", size: 13)
        cell.detailTextLabel?.textColor = UIColor.darkGray
        cell.profileImageView.image = UIImage(named: "profilepic")
        cell.separatorInset.left = CGFloat(55)
        cell.separatorInset.right = CGFloat(5)
        let user = users[indexPath.row] as! User
        cell.configure(user: user)
        return cell
    } else {
        if indexPath.row == 0 {

            let cell = tableView.dequeueReusableCell(withIdentifier: titleCell, for: indexPath) as! FriendRequestsTitleCell
            cell.selectionStyle = UITableViewCellSelectionStyle.none
            return cell

        } else {
            let cell = tableView.dequeueReusableCell(withIdentifier: cellId, for: indexPath) as! UserFriendRequestsCell
            cell.selectionStyle = UITableViewCellSelectionStyle.none
            cell.textLabel?.text = "Instagram User Name"
            cell.textLabel?.font = UIFont(name: "Nunito", size: 16)
            cell.textLabel?.textColor = UIColor(r: 51, g: 51, b: 51)
            cell.detailTextLabel?.text = "Instagram Handle"
            cell.detailTextLabel?.font = UIFont(name: "Nunito", size: 13)
            cell.detailTextLabel?.textColor = UIColor.darkGray
            cell.profileImageView.image = UIImage(named: "profilepic")
            cell.separatorInset.left = CGFloat(55)
            cell.separatorInset.right = CGFloat(5)

            return cell
        }
    }
}

func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat{

    if self.searchBar.isFirstResponder {
        return 55
    }

    else {
        if indexPath.row == 0 {
            return 30
        } else {
            return 55
        }
    }
}
}
class UserFriendRequestController:UIViewController、UITableViewDelegate、UITableViewDataSource、UISearchBarDelegate{
让titleCell=“titleCell”
let cellId=“cellId”
让contactSearch=“contactSearch”
var searchBar=UISearchBar()
var addFriendsTable:UITableView=UITableView()
让用户=NSMutableArray()
重写func viewDidLoad(){
super.viewDidLoad()
view.addSubview(addFriendsTable)
设img=UIImage(名为“白屏”)
self.navigationController?.navigationBar.shadowImage=img
self.navigationController?.navigationBar.setBackgroundImage(img,for:UIBarMetrics.default)
navigationItem.title=“添加联系人”
self.navigationController?.navigationBar.titleTextAttributes=[NSFontAttributeName:UIFont(名称:“Nunito”,大小:18.5)!,NSForegroundColorAttributeName:UIColor(r:86,g:214,b:209)]
addFriendsTable.frame=CGRect(x:0,y:0,宽度:UIScreen.main.bounds.width,高度:UIScreen.main.bounds.height-(70))
addFriendsTable.delegate=self
addFriendsTable.dataSource=self
searchBar.sizeToFit()
searchBar.searchBarStyle=.minimal
searchBar.placeholder=“搜索联系人”
searchBar.showsCancelButton=true
searchBar.delegate=self
addFriendsTable.tableHeaderView=搜索栏
addFriendsTable.contentOffset=CGPoint(x:0.0,y:0.0)
UIBarButtonim.appearance(当包含[UISearchBar.self]实例时)。setTitleTextAttributes([NSFontAttributeName:UIFont(名称:“Nunito”,大小:15)!,NSForegroundColorAttributeName:UIColor(r:78,g:78,b:78)
],适用于:。正常)
var image=UIImage(名为:“后退按钮”
image=image?.withRenderingMode(UIImageRenderingMode.alwaysOriginal)
navigationItem.leftBarButtonItem=UIBarButtonItem(图像:图像,样式:。普通,目标:自我,操作:#选择器(把手))
addFriendsTable.register(UserFriendRequestsCell.self,forCellReuseIdentifier:cellId)
addFriendsTable.register(FriendRequestsTitleCell.self,forCellReuseIdentifier:titleCell)
addFriendsTable.register(AddFriendRequestCell.self,forCellReuseIdentifier:contactSearch)
addFriendsTable.separatorColor=UIColor(r:225,g:225,b:225)
设置()
}
函数设置(){
对于1..<20中的i{
让用户=用户()
用户。添加(用户)
}
}
func searchBarCancelButtonClicked(searchBar:UISearchBar){
searchBar.text=nil
searchBar.resignFirstResponder()辞职
}
func把手{
驳回(动画:真,完成:无)
}
func tableView(tableView:UITableView,numberofrowsinssection:Int)->Int{
返回10
}
func tableView(tableView:UITableView,cellForRowAt indexath:indexPath)->UITableViewCell{
如果self.searchBar.isFirstResponder{
让cell=tableView.dequeueReusableCell(标识符:contactSearch,for:indexPath)作为!AddFriendRequestCell
cell.selectionStyle=UITableViewCellSelectionStyle.none
cell.textlab?.text=“Instagram用户名”
cell.textLabel?.font=UIFont(名称:“Nunito”,大小:16)
cell.textLabel?.textColor=UIColor(r:51,g:51,b:51)
cell.detailTextLabel?.text=“Instagram句柄”
cell.detailTextLabel?.font=UIFont(名称:“Nunito”,大小:13)
cell.detailTextLabel?.textColor=UIColor.darkGray
cell.profileImageView.image=UIImage(名为:“profilepic”)
cell.separatorInset.left=CGFloat(55)
cell.separatorInset.right=CGFloat(5)
让user=users[indexPath.row]作为!user
cell.configure(用户:用户)
返回单元
}否则{
如果indexath.row==0{
让cell=tableView.dequeueReusableCell(标识符为:titleCell,for:indexPath)作为!FriendRequestsTitleCell
cell.selectionStyle=UITableViewCellSelectionStyle.none
返回单元
}否则{
让cell=tableView.dequeueReusableCell(标识符:cellId,for:indexPath)作为!UserFriendRequestsCell
cell.selectionStyle=UITableViewCellSelectionStyle.none
cell.textlab?.text=“Instagram用户名”
cell.textLabel?.font=UIFont(名称:“Nunito”,大小:16)
cell.textLabel?.textColor=UIColor(r:51,g:51,b:51)
cell.detailTextLabel?.text=“Instagram句柄”
cell.detailTextLabel?.font=UIFont(名称:“Nunito”,大小:13)
cell.detailTextLabel?.textColor=UIColor.darkGray
cell.profileImageView.image=UIImage(名为:“profilepic”)
cell.separatorInset.left=CGFloat(55)
cell.separatorInset.right=CGFloat(5)
返回单元
}
}
}
func tableView(tableView:UITableView,heightForRowAt indexath:indexPath)->CGFloat{
如果self.searchBar.isFirstResponder{
返回55
}
否则{
如果indexath.row==0{
返回30
}否则{
返回55
}
}
}
}

为此,我建议查看中的其他方法,特别是
searchbartextdidebenediting()
searchbartextdidediting()
。在每个委托方法中,您都可以调用
[self.tableView reloadData]
来更新所有单元格

另外,实现
FriendRequestStiteCell
的另一种方法是在表视图中添加自定义标题视图,因为您使用的单元格只出现在表视图的顶部