ios - UITableViewCell 从 1 计数到 50

标签 ios swift uitableview

我正在使用 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/

相关文章:

ios - self.tableView.reloadData() 在 Swift 中不起作用

ios - 从文本字段在 tableView 中添加新单元格

iphone - 背景图像不会扩展到 TableView 单元格中的附件 View 背景

android - 如何从本地数据库导出/导入数据?

iOS Xcode SPM 未能解开父类(super class)

ios - 只显示带小数点的 Double 的小数部分

ios - 如何在 TableView 中使用 TableViewController 类?

iOS 区域监控,设备重启后位置不会发生重大变化

Swift ObjectMapper 不调用 didSet

ios - 如何使用 UIKit 在 Swift 5 中没有任何第三方库的情况下在 iOS 应用程序中使用 GIF?