【发布时间】:2020-09-20 12:42:49
【问题描述】:
我正在从右到左显示一个水平集合视图,并且遇到了一个问题,即在显示元素时,它们从末尾显示(而不是从左到右):
- 在运行架构选项中,应用语言设置为从右到左伪语言
- 如果我在启动应用程序之前添加项目而不是在之后添加它们并调用 reloadData,则不会出现问题
- 附上示例项目和屏幕截图
提出的一个解决方案是添加下面的代码,但需要我颠倒添加项目的顺序。我想知道是否有更好的方法来做到这一点,并且在 API 中而不是使用覆盖扩展
extension UICollectionViewFlowLayout {
open override var flipsHorizontallyInOppositeLayoutDirection: Bool {
return true
}
}
我们将不胜感激
代码:
import UIKit
class HorizontalScrollTestViewController: UIViewController, UICollectionViewDataSource , CellDelegate{
var items = [String]()
@IBOutlet weak var collectionView: UICollectionView!
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return items.count
}
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "ReusableCell", for: indexPath) as! CategoryCollectionViewCell
cell.category.setTitle(items[indexPath.row] , for: UIControl.State.normal)
cell.delegate = self
return cell
}
override func viewDidLoad() {
super.viewDidLoad()
let dispatchQueue = DispatchQueue(label: "resourcesQueue", qos: .background)
dispatchQueue.async {
self.addItems()
DispatchQueue.main.async {
self.collectionView.reloadData()
}
}
collectionView.dataSource = self
collectionView?.register(UINib(nibName: "CategoryCollectionViewCell", bundle: nil), forCellWithReuseIdentifier: "ReusableCell")
}
func buttonPressed(_ cell: UICollectionViewCell) {
if let row = self.collectionView.indexPath(for: cell)?.row {
print(items[row])
}
}
func addItems() {
self.items.append("test 10")
self.items.append("test 9")
self.items.append("test 8")
self.items.append("test 7")
self.items.append("test 6")
self.items.append("test 5")
self.items.append("test 4")
self.items.append("test 3")
self.items.append("test 2")
self.items.append("test 1")
}
}
在应用启动后添加项目并使用 reloadData 时的示例:
预期行为
【问题讨论】:
-
请分享您如何填充集合视图的代码 sn-ps,而不是分享整个项目
-
jst让我知道当你添加新元素时,比如说“Test11”,那么集合应该滚动到哪里。??极右(test1)或极左(test11)
-
@S1LENTWARRIOR 代码 sn-p 添加
-
@dRAGONAIR,谢谢,看看我更新的评论
标签: ios swift xcode uicollectionview right-to-left