我创建了这个数组: let yourShares = ["90%","40%", "23%","15%","10%"]
然后访问这个:
for yourShare in yourShares {
dataSource[0].yourShareTitle = "90%"
dataSource[1].yourShareTitle = "40%"
dataSource[2].yourShareTitle = "23%"
dataSource[3].yourShareTitle = "10%"
}
然后为了访问它,我将此代码放入 Collection View 的扩展中
让 yourShare = model.dataSource?[indexPath.row]
cell.yourShareLabel.text = "您的分享\(yourShare!.yourShareTitle)"
cell.titleLabel?.font = UIFont.systemFont(ofSize: 1)
问题是当我运行它时它只是打印可选。为什么,我该如何解决这个问题? 〜谢谢
最佳答案
你需要打开它
if let yourShare = model.dataSource?[indexPath.row]?.yourShareTitle as? String {
cell.yourShareLabel.text = "Your Share \(yourShare)"
cell.titleLabel?.font = UIFont.systemFont(ofSize: 1)
}
关于swift - 访问 For in 循环 Collection View Swift,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45203555/