首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >当搜索栏成为第一响应者时更改表格-快速

当搜索栏成为第一响应者时更改表格-快速
EN

Stack Overflow用户
提问于 2017-07-11 09:14:06
回答 1查看 367关注 0票数 1

嗨,当搜索栏变成first responder时,我正在尝试更改表格中的单元格。理想情况下,我希望我的add friends表最初显示好友请求,当搜索栏成为first responder时,它将更改单元格,以显示键入到搜索栏中的内容的搜索结果。下面是我当前的代码:

代码语言:javascript
复制
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
        }
    }
}
}
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2017-07-11 09:21:37

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

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

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/45023850

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档