2016-01-20 94 views
0

我有關於tableView的問題。我正在使用聊天模塊快速開展一個項目。我從Web服務獲取數據。當視圖加載時,我的聊天室第一次顯示完美。但是當我發送消息或滾動tableView時,數值會改變或丟失。任何人都可以幫助我請這個。我在這個問題上陷入了很長時間。我有關於tableView的問題。滾動值的變化

+0

你可以發佈你正在使用的代碼創建一個新的消息? –

回答

1

我有關於tableView的答案。 scnr

你需要跟蹤你的數據在你的tableView的dataSource以及tableView本身的變化。

編輯: 假設你的聊天數據是一個字符串數組,按日期排序,在上面最新消息,在插入數據時,你應該

tableView?.beginUpdates() // signal the tableview a pending update 
data.insert(newMessage, atIndex:0) // modify your dataSource's data 
tableView?.insertRowsAtIndexPaths([NSIndexPath(0, inSection: 0)], withRowAnimation: .Automatic) // insert the new row in the tableView 
tableView?.endUpdates() // commit changes 
+0

你能解釋一下嗎 –

+0

我的確編輯了我的答案,包括有關在問題和某些快捷方式中缺少的代碼的推測。 – SmokeDispenser

0

您的數據源應與之配合tableView索引.ie應該有一個規則來找出應該顯示給出的消息和tableView的IndexPath。最簡單的規則是假定你的tableView只包含1個部分,你的dataSource中的indexOf消息應該等於IndexPath.Row。例如,您要發送應添加到您的tableView

dataSource.append(newMessage) 
tableView.insertRowsAtIndexPaths([NSIndexPath(0, inSection: 0)], withRowAnimation: .Automatic) 

,並在你的cellForRowAtIndexPath委託方法的底部

let message = dataSource[indexPath.row] 
let cell = tableView.dequeueReusableCellWithIdentifier("MessageCell", forIndexPath: indexPath) 
// then use the message to populate the cell then return cell 

return cell