2016-12-07 7 views
0

私は、各会社が名前、ロゴ、および株価を持つCompanyクラスを持っています。これらはNSMutableArrayで保持されます。私はテーブルビューでこれらのプロパティにアクセスし、ラベルなどString(NSMutableArray)としてのカスタムオブジェクトのプロパティへのアクセス

ここに私の会社クラスの中でそれらを使用しようとしている

:私のCompanyControllerで

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!) 

、私は初期化

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.CompanyPropertyを」必要があります場合は

company.companies.companyName[indexPath.row] 

答えて

0

ような何かを行うことができるように願っています。

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

私はそれが私のNSMutableArrayのからの直接の特性をつかむための方法があります場合、私はちょうどなくても、思ったんだけど、私がしなければならないだろうおそらく何だと思う:あなたは簡単にすべての礼儀を得ることができる機能で 各プロパティに描画する別の配列を与えます。 – d0xi45

関連する問題