gpt4 book ai didi

c# - UITableView reloadData 重复单元格

转载 作者:行者123 更新时间:2023-11-28 20:12:12 24 4
gpt4 key购买 nike

让“reloadData”与 UITableView 一起工作时遇到一些麻烦。简而言之;如果我向表中添加数据/行,行将添加,但显示为现有行的副本。如果我调试“DequeueReusableCell”,它似乎只是返回一个随机单元格。这是我的代码。 (是的,它是 C# - monotouch,如果你知道 Objective-C 中的答案,只需发布​​它,我很灵活)

public partial class MyViewController : UITableViewController
{
AppDelegate AppDelegate;
MyStuff sendToDetail;
MyRestClient client;

public MyViewController (IntPtr handle) : base (handle)
{
AppDelegate = (AppDelegate)UIApplication.SharedApplication.Delegate;
client = new MyRestClient();
}

public override void ViewDidLoad ()
{
base.ViewDidLoad ();

this.TableView.Source = new TableSource(getMyStuff (), this);
}

public override void PrepareForSegue (UIStoryboardSegue segue, NSObject sender)
{
base.PrepareForSegue (segue, sender);

if (segue.Identifier == "DetailSegue") {
DetailController controller = segue.DestinationViewController as DetailController;
controller.MyStuffItem = sendToDetail;
}
}

private IEnumerable<MyStuff> getMyStuff(int skip = 0, int count = 10)
{
var request = new RestRequest("MyStuff");
request.AddParameter("id", AppDelegate.ActiveUser.Id);
request.AddParameter("$top", count);
request.AddParameter("$skip", skip);
var response = client.Execute(request);

var source = (IEnumerable<MyStuff>)JsonConvert.DeserializeObject(response.Content, typeof(IEnumerable<MyStuff>));

return source;
}

public class TableSource : UITableViewSource {
IEnumerable<MyStuff> tableItems;
MyViewController Controller;

public TableSource (IEnumerable<MyStuff> items, MyViewController controller)
{
tableItems = items;
Controller = controller;
}

public override int NumberOfSections (UITableView tableView)
{
return 1;
}

public override int RowsInSection (UITableView tableview, int section)
{
return tableItems.Count() + 1;
}

public override UITableViewCell GetCell (UITableView tableView, MonoTouch.Foundation.NSIndexPath indexPath)
{

if (indexPath.Row == tableItems.Count()) {
//bottom load more cell

LoadingCell loadMore = tableView.DequeueReusableCell ("LoadingCell") as LoadingCell;
if (loadMore == null)
loadMore = LoadingCell.Create ();

return loadMore;
}

MyStuffCell cell = tableView.DequeueReusableCell ("MyStuffCell") as MyStuffCell;
// if there are no cells to reuse, create a new one
if (cell == null) {
cell = MyStuffCell.Create ();

MyStuff current = tableItems.ElementAt (indexPath.Row);

cell.Update (current);
}

return cell;
}

public override void RowSelected (UITableView tableView, NSIndexPath indexPath)
{
if (indexPath.Row != tableItems.Count()) {
Controller.sendToDetail = tableItems.ElementAt (indexPath.Row);
Controller.PerformSegue ("DetailSegue", this);
}
}

public override void WillDisplay (UITableView tableView, UITableViewCell cell, NSIndexPath indexPath)
{
if (cell is LoadingCell) {
var count = tableItems.Count ();
var second = Controller.getMyStuff (count);
tableItems = tableItems.Concat (second);
tableView.ReloadData ();
}
}
}
}

魔法发生在 WillDisplay 中。在这里,我检查它是否会显示一个 LoadingCell(底部带有加载指示器的那个)并将相应地加载额外数据并将其添加到我的 IEnumerable 中。然后在调用 reloadData 之后,它将进入 GetCell 并且 DequeueReusableCell 返回一个副本...即使是"new"单元格...

我删除了所有不属于问题的部分,这很真实:我的 getMyStuff 方法中没有错误处理,看起来我也没有任何缓存......否则我愿意接受最佳实践评论。

我确信它非常简单,但我就是无法理解它。我在 StackOverflow 上看到过其他类似的问题,但没有一个提供明确的答案...

最佳答案

DequeueReusableCell 只是从它的内部缓存中返回一个分配的 Cell 对象。您仍然需要更新单元格的内容以反射(reflect)适合该部分/行的内容。

        MyStuffCell cell = tableView.DequeueReusableCell ("MyStuffCell") as MyStuffCell;
// if there are no cells to reuse, create a new one
if (cell == null) {
cell = MyStuffCell.Create ();
}

MyStuff current = tableItems.ElementAt (indexPath.Row);
cell.Update (current);

return cell;

关于c# - UITableView reloadData 重复单元格,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19776062/

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