【问题标题】:How to put data from array into label Swift 3如何将数组中的数据放入标签Swift 3
【发布时间】:2017-05-04 10:03:05
【问题描述】:

我对 Swift 很陌生,问题是如何在标签中表示数组中的值。

我想要一个带有单元格的 TableView 动态地将数组中的值表示到将在 tableView 行中创建的标签中。

import UIKit
import Foundation



    class TableViewMarketItemsViewCell: UITableViewController {


        var fruits = ["Avocado", "Apricot", "Pomegranate", "Quince"]
        var PriceArray = ["1000 тг.","4000 тг.","3000 тг.","2000 тг."]
        var categoryArray = ["Green category","Maroon category","Red category","Yellow category"]

        // MARK: - UITableViewDataSource

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

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

        override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
            let cell = tableView.dequeueReusableCell(withIdentifier: "LabelCell", for: indexPath) as! TableViewCell

           let fruitName = fruits[indexPath.row]


            cell.productTitle.text = fruitName
            cell.productImage.image = UIImage(named: fruitName)



            return cell
        }


        }

提前谢谢

【问题讨论】:

  • 你已经尝试了什么?你经历了哪些教程,哪些没用,或者你不明白哪些?显示一些代码。

标签: ios arrays uitableview swift3 labels


【解决方案1】:
import UIKit
import Foundation



    class TableViewMarketItemsViewCell: UITableViewController {


        var fruits = ["Avocado", "Apricot", "Pomegranate", "Quince"]
        var PriceArray = ["1000 тг.","4000 тг.","3000 тг.","2000 тг."]
        var categoryArray = ["Green category","Maroon category","Red category","Yellow category"]

        // MARK: - UITableViewDataSource

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

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

        override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
            let cell = tableView.dequeueReusableCell(withIdentifier: "LabelCell", for: indexPath) as! TableViewCell

           let fruitName = fruits[indexPath.row]


            cell.productTitle.text = fruitName
            cell.productImage.image = UIImage(named: fruitName)
            cell.productPrice.text = PriceArray[indexPath.row]
            cell.productsubTitle.text = categoryArray[indexPath.row]


            return cell
        }



        }

这对我有帮助。

结果如下图: img

【讨论】:

    【解决方案2】:

    要向 UITableViewcell 中插入数据,请使用以下代码:

    import UIKit
    
    class ViewController: UIViewController,UITableViewDataSource {
        @IBOutlet var tableView: UITableView!
        var dataArray:NSArray!
    
        override func viewDidLoad() {
            super.viewDidLoad()
            // Do any additional setup after loading the view, typically from a nib.
            tableView.dataSource = self
            dataArray = NSArray(objects: "a","b","c")
        }
    
        func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
            return dataArray.count
        }
        func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
            let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath)
            cell.textLabel?.text = dataArray.object(at: indexPath.row) as? String
            return cell
        }
    
    }
    

    tableView是UItableView的出口。

    【讨论】:

      【解决方案3】:

      您可以从如下数组中填充UITableView: (假设您的数组有一个字符串值列表):

      func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
          return array.count
      }
      
      func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
      
          // Creating the tableView cell
          let tableViewCell = tableView.dequeueReusableCell(withIdentifier: "tableViewCell", for: indexPath) as! UITableViewCell
      
          //Assigning values
          tableViewCell.lblName?.text = array.object(at: indexPath.row) as? String
      
          return tableViewCell
      }
      

      通过这种方式,您可以在您的tableView 中显示从您的arraylabel 的值。

      【讨论】:

      • 假设你的数组有一个字符串值列表在 Swift 中它是array[indexPath.row],没有类型转换,没有可选参数,no object(at:
      • @vadian:先生,我没听明白。
      • 一个字符串数组是[String](),类型是不同的且非可选的。所以类型转换为 optional (as? String) 是没有意义的。
      猜你喜欢
      • 2012-07-03
      • 1970-01-01
      • 2015-05-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-01-05
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多