Ios 单元格不使用UITableVIewDiffableDataSource加载
我正在尝试使用新的DiffableDataSource api实现表视图,但单元格根本不加载:Ios 单元格不使用UITableVIewDiffableDataSource加载,ios,swift,tableview,Ios,Swift,Tableview,我正在尝试使用新的DiffableDataSource api实现表视图,但单元格根本不加载: var tableView = UITableView() var currencyPairsArray = [String]() lazy var fetcher = NetworkDataFetcher() lazy var searchText = String() lazy var searchArray = [String]() lazy var s
var tableView = UITableView()
var currencyPairsArray = [String]()
lazy var fetcher = NetworkDataFetcher()
lazy var searchText = String()
lazy var searchArray = [String]()
lazy var searchController: UISearchController = {
let controller = UISearchController(searchResultsController: nil)
controller.hidesNavigationBarDuringPresentation = false
controller.obscuresBackgroundDuringPresentation = false
controller.searchBar.delegate = self
return controller
}()
fileprivate var dataSource : UITableViewDiffableDataSource<Section, String>!
var searchBarIsEmpty: Bool {
return searchController.searchBar.text?.isEmpty ?? true
}
override func viewDidLoad() {
super.viewDidLoad()
setupVC()
setupTableView()
setupDataSource()
performSearch(with: nil)
fetcher.fetchCurrencyPairs { [weak self] pairsArray in
self?.currencyPairsArray.append(contentsOf: pairsArray)
DispatchQueue.main.async {
self?.tableView.reloadData()
}
}
//tableView.reloadData()
// Do any additional setup after loading the view.
}
func setupVC() {
view.backgroundColor = .white
view.addSubview(tableView)
navigationItem.title = "Currency pairs"
navigationItem.searchController = searchController
navigationItem.hidesSearchBarWhenScrolling = false
}
func setupTableView() {
tableView.delegate = self
tableView.translatesAutoresizingMaskIntoConstraints = false
tableView.pinToSuperView()
tableView.register(UINib(nibName: "CurrencyPairCell", bundle: nil), forCellReuseIdentifier: CurrencyPairCell.reuseIdentifier)
}
func setupDataSource() {
dataSource = UITableViewDiffableDataSource<Section, String>(tableView: tableView, cellProvider: { [weak self] (tableView, indexPath, _) -> UITableViewCell? in
let cell = tableView.dequeueReusableCell(withIdentifier: CurrencyPairCell.reuseIdentifier, for: indexPath) as! CurrencyPairCell
cell.delegate = self
let pair = self?.currencyPairsArray[indexPath.row].formattedPair()
cell.currencyPairLabel.text = pair
cell.currencyPair = self?.currencyPairsArray[indexPath.row] ?? ""
return cell
})
}
func performSearch(with filter: String?) {
var snapshot = NSDiffableDataSourceSnapshot<Section, String>()
if let filter = filter {
let filteredPairs = currencyPairsArray.filter {$0.contains(filter)}
snapshot.appendSections([.main])
snapshot.appendItems(filteredPairs, toSection: .main)
dataSource.apply(snapshot, animatingDifferences: true)
} else {
let pairs = currencyPairsArray.sorted()
snapshot.appendSections([.main])
snapshot.appendItems(pairs, toSection: .main)
dataSource.apply(snapshot)
}
}
}
extension CurrencyListViewController: UISearchBarDelegate {
func searchBar(_ searchBar: UISearchBar, textDidChange searchText: String) {
performSearch(with: searchText)
}
}
extension CurrencyListViewController {
fileprivate enum Section: Hashable {
case main
}
}
var tableView=UITableView()
var currencyPairArray=[String]()
lazy var fetcher=NetworkDataFetcher()
lazy var searchText=String()
lazy var searchArray=[String]()
lazy var searchController:UISearchController={
let controller=UISearchController(searchResultsController:nil)
controller.hidesNavigationBarDuringPresentation=false
controller.obscuresBackgroundDuringPresentation=false
controller.searchBar.delegate=self
返回控制器
}()
fileprivate var数据源:UITableViewDiffableDataSource!
var searchBarIsEmpty:布尔{
返回searchController.searchBar.text?.isEmpty??true
}
重写func viewDidLoad(){
super.viewDidLoad()
setupVC()
setupTableView()
setupDataSource()
性能搜索(带:无)
fetcher.fetchCurrencyPairs{[weake self]中的成对数组
self?.currencyPairsaray.append(内容:Pairsaray)
DispatchQueue.main.async{
self?.tableView.reloadData()
}
}
//tableView.reloadData()
//加载视图后执行任何其他设置。
}
func setupVC(){
view.backgroundColor=.white
view.addSubview(tableView)
navigationItem.title=“货币对”
navigationItem.searchController=searchController
navigationItem.HideseArchBarWhenScrolling=false
}
func setupTableView(){
tableView.delegate=self
tableView.translatesAutoresizingMaskIntoConstraints=false
tableView.pinToSuperView()
tableView.register(UINib(nibName:“CurrencyPairCell”,bundle:nil),forCellReuseIdentifier:CurrencyPairCell.reuseIdentifier)
}
func setupDataSource(){
dataSource=UITableViewDiffableDataSource(tableView:tableView,cellProvider:{[weak self](tableView,indexPath,715;)->UITableViewCell?在
让cell=tableView.dequeueReusableCell(标识符为:CurrencyPairCell.reuseIdentifier,for:indexPath)作为!CurrencyPairCell
cell.delegate=self
让pair=self?.currencyPairArray[indexPath.row].formattedPair()
cell.currencyPairLabel.text=成对
cell.currencyPair=self?.currencyPairArray[indexPath.row]??“
返回单元
})
}
func performSearch(带过滤器:字符串?){
var snapshot=NSDiffableDataSourceSnapshot()
如果let filter=filter{
让filteredPairs=currencyPairsary.filter{$0.contains(filter)}
snapshot.appendSections([.main])
snapshot.appendItems(filteredPairs,toSection:.main)
dataSource.apply(快照、动画差异:true)
}否则{
让pairs=currencyPairArray.sorted()
snapshot.appendSections([.main])
snapshot.appendItems(成对,toSection:.main)
dataSource.apply(快照)
}
}
}
扩展电流列表视图控制器:UISearchBarDelegate{
func searchBar(searchBar:UISearchBar,textDidChange searchText:String){
performSearch(带:searchText)
}
}
扩展CurrencyListViewController{
fileprivate枚举节:可哈希{
主箱
}
}
另外,我从xcode收到一个警告:
[TableView]仅警告一次:UITableView被告知布局其可见单元格和其他内容,而不在视图层次结构中(尚未将表视图或其中一个超级视图添加到窗口)。这可能会导致错误,因为在没有准确信息(例如,表视图边界、特征集合、布局边距、安全区域插入等)的情况下,强制表视图内的视图加载和执行布局,并且还会由于额外的布局过程而导致不必要的性能开销。在UITableViewAlertForLayoutOutsideViewHierarchy上创建一个符号断点,以在调试器中捕获此断点,并查看导致此情况发生的原因,因此,如果可能,您可以完全避免此操作,或者将其推迟到将表视图添加到窗口中。表视图:;层=;contentOffset:{0,0};contentSize:{414,0};adjustedContentInset:{0,0,0,0};数据源:>首先,代码中有一个很大的设计错误 使用
UITableViewDiffableDataSource
停止思考索引路径和数据源数组。相反,在datasource项目中思考
func setupDataSource() {
dataSource = UITableViewDiffableDataSource<Section, String>(tableView: tableView, cellProvider: { [weak self] (tableView, _, pair) -> UITableViewCell? in
let cell = tableView.dequeueReusableCell(withIdentifier: CurrencyPairCell.reuseIdentifier, for: indexPath) as! CurrencyPairCell
cell.delegate = self
cell.currencyPairLabel.text = pair.formattedPair()
cell.currencyPair = pair
return cell
})
}
在setupDataSource
中,无论您是否要显示过滤后的数据,您总是从数据源数组currencyPairArray
获取行的模型项。忘记currencyPairArray
和索引路径。利用闭包中表示项的第三个参数
func setupDataSource() {
dataSource = UITableViewDiffableDataSource<Section, String>(tableView: tableView, cellProvider: { [weak self] (tableView, _, pair) -> UITableViewCell? in
let cell = tableView.dequeueReusableCell(withIdentifier: CurrencyPairCell.reuseIdentifier, for: indexPath) as! CurrencyPairCell
cell.delegate = self
cell.currencyPairLabel.text = pair.formattedPair()
cell.currencyPair = pair
return cell
})
}
使用UITableViewDiffableDataSource
时,决不调用tableView.reloadData()
,这很可能是问题的原因
替换
performSearch(with: nil)
fetcher.fetchCurrencyPairs { [weak self] pairsArray in
self?.currencyPairsArray.append(contentsOf: pairsArray)
DispatchQueue.main.async {
self?.tableView.reloadData()
}
}
与
那很有效!我现在明白这件事该怎么办了!非常感谢。
fetcher.fetchCurrencyPairs { [weak self] pairsArray in
self?.currencyPairsArray.append(contentsOf: pairsArray)
DispatchQueue.main.async {
self?.performSearch(with: "", animatingDifferences: false)
}
}