2015-12-24 21 views
0

我有一个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!)) 
    } 
} 

所以,问题是,当我滚动,及时挖掘第二部分,应用程序崩溃:

enter image description here

因为self.news = []self.news.appendgetJson之间,在这里去cellForRowAtIndexPath

我的问题是如何当我点击段时停止cellForRowAtIndexPath?谢谢。

+0

你在'numberOfRowsInSection'中返回什么?你在切换段并开始加载数据时调用'reloadData'吗? – Paulw11

+0

我返回'news.count'。我打电话。问题是'tableView'不断填充单元格,但'getjson'确实'self.news = []' – aaisataev

+1

通常情况下,您会将数据加载到临时数组中,然后在加载数据后将数据复制到'self.news'中,然后调用reloadData – Paulw11

回答

2

如果你的代码是从网络加载数据与驱动你的tableview的数组搞混了,那么你将遇到问题......而你是。

典型的方法是将数据加载到网络加载代码中的临时数组中,然后在加载完成时将此数据分配给实际数组(在您的情况下为self.news),然后在您的tableview上调用reloadData

您可能还想在数据加载时考虑用户体验。我可能会做这样的事情:

  1. 当段改变,隐藏的tableview和取消是在tableview中的位置为中心
  2. 开始装载从网络到一个临时数组
  3. 当一个UIActivityIndi​​catorView加载完成其分配给self.news
  4. 重新加载表数据
  5. 隐藏UIActivityView和取消隐藏的tableview
相关问题