// // MPPositive_SearchSuggestionsView.swift // MusicPlayer // // Created by Mr.Zhou on 2024/5/13. // import UIKit ///展示搜索建议框 class MPPositive_SearchSuggestionsView: UIView { //搜索建议tableView private lazy var tableView:UITableView = { let tableView = UITableView() tableView.backgroundColor = .clear tableView.separatorStyle = .none tableView.rowHeight = 75*width tableView.dataSource = self tableView.delegate = self tableView.register(MPPositive_SearchSuggestionItemTableViewCell.self, forCellReuseIdentifier: MPPositive_SearchSuggestionItemTableViewCellID) return tableView }() private let MPPositive_SearchSuggestionItemTableViewCellID = "MPPositive_SearchSuggestionItemTableViewCell" //对用户展示的搜索建议组 var suggestions:[NSAttributedString]!{ didSet{ //刷新内容 tableView.reloadData() } } var selectedTextBlock:((String) -> Void)? override init(frame: CGRect) { super.init(frame: frame) backgroundColor = .clear addSubview(tableView) tableView.snp.makeConstraints { make in make.left.right.bottom.top.equalToSuperview() } } required init?(coder: NSCoder) { super.init(coder: coder) } } //MARK: - tableView extension MPPositive_SearchSuggestionsView:UITableViewDataSource, UITableViewDelegate { func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { return suggestions != nil ? suggestions.count:0 } func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { let cell = tableView.dequeueReusableCell(withIdentifier: MPPositive_SearchSuggestionItemTableViewCellID, for: indexPath) as! MPPositive_SearchSuggestionItemTableViewCell cell.item = suggestions[indexPath.row] return cell } func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) { if selectedTextBlock != nil { let text = suggestions[indexPath.row].string selectedTextBlock!(text) } } }