0

「reloadData」をUITableViewで動作させるのに少し問題があります。要するに; テーブルにデータ/行を追加すると、行は追加されますが、既存の行の複製として表示されます。「DequeueReusableCell」をデバッグすると、ランダムなセルが返されるようです。これが私のコードです。(はい、C# です - モノタッチです。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 に追加します。次に、呼び出した後、「新しい」セルであっても、重複reloadDataGetCellDequeueReusableCell返されます...

問題の一部ではないものはすべて削除したので、十分に真実です。私のgetMyStuffメソッドにはエラー処理がなく、キャッシュが配置されているようにも見えません...それ以外の場合は、ベストプラクティスのコメントを受け入れます.

信じられないほど単純なものだと確信していますが、頭を包むことはできません。ここStackOverflowで他の同様の質問を見てきましたが、明確な答えを提供するものはありません...

4

1 に答える 1