swift - '下标(_ :)' is unavailable: cannot subscript String with an Int (Swift 5)

标签 swift uitableview character subscript custom-sections

我在这里只找到了一个答案。我的问题是我没有使用 Character,除了 String 是一个字符集合。我尝试将其设为 String 即;

        let name = String(array[indexPath.section][indexPath.row])

但我遇到了同样的错误无法将“字符”类型的值分配给“字符串”类型?

这是我的手机:

struct SectionStrings {
    let sections = ["Stuff", "More Stuff", "Best Stuff"]
}

struct ArrayStrings {
    let array = ["Little Stuff", "Bigger Stuff", "Biggest Stuff"]

}

struct DetailArrayStrings {
    let detailArray = ["Teeny Stuff", "Teentsie Weentsie Stuff", "Invisible Stuff"]

}

这是我的 Controller :

let sections = SectionStrings().sections
var array = ArrayStrings().array
let detailArray = DetailArrayStrings().detailArray

 override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {

        let cell = tableView.dequeueReusableCell(withIdentifier: reuseIdentifier, for: indexPath) as! SettingsCell

        let name = array[indexPath.section][indexPath.row] // errors here
        cell.textLabel?.text = name
        cell.textLabel?.font = UIFont.boldSystemFont(ofSize: 17)
        cell.textLabel?.textColor = .white

        let detailName = detailArray[indexPath.section][indexPath.row] // and here
        cell.detailTextLabel?.text = detailName
        cell.detailTextLabel?.font = UIFont.systemFont(ofSize: 15)
        cell.detailTextLabel?.textColor = .white
        cell.detailTextLabel?.numberOfLines = 0

        cell.selectionStyle = .none
        cell.backgroundColor = .black

        return cell
    }

最佳答案

您应该仅使用 row 获取 indexPath 所需的 name,如下所示,

let name = array[indexPath.row]
cell.textLabel?.text = name
cell.textLabel?.font = UIFont.boldSystemFont(ofSize: 17)
cell.textLabel?.textColor = .white

let detailName = detailArray[indexPath.row]

关于swift - '下标(_ :)' is unavailable: cannot subscript String with an Int (Swift 5),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58177287/

相关文章:

swift - NSAttributedString:图像未显示在 UILabel 中

ios - 无法在 iOS 中通过 cocoa pod 安装 Realm

iOS 静态表格 View 在底部显示一些虚拟行

ios - 在 Xamarin.iOS 中为 UITableView 的默认部分标题添加底部边框

c++ - C++ 编程语言 12.7 问题 2

ios - 从代码中删除打印行

ios - 从解析中检索用户数据以快速显示在 TableView 中

javascript - 如何防止 '0' 成为文本框中输入的第一个字符

javascript - 计算字符串中 2 个指定单词之间的字符数

swift - 将 UIBezierPath 居中