【问题标题】:How can I retrieve the array elements in a different cells swift ios如何快速检索不同单元格中的数组元素 ios
【发布时间】:2018-01-25 05:34:16
【问题描述】:

我正在检索一个数组元素,但所有元素都在单个单元格中检索,而不是每个新单元格中的每个元素。我希望PDFList 数组的每个元素都应该在每个单元格中。

这是我得到的

这是我迄今为止为获取数据所做的工作:

import UIKit

@available(iOS 11.0, *)
class PDFListViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {

var PDFList = [String]()

@IBOutlet weak var PDFListTable: UITableView!

func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    return PDFList.count
}
func numberOfSections(in tableView: UITableView) -> Int {
    return 1
}

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell = PDFListTable.dequeueReusableCell(withIdentifier: "PDFCell") as! PDFTableViewCell
    cell.pdfLabel.text = PDFList[indexPath.row]
    return cell
}


override func viewDidLoad() {
    super.viewDidLoad()

    PDFListTable.delegate = self
    PDFListTable.dataSource = self

    PDFList = ["Form title 1, Form title 2, Form title 3,Form title 4, Form title 5, Form title 6,Form title 7, Form title 8, Form title 9,Form title 10, Form title 11, Form title 12"]
    // Do any additional setup after loading the view.
}

override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated.
}
}

【问题讨论】:

  • 请遵守标准。变量和函数名称以小写字母开头。类和结构名称以大写字母开头。

标签: ios arrays swift uitableview tableview


【解决方案1】:

你的数组必须是这样的:

PDFList = ["Form title 1", "Form title 2", "Form title 3", "Form title 4", "Form title 5", "Form title 6", "Form title 7", "Form title 8", "Form title 9", "Form title 10", "Form title 11", "Form title 12"]

在双引号“”内的值定义了数组的单个元素..这就是你得到上述结果的原因。

【讨论】:

    【解决方案2】:

    您在 PDFList 中声明了单个字符串。

    PDFList = ["Form title 1", "Form title 2", "Form title 3",...]
    

    【讨论】:

      【解决方案3】:

      您的 PDFListArray 中索引为零处的 String。在您的 PDFList[IndexPath.row] 将返回 Index[0] ,它位于表格列表的第一行。

      【讨论】:

        【解决方案4】:

        如果您的数组包含单个字符串,那么您可以访问不同单元格的不同元素。

        例如:- cell.label.text = arrlist[indexPath.row]

        但是如果您的数组包含键值数据,那么您可以通过以下形式访问数据:-

        “arrData”:[ { “纬度”:“46.1817”, “经度”:“150.7960”, }, { “纬度”:“28.1069”, “经度”:“62.0532”, } ]

        例如:- cell.label.text = arrData["latitude"][row]

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2015-02-15
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多