97 lines
2.6 KiB
Swift
97 lines
2.6 KiB
Swift
//
|
|
// SPSearchAssociativeView.swift
|
|
// Thimra
|
|
//
|
|
// Created by 曾觉新 on 2025/4/18.
|
|
//
|
|
|
|
import UIKit
|
|
|
|
class SPSearchAssociativeView: UIView {
|
|
|
|
|
|
private(set) lazy var searchText: String = ""
|
|
|
|
private lazy var dataArr: [SPShortModel] = []
|
|
|
|
private lazy var tableView: SPTableView = {
|
|
let tableView = SPTableView(frame: .zero, style: .plain)
|
|
tableView.delegate = self
|
|
tableView.dataSource = self
|
|
tableView.rowHeight = 50
|
|
tableView.contentInset = UIEdgeInsets(top: 0, left: 0, bottom: kSPTabbarSafeBottomMargin, right: 0)
|
|
SPSearchAssociativeCell.registerCell(tableView: tableView)
|
|
return tableView
|
|
}()
|
|
|
|
override init(frame: CGRect) {
|
|
super.init(frame: frame)
|
|
|
|
_setupUI()
|
|
}
|
|
|
|
required init?(coder: NSCoder) {
|
|
fatalError("init(coder:) has not been implemented")
|
|
}
|
|
|
|
func search(text: String) {
|
|
self.searchText = text
|
|
|
|
self.requestSearch(text: text)
|
|
|
|
}
|
|
}
|
|
|
|
extension SPSearchAssociativeView {
|
|
|
|
private func _setupUI() {
|
|
addSubview(tableView)
|
|
|
|
tableView.snp.makeConstraints { make in
|
|
make.edges.equalToSuperview()
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
//MARK: -------------- UITableViewDelegate & UITableViewDataSource --------------
|
|
extension SPSearchAssociativeView: UITableViewDelegate, UITableViewDataSource {
|
|
|
|
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
|
|
let cell = SPSearchAssociativeCell.dequeueReusableCell(tableView: tableView, indexPath: indexPath)
|
|
cell.searchText = self.searchText
|
|
cell.model = dataArr[indexPath.row]
|
|
return cell
|
|
}
|
|
|
|
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
|
|
return dataArr.count
|
|
}
|
|
|
|
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
|
|
let model = self.dataArr[indexPath.row]
|
|
|
|
let vc = SPPlayerDetailViewController()
|
|
vc.shortPlayId = model.short_play_id
|
|
self.viewController?.navigationController?.pushViewController(vc, animated: true)
|
|
}
|
|
}
|
|
|
|
extension SPSearchAssociativeView {
|
|
|
|
private func requestSearch(text: String) {
|
|
|
|
SPHomeAPI.requestSearch(text: text) { [weak self] list in
|
|
guard let self = self else { return }
|
|
if self.searchText != text { return; }
|
|
|
|
if let list = list {
|
|
self.dataArr = list
|
|
self.tableView.reloadData()
|
|
}
|
|
}
|
|
|
|
}
|
|
}
|