由于性能原因,我在我的应用程序中实现了聊天,限制为 100 条消息,突然我发现,行编辑并不像我预期的那样容易。 所以我尝试了这个:
public void RemoveTopRow()
{
messages.RemoveAt (0);
tableView.ReloadData ();
}
但它因 native 异常而崩溃,我没有在这里停下来尝试这个:
tableView.DeleteRows (new NSIndexPath[]{ NSIndexPath.FromItemSection(0,0) }, UITableViewRowAnimation.Fade);
也许有人知道该怎么做?小的工作代码示例会很有帮助,我保证,在我的下一个假期,我会做一些重构并在 github 上发布这个聊天客户端,这样其他人就可以在他们的项目中实现它而不会出现 UI 问题,因为我已经在这些布局修复上花费了太多时间和精力
最佳答案
没有看到你在哪里调用你的 RemoveTopRow
方法,我只能猜测,但根据错误,我假设你正在添加一个新的聊天行并在 TableView /数据源完成同步之前从 FIFO 堆栈中删除第一个:
public void RemoveTopRow()
{
dataSource.Objects.RemoveAt (0);
TableView.ReloadData ();
}
void AddNewItem (object sender, EventArgs args)
{
dataSource.Objects.Add (DateTime.Now);
// This will cause the error that you are seeing when
// it reaches the TableView.InsertRows...
//if (dataSource.Objects.Count > 10) {
// RemoveTopRow ();
//}
using (var indexPath = NSIndexPath.FromRowSection (0, 0))
TableView.InsertRows (new [] { indexPath }, UITableViewRowAnimation.Automatic);
//This will work
if (dataSource.Objects.Count > 10) {
RemoveTopRow ();
}
}
关于c# - Xamarin iOS UITableView,如何删除第一行?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33524587/