我有一个segmentedControl
和一个tableView
下面。当点击段时停止cellForRowAtIndexPath
SegmentedControl
有两个部分:'新闻'和'事件'。
新闻和活动下载到var news = [News]()
在func getJson(type: Int)
。
新闻 - news.type = 1
,事件news.type = 2
。
我getJson
方法是:
func getJson(type: Int) {
self.news = [] // empty news array
if jsonData["type"] == type {
// save needed news to array
self.news.append(News(id: jid, title: jtitle, subtitle: jsubtitle, urlstring: jurlstring, image: jimage!))
}
}
所以,问题是,当我滚动,及时挖掘第二部分,应用程序崩溃:
因为self.news = []
和self.news.append
在getJson
之间,在这里去cellForRowAtIndexPath
。
我的问题是如何当我点击段时停止cellForRowAtIndexPath
?谢谢。
你在'numberOfRowsInSection'中返回什么?你在切换段并开始加载数据时调用'reloadData'吗? – Paulw11
我返回'news.count'。我打电话。问题是'tableView'不断填充单元格,但'getjson'确实'self.news = []' – aaisataev
通常情况下,您会将数据加载到临时数组中,然后在加载数据后将数据复制到'self.news'中,然后调用reloadData – Paulw11