我正在使用最新版本的Rubymotion構建iOS應用程序。 我得到了一個表格,我想填充來自遠程API的數據。無法從模型到控制器獲取Bubblewrap數據
我命名控制器:ProjectsController
我命名型號:項目
在控制器的viewDidLoad我想從API項目的清單。 我在Project模型中創建了一個名爲load_projects的靜態方法。
def self.load_projects
BW::HTTP.get("#{URL}projects?id=25&project_id=24&access_token=#{TOKEN}") do |response|
result_data = BW::JSON.parse(response.body)
output = result_data["projects"]
output
end
end
這是我在控制器viewDidLoad中:
def viewDidLoad
super
@projects = Project.load_projects
self.navigationItem.title = "Projekt"
end
我在模型做我不明白在viewDidLoad中同樣的反應。在模型方法中,我得到正確的響應數據,但在viewDidLoad中,我得到一個「元」對象返回。 BubbleWrap :: HTTP :: Query對象。我究竟做錯了什麼?
更新一個
我試着用下面的第一個答案是這樣,但我得到一個錯誤:
def tableView(tableView, cellForRowAtIndexPath:indexPath)
cellIdentifier = self.class.name
cell = tableView.dequeueReusableCellWithIdentifier(cellIdentifier) || begin
cell = UITableViewCell.alloc.initWithStyle(UITableViewCellStyleDefault, reuseIdentifier:cellIdentifier)
cell
end
project = @projects[indexPath.row]['project']['title']
cell.textLabel.text = project
cell
end
的錯誤是:
projects_controller.rb:32:in `tableView:cellForRowAtIndexPath:': undefined method `[]' for nil:NilClass (NoMethodError)
2012-11-09 01:08:16.913 companyapp[44165:f803] *** Terminating app due to uncaught exception 'NoMethodError', reason: 'projects_controller.rb:32:in `tableView:cellForRowAtIndexPath:': undefined method `[]' for nil:NilClass (NoMethodError)
我可以顯示這裏返回的數據沒有錯誤:
def load_data(data)
@projects ||= data
p @projects[0]['project']['title']
end
謝謝!我正在路上,但不幸的是我無法使用返回的數據。請參閱上面我更新的問題。 –
這已經超出了這個特定問題的範圍,但我建議你調試@person以確保它是你期望的正確對象,因爲錯誤消息說它是一個零對象。 – n00shie
毫米,不確定你的意思?什麼是@person? load_data方法的要點是能夠在控制器的其餘部分使用@projects,對吧? –