Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/swift/16.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios UITableView中的InsertRowsatindExpath添加重复行_Ios_Swift_Uitableview_Uiview_Swift2 - Fatal编程技术网

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查看我的编辑:)的代码是什么?如果您打印f
message.text
(在
sendMessage:
中),您有“您好”吗?您是否也可以给出
numberofrowInstition:
numberOfSection:
的代码,只是为了确保这一点?顺便说一句,您可以将
let cell:GroupChatMessageCell
替换为
let cell
,因为您显式地强制转换了类型。我与上面的问题相同,而且我已经有了消息。insert。你能看看这个问题,给我你的意见吗。
-(CGFloat)tableView:(UITableView *)tableView estimatedHeightForRowAtIndexPath:(NSIndexPath *)indexPath {
return [self tableView:tableView heightForRowAtIndexPath:indexPath];
}