UITableView 中的 insertRowsAtIndexPaths 添加重复行
insertRowsAtIndexPaths in UITableView adding Duplicate Rows
我正在使用 insertRowsAtIndexPaths
方法向我的 UITableView
添加新行。存在添加重复行的问题。
例如,如果我键入 "HI"
,然后触摸 return,则会将包含文本 "HI"
的行添加到 table。如果我然后返回并键入 "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()
}
非常感谢!
Edit: Here's the cellForRowAtIndexPath
code
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell:GroupChatMessageCell = tableView.dequeueReusableCellWithIdentifier("cell", forIndexPath: indexPath) as! GroupChatMessageCell
cell.message.text = messages[indexPath.row].text
cell.contentView.transform = CGAffineTransformMakeScale (1,-1);
return cell
}
您似乎将新消息附加到数组的末尾,但在第一行插入了新行。所以它要求 func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell
中的第一个数组元素
可以通过将 self.messages.append(message)
更改为 self.messages.insert(message, atIndex: 0)
来修复
实施 estimatedHeightForRowAtIndexPath 来解决问题![=11=]
-(CGFloat)tableView:(UITableView *)tableView estimatedHeightForRowAtIndexPath:(NSIndexPath *)indexPath {
return [self tableView:tableView heightForRowAtIndexPath:indexPath];
}
尽情享受 ;)
我正在使用 insertRowsAtIndexPaths
方法向我的 UITableView
添加新行。存在添加重复行的问题。
例如,如果我键入 "HI"
,然后触摸 return,则会将包含文本 "HI"
的行添加到 table。如果我然后返回并键入 "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()
}
非常感谢!
Edit: Here's the
cellForRowAtIndexPath
code
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell:GroupChatMessageCell = tableView.dequeueReusableCellWithIdentifier("cell", forIndexPath: indexPath) as! GroupChatMessageCell
cell.message.text = messages[indexPath.row].text
cell.contentView.transform = CGAffineTransformMakeScale (1,-1);
return cell
}
您似乎将新消息附加到数组的末尾,但在第一行插入了新行。所以它要求 func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell
可以通过将 self.messages.append(message)
更改为 self.messages.insert(message, atIndex: 0)
实施 estimatedHeightForRowAtIndexPath 来解决问题![=11=]
-(CGFloat)tableView:(UITableView *)tableView estimatedHeightForRowAtIndexPath:(NSIndexPath *)indexPath {
return [self tableView:tableView heightForRowAtIndexPath:indexPath];
}
尽情享受 ;)