我的tableview不会重新加载数据,它只显示noob
但不会显示更新的数据。
我是否遗漏了代码中的任何步骤?
class ReservoirsScreen < PM::TableScreen
title "I want to refresh"
refreshable
def on_load
@returned_data ||= [{title:'noob'}]
on_refresh
end
def table_data
[{
cells: @returned_data.map do |reservoir|
{
title: reservoir["title"],
action: :select_reservoir,
arguments: { reservoir: reservoir["title"] }
}
end
}]
end
def select_reservoir(reservoir)
PM.logger.info reservoir
end
def on_refresh
start_refreshing
get_reserviors
stop_refreshing
update_table_data
end
def get_reserviors
start_refreshing
url_string = "http://128.199.223.114:10080/"
AFMotion::JSON.get(url_string) do |result|
if result.success?
@returned_data = []
result.object["data"].each do |r|
@returned_data << {
title: [r["reservoirName"], r["immediatePercentage"]].join(":")
}
end
else
@returned_data = ['failed','d']
end
end
stop_refreshing
update_table_data
end
end
答案 0 :(得分:1)
AFMotion使用异步回调,因此您的代码实际上是这样执行的:
相反,将stop_refreshing
和update_table_data
行移动到回调中(if result.success?
/ else
块),这样在JSON响应之前它们不会被执行收到:
if result.success?
@returned_data = []
result.object["data"].each do |r|
@returned_data << {
title: [r["reservoirName"], r["immediatePercentage"]].join(":")
}
end
update_table_data
stop_refreshing
else
@returned_data = ['failed','d']
update_table_data
stop_refreshing
end