【问题标题】:Swift TableViewCell ArraySwift TableViewCell 数组
【发布时间】:2016-05-21 03:35:20
【问题描述】:

我的情况:我想在第 1 节中的索引 X 的一行中的索引 X 处保存数组中的数据。

我的代码是:

override func numberOfSectionsInTableView(tableView: UITableView) -> Int {
    return 1
}

override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    return setObjectToPass.count
}

override func tableView(tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
    return "Section \(section)"
}

override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
    var cellEmpty = tableView.dequeueReusableCellWithIdentifier("LabelCell")
    var countCell = 0


    while countCell < setObjectToPass.count {
        let indexPaths = NSIndexPath(forRow: countCell, inSection: 0)
        let cell = tableView.dequeueReusableCellWithIdentifier( "LabelCell", forIndexPath: indexPaths)

        cell.textLabel!.text = String(setObjectToPass[countCell])
        print(cell)
        countCell+=1
        return cell
    }

我的问题是只有数组 SetObjectToPass 的第一个索引被传递并设置到 Cell.text

 while counter < fetchResult?.count {

    let set = fetchResult![counter]
    counter+=1;
    setObject.append((set.reps?.integerValue)!)
}

【问题讨论】:

  • 我建议研究 UITableViewDataSource 的基本实现。你在cellForRowAtIndexPath(_:) 方法中所做的事情很奇怪。
  • 好的,谢谢,我试试,还有什么建议吗?
  • setObjectToPass 数组值是什么?
  • 这些是整数值

标签: ios arrays swift uitableview tableview


【解决方案1】:

您错误地实现了tableView(_:cellForRowAtIndexPath:indexPath:) 方法。

请记住,UITableViewDelegate 中的每个委托方法都像是在问你一个问题。例如,numberOfSectionsInTableView(_:) 就像问您“您的表格视图中需要多少个部分?”。您通过返回一个值来回答问题。

tableView(_:cellForRowAtIndexPath:indexPath:) 类似。它也提出了一个问题。它询问“我应该在这个索引路径的表格行中显示什么?”

在您的代码中,您似乎想要给出多个答案 - 循环遍历数组并尝试多次返回。但是这样不行,你只能给出一个答案。

while 循环的第一次迭代中,执行命中return 并停止。这就是为什么您只能看到第一个表格单元格。

因此,您应该更改您的代码,以便它只给出一个问题的答案:

let cell = tableView.dequeueReusableCellWithIdentifier("LabalCell")
cell.textLabel?.text = String(setObjectsToPass[indexPath.row])
return cell

【讨论】:

  • 我收到 setObjectToPass 的“使用未解析的标识符”。声明在这里:class TableViewControllerResult: UITableViewController { //Fetch variable from other ViewController var counterToPass: Int! var counterSetToPass: Int! var setObjectToPass: [Int]! override func viewDidLoad() { super.viewDidLoad() print(counterToPass) }
  • 糟糕!我的意思是setObjectToPass@HendrikBreezy
  • 结合其他评论它的工作,tyvm!
【解决方案2】:

不要在cellForRowAtIndexPath 委托方法中使用循环。 cellForRowAtIndexPath 方法根据numberOfRowsInSection 计数行调用每一行,只需使用indexpath.row,使用此代码,

override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
    var cellEmpty = tableView.dequeueReusableCellWithIdentifier("LabelCell")

    let cell = tableView.dequeueReusableCellWithIdentifier( "LabelCell", forIndexPath: indexPaths)

    cell.textLabel!.text = setObjectToPass[indexPath.row] as? String

    return cell
}

希望对你有帮助

【讨论】:

  • 我使用 String(setObjectToPass[indexPath.row]) 保存标签 Text 因为它无法将 Int 转换为 String 现在工作!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-04-15
  • 1970-01-01
  • 1970-01-01
  • 2017-09-17
  • 2019-11-06
  • 2020-05-31
相关资源
最近更新 更多