支持 iOS 12 和 13 时的 UITableView 和 Diffable 数据源



我已经在我的项目中实现了UITableView Diffable Datasource。它在 IOS 13 中运行良好。 当我在iOS 13版本以下运行此应用程序时,它会警告我它仅在ios 13上可用。所以我正在尝试为以下的iOS 13版本实现UITableView。

UITableView 代码已完成,但我在 iOS 13 上面临此问题。

@available(iOS 13.0, *)
typealias UserDataSource = UITableViewDiffableDataSource<TblSection, YoutubeVideoModel>
@available(iOS 13.0, *)
typealias UserSnapshot = NSDiffableDataSourceSnapshot<TblSection, YoutubeVideoModel>

在这里,我声明数据源变量

class SearchViewController: UIViewController {
@available(iOS 13.0, *)
var datasource: UserDataSource! //Stored properties cannot be marked potentially unavailable with '@available'
}

初始化数据源和快照

@available(iOS 13.0, *)
func configureDatasource(){
datasource = UITableViewDiffableDataSource<TblSection, YoutubeVideoModel>(tableView: searchTblView, cellProvider: { (tableView, indexPath, modelVideo) -> VideoTableViewCell? in
self.configurationCell(indexPath: indexPath)
})
}
@available(iOS 13.0, *)
func createSnapshot(users: [YoutubeVideoModel]){
var snapshot = UserSnapshot()
snapshot.appendSections([.first])
snapshot.appendItems(users)
datasource.apply(snapshot, animatingDifferences: true)
} 

当我声明数据源时,我遇到了此错误,请帮助谢谢。

//Stored properties cannot be marked potentially unavailable with '@available'

我在集合视图上也面临同样的问题。

我们可以在计算属性上使用@available。但是,lazy变量被视为计算属性,因此您也可以对它们使用@available。这有一个很好的好处,即删除额外存储属性的样板和强制强制转换 - 事实上,它在 iOS 10 之前的代码中没有留下该属性的证据。

您可以简单地像这样声明它:

@available(iOS 13.0, *)
lazy var datasource = UserDataSource()

您可以查看此要点以获取更多信息 https://gist.github.com/YogeshPateliOS/b2b13bfe5f7eef5cd7fa4a894cd35d5a

最新更新