2017-05-04 27 views
-5

對於Swift,我真的很陌生,問題是如何從標籤中的數組表示值。如何將數據從數組中放入標籤Swift 3

我想要一個帶有單元格的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 
     } 


     } 

日Thnx提前

+0

https://www.weheartswift.com/how-to-make-a-simple-table-view-with-ios-8-and-swift/ –

+1

你已經試過了什麼?你經歷了哪些教程,什麼不起作用,或者你不明白什麼?顯示一些代碼。 – Losiowaty

回答

0

您可以從數組填充一個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的值。

+1

*假設你的數組有一個字符串值的列表*在Swift中它是'array [indexPath.row]',沒有類型轉換,沒有可選項,'沒有對象(在:' – vadian

+0

@vadian:我沒有讓你sir 。 – iPeter

+0

字符串數組是'[String]()',類型是不同的和非可選的。所以類型轉換爲可選的('as?String')是無意義的。 – vadian

0
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

1

對於插入數據的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的出口。

相關問題