gpt4 book ai didi

ios - 在 GetCell 方法中删除动态单元格

转载 作者:行者123 更新时间:2023-11-29 03:35:53 25 4
gpt4 key购买 nike

如果单元格的图像是错误图像,我正在尝试从 UITableView 中删除该单元格。基本上我的代码对每个单元格的图像执行 threadPool 调用,以使用户滚动时绑定(bind)数据的流程像在 GetCell 方法中一样平滑:

public override UITableViewCell GetCell (UITableView tableView, MonoTouch.Foundation.NSIndexPath indexPath)
{
//other stuff
ThreadPool.QueueUserWorkItem(new WaitCallback(RetrieveImage),(object)cell);
}

在该异步方法中,我调用 cell.imageUrl 属性并将其绑定(bind)到 NSData 对象:

NSData data = NSData.FromUrl(nsUrl); //nsUrl is cell.imageUrl

从那里我知道如果 data==null 那么检索图像时就会出现问题,所以我想删除它。我目前所做的是将隐藏属性设置为 true,但这会留下一个空白区域。我想删除整个单元格,这样用户甚至都不知道它存在。我无法将单元格高度设置为 0,因为在 indexrowpath 启动 getcell 调用之前我不知道 imageUrl 是否错误。我不想只检查绑定(bind)到 UITableView 的数据中的所有图像,因为这将对性能造成很大影响,因为它很容易包含一百个项目。我目前正在从提供前 200 个项目的网络服务中获取项目。如何完全删除单元格 if the data==null example

NSUrl nsUrl = new NSUrl(cell.imageUrl);
NSData data = NSData.FromUrl(nsUrl);
if (data != null) {
InvokeOnMainThread (() => {
cell.imgImage = new UIImage (data);
//cell.imgImage.SizeToFit();
});
} else {
//I tried the below line but it does't work, I have a cell.index property that I set in the getCell method
_data.RemoveAt(cell.Index);//_data is the list of items that are binded to the uitableview
InvokeOnMainThread (() => {
//cell.Hidden = true;
});
}

最佳答案

您可以通过从源中删除单元格来删除单元格,然后通过调用 ReloadData() 让 UITableView 知道源已更改。 这将触发刷新过程。 UITableView 将询问您的源有多少行,并且因为您从数据中删除了一行,所以表示该行的单元格将消失。但是 ReloadData() 将重新加载整个表。有一种方法允许您专门删除单元格,名为 DeleteRows()。 您可以在 Xamarin 找到示例。 .

删除行时,请务必先更新模型,然后再更新 UI。

或者,我建议您查看 MonoTouch.Dialog这允许以更直接的方式与 UITableView 交互。它还包括对延迟加载图像的支持。

关于ios - 在 GetCell 方法中删除动态单元格,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19191995/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com