2016-12-07 138 views
0

我有一个公司类,其中每个公司都有一个名称,标志和股票价格,都是String类型。这些被保存在一个NSMutableArray中。作为字符串访问自定义对象属性(NSMutableArray)

我想要在tableview中访问这些属性和标签等

这是我的公司类使用它们:

import UIKit 
import Alamofire 
import SwiftyJSON 



var companyController: CompanyController? 

var companies:NSMutableArray? = NSMutableArray() 

var applePrice: String? 
var googlePrice: String? 
var twitterPrice: String? 
var teslaPrice: String? 
var samsungPrice: String? 

var stockPrices = [String]() 

let stockUrl = "https://query.yahooapis.com/v1/public/yql?q=select%20symbol%2C%20Ask%2C%20YearHigh%2C%20YearLow%20from%20yahoo.finance.quotes%20where%20symbol%20in%20(%22AAPL%22%2C%22GOOG%22%2C%22TWTR%22%2C%22TSLA%22%2C%20%22SSNLF%22)&format=json&env=store%3A%2F%2Fdatatables.org%2Falltableswithkeys" 



class Company: NSObject { 

var companyName: String? 
var companyLogo: String? 
var stockPrice: String? 
init(companyName:String, companyLogo:String, stockPrice:String) { 
    self.companyName = companyName 
    self.companyLogo = companyLogo 
    self.stockPrice = stockPrice 

    companies?.add(apple) 
    companies?.add(google) 
    companies?.add(twitter) 
    companies?.add(tesla) 
    companies?.add(samsung) 
} 
} 




func stockFetcher() { 

Alamofire.request(stockUrl).responseJSON { (responseData) -> Void in 
    if((responseData.result.value) != nil) { 
     let json = JSON(responseData.result.value!) 
     if let appleStockPrice = json["query"]["results"]["quote"][0]["Ask"].string { 
      print(appleStockPrice) 
      applePrice = appleStockPrice 
     } 
     if let googleStockPrice = json["query"]["results"]["quote"][1]["Ask"].string { 
      print(googlePrice) 
      googlePrice = googleStockPrice 
     } 
     if let twitterStockPrice = json["query"]["results"]["quote"][2]["Ask"].string { 
      print(twitterStockPrice) 
      twitterPrice = twitterStockPrice 
     } 
     if let teslaStockPrice = json["query"]["results"]["quote"][3]["Ask"].string { 
      print(teslaStockPrice) 
      teslaPrice = teslaStockPrice 
     } 
     if let samsungStockPrice = json["query"]["results"]["quote"][4]["Ask"].string { 
      print(samsungStockPrice) 
      samsungPrice = samsungStockPrice 
     } 
     let stockPriceArray = [applePrice, googlePrice, twitterPrice, teslaPrice, samsungPrice] 
     stockPrices = stockPriceArray as! [String] 
     companyController?.tableView.reloadData() 
     print(json) 
    } 
} 
} 


let apple = Company(companyName: "Apple", companyLogo: "AppleLogo", stockPrice: applePrice!) 
let google = Company(companyName: "Google", companyLogo: "GoogleLogo", stockPrice: googlePrice!) 
let twitter = Company(companyName: "Twitter", companyLogo: "TwitterLogo", stockPrice: twitterPrice!) 
let tesla = Company(companyName: "Tesla", companyLogo: "TeslaLogo", stockPrice: teslaPrice!) 
let samsung = Company(companyName: "Samsung", companyLogo: "SamsungLogo", stockPrice: samsungPrice!) 

在我CompanyController,我初始化

var company = [Company]() 

,现在想在单元格中使用公司信息,例如在cellForRowAt indexPath中,我想将公司名称分配给文本标签。

let cell = tableView.dequeueReusableCell(withIdentifier: cellId, for: indexPath) as! CompanyCell 

    //Text label 
    cell.textLabel?.text = <what goes here?>[indexPath.row] 

如何访问NSMutableArray中的公司属性以获取名称等?我希望能够做到像

company.companies.companyName[indexPath.row] 

回答

0

如果你需要“company.CompanyProperty”你可以创建一个数组,并把所有的公司。 在功能中可以轻松地所有的礼仪:

cell.textLabel?.text = [indexPath.row].companyName 
cell.textLabel?.text2 = [indexPath.row].companyLogo 
cell.textLabel?.text3 = [indexPath.row].stockPrice 
+0

我认为这可能是我必须做的,我只是想知道如果有一种方法可以直接从我的NSMutableArray抢特性,而无需给每个属性一个单独的数组来绘制。 – d0xi45