【问题标题】:Using Swift to display Array in MainStoryboard Table View?使用 Swift 在 MainStoryboard 表视图中显示数组?
【发布时间】:2015-04-20 13:20:48
【问题描述】:

我已经能够使用 Swift 语言创建一个数组了。但是随后能够映射这个数组以显示在 iOS 上的 Main.storyboard 中是我绘制空白的地方。我理解可以有TableViewControllers 和TableViews 和TableViewCells,但不知何故我根本无法从

 myArray ["Viktor Schauberger", "Nikola Tesla", "Wilhelm Reich", "John Bedini", "Walter Russell", "Ed Leedskalnin", "R. Buckminster Fuller", "John Keely", "Nathan Stubblefield", "Ismael Aviso", "Hector Perez Torres", "Thomas Henry Moray"]

能够在 iOS 模拟器中显示它,然后从显示一系列事物到使它们可点击。但就目前而言,我只满足于弄清楚如何使用 Swift 1.2 在 TableView / TableViewController 中直观地显示这个数组 - 无论哪个适用。

【问题讨论】:

  • 我认为您遇到的 Swift 问题不如 UIKit 问题。我建议阅读有关UITableViewDataSourceUITableViewDelegate 协议如何工作的详细信息。

标签: arrays uitableview swift tableview


【解决方案1】:
override func tableView(tableView: UITableView!, cellForRowAtIndexPath indexPath: NSIndexPath!) -> UITableViewCell! {
    let cell = tableView.dequeueReusableCellWithIdentifier("Cell", forIndexPath:indexPath) as MyCell
    // no "if" - the cell is guaranteed to exist
    // ... do stuff to the cell here ...
    cell.textLabel.text = self.Myarray objectAtIndex:indexPath.row;
    // ...
    return cell
}

试试这个:)

或转到下面的此链接可能会对您有所帮助https://www.weheartswift.com/how-to-make-a-simple-table-view-with-ios-8-and-swift/

用于加载原型单元 prototype cell

【讨论】:

  • Sarath,请问我应该把这段代码放在哪里? :)
  • 嗨,我尝试将 myArray 和您的信息添加到其中,并得到“一行上的连续语句必须用 ; 分隔”在 cell.textLabel.text = self.Myarray objectAtIndex:indexPath.row;总体而言,我感到非常困惑。此外,这里说“'tableview(_:cellForRowAtIndexPath:)' 的结果和参数具有与协议 UITableViewDataSource 预期的不同的可选性。还有“使用未声明的类型 'MyCell'.. 等等。
  • 老兄,“MyCell”是我在项目中创建的自定义单元的名称。如果您正在加载任何自定义单元格或其他内容,您可以使用该名称而不是 Mycell
  • 如果您使用的是原型单元。我认为索引路径处的行的正常单元格就足够了
  • tableView(tableView: UITableView!, cellForRowAtIndexPath indexPath: NSIndexPath!) -> UITableViewCell!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-01-05
  • 2014-01-14
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多