我正在使用 Xcode 9 学习 Swift 4,并尝试制作一个包含 50 个单元格的 TableView。 在单元格 1“1”中 在单元格 2“2”中 等等。
这是我的代码:
import UIKit
class ViewController: UIViewController, UITableViewDataSource, UITableViewDelegate{
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return 50
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = UITableViewCell(style: UITableViewCellStyle.default, reuseIdentifier: "Cell")
cell.textLabel?.text = String[indexPath.row + 1]
}
}
现在编译器在这一行显示 cell.textLabel?.text = String[indexPath.row + 1]
“实例成员“下标”不能在字符串类型上使用”
如何制作 50 个内容为 1 到 50 的单元格?
最佳答案
您可以使用其中一个或以下:
cell.textLabel?.text = String(indexPath.row + 1)
或
cell.textLabel?.text = "\(indexPath.row + 1)"
关于ios - UITableViewCell 从 1 计数到 50,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51090132/