2016-01-20 96 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