【问题标题】:Showing Values in an Array on Each Row [closed]在每一行的数组中显示值[关闭]
【发布时间】:2017-12-31 04:52:41
【问题描述】:

我已经用我知道的各种方法积极尝试解决这个问题,但都没有成功,任何解决这个问题的帮助都会很棒。

我正在尝试创建一个数字数组(1 到 50)并在视图控制器的不同行上显示每个数字。

我遇到了一个障碍,Xcode 建议应该有一个返回值,但是如果我将返回值放在 for 循环之外,它将无法被识别(参见图片以获得视觉表示)。

【问题讨论】:

  • 删除for i in numbers {循环和右括号}并将String(i)更改为numbers[indexPath.row]

标签: arrays swift xcode


【解决方案1】:

首先像这样创建一个整数数组

var numbers = Array(0...50)

然后将您的 cellForRowAt 方法替换为

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell = UITableViewCell(style: .default, reuseIdentifier: "Cell")
    // since this method is called each time a cell is about to display so for loop is not the correct way set text to a textLabel
    cell.textLabel?.text = String(numbers[indexPath.row])
    return cell
}

并确保您的数据源已连接到您的 UIViewController,否则您将无法在 UITableView 中看到数据。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-02-15
    • 2021-11-13
    • 2021-09-19
    • 1970-01-01
    • 2018-09-18
    • 2014-03-06
    • 2021-09-15
    相关资源
    最近更新 更多