Music_Player3/relax.offline.mp3.music/MP/MPPositive/Views/Search/MPPositive_SearchSuggestionsView.swift
2024-06-03 09:48:39 +08:00

61 lines
2.1 KiB
Swift

//
// 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)
}
}
}