Ios UITableView中的InsertRowsatindExpath添加重复行
我正在使用Ios UITableView中的InsertRowsatindExpath添加重复行,ios,swift,uitableview,uiview,swift2,Ios,Swift,Uitableview,Uiview,Swift2,我正在使用insertRowsAtIndexPaths方法向我的UITableView添加新行。存在添加重复行的问题 例如,如果我键入“HI”,然后触摸return,则表中会添加一行“HI”。如果我返回并键入“Hello”,然后按return,则会再次添加一行,该行显示“HI”。下面是一些代码: func textFieldShouldReturn(textField: UITextField) -> Bool { sendMessage(textField.text)
insertRowsAtIndexPaths
方法向我的UITableView
添加新行。存在添加重复行的问题
例如,如果我键入“HI”
,然后触摸return,则表中会添加一行“HI”
。如果我返回并键入“Hello”
,然后按return,则会再次添加一行,该行显示“HI”
。下面是一些代码:
func textFieldShouldReturn(textField: UITextField) -> Bool {
sendMessage(textField.text)
return true
}
func sendMessage(text:String){
var message = Message(text: text)
//global array that stores all the messages
self.messages.append(message)
let indexPathZero : NSIndexPath = NSIndexPath(forRow: 0, inSection: 0)
self.tableView.beginUpdates()
self.tableView.insertRowsAtIndexPaths(NSArray(object: indexPathZero) as [AnyObject], withRowAnimation: UITableViewRowAnimation.None)
self.tableView.endUpdates()
}
非常感谢
编辑:以下是单元格行索引路径
代码
似乎您正在将新消息追加到数组的末尾,但在第一行插入新行。因此,它要求在
func tableView(tableView:UITableView,cellForRowAtIndexPath:nsindepath)->UITableViewCell
可以通过更改
self.messages.append(message)
到self.messages.insert(message,atIndex:0)
实现estimatedHeightForRowAtIndexPath来修复此问题。请完成此操作
-(CGFloat)tableView:(UITableView *)tableView estimatedHeightForRowAtIndexPath:(NSIndexPath *)indexPath {
return [self tableView:tableView heightForRowAtIndexPath:indexPath];
}
享受;)
tableView:cellforrowatinexpath:
?@Larme查看我的编辑:)的代码是什么?如果您打印fmessage.text
(在sendMessage:
中),您有“您好”吗?您是否也可以给出numberofrowInstition:
和numberOfSection:
的代码,只是为了确保这一点?顺便说一句,您可以将let cell:GroupChatMessageCell
替换为let cell
,因为您显式地强制转换了类型。我与上面的问题相同,而且我已经有了消息。insert。你能看看这个问题,给我你的意见吗。
-(CGFloat)tableView:(UITableView *)tableView estimatedHeightForRowAtIndexPath:(NSIndexPath *)indexPath {
return [self tableView:tableView heightForRowAtIndexPath:indexPath];
}