使用Xamarin进行分页

时间:2014-05-02 19:35:03

标签: c# ios uitableview xamarin.ios xamarin

当用户使用UITableViewXamarin拉到屏幕底部时,我正在尝试加载旧的Instagram图片。但是我似乎不知道该怎么做。我在Objective-C中运行良好,它在C#中看起来有点不同。以下是我收到初始帖子的方式:

public void getInstagramFeed(UITableView table){

        IEnumerable<Account> accounts = AccountStore.Create ().FindAccountsForService ("Instagram");
        var enumerable = accounts as IList<Account> ?? accounts.ToList ();
        if (enumerable.Any ()) {
            Account instagram = enumerable.First ();
            var instagramAccessToken = instagram.Properties ["access_token"].ToString ();

            var request = new RestRequest { RootElement = "data", Resource = "/users/self/feed" };
            request.AddParameter ("access_token", instagramAccessToken);

            var client = new RestClient ("https://api.instagram.com/v1");

            client.ExecuteAsync (request, response => {
                table.InvokeOnMainThread (() => {
                RootObject rootObject = JsonConvert.DeserializeObject<RootObject> (response.Content);
                var dataSource = new ObservableDataSource<Datum> (rootObject.data);
                dataSource.Bind (table);
            });
        }); 
    }
}

以下是我datasource的绑定方法:

public void Bind(Datum datum)
        {
            this.datum = datum;
            if (this.datum == null || this.datum.caption == null)
            {
                this.captionLabel.Text = "";
            }
            else
            {
                this.captionLabel.Text = datum.caption.text;

            }

            this.pictureImage.InvokeOnMainThread (() => this.pictureImage.SetImage (
                url: new NSUrl (datum.images.standard_resolution.url)
                    )
                );

                this.profileImage.InvokeOnMainThread (() => this.profileImage.SetImage (
                    url: new NSUrl (datum.user.profile_picture)
                    )
                );
            this.nameLabel.Text = this.datum.user == null ? "user is null" : datum.user.full_name;
        }

以下是我tableView的课程:

    public TableView ()
    {
    }

    public TableView (IntPtr handle) : base(handle)
    {
    }

    public UITableViewCell GetCell (Datum item)
    {
        var newCell = this.DequeueReusableCell(InstagramCell.Key) 
            as InstagramCell ?? InstagramCell.Create();

        newCell.Bind (item);

        return newCell;
    }

    public float GetHeightForRow (NSIndexPath indexPath)
    {
        return 340f;
    }

感谢您提前获得的所有帮助,对不起,我很抱歉:)

// PS这就是我在OBJc中的表现:

NSDictionary *page = instagramResponse[@"pagination"];
NSString *nextPage = page[@"next_url"];

[[InstagramClient sharedClient] getPath:[NSString stringWithFormat:@"%@",nextPage] parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {
    instagramResponse = [responseObject mutableCopy];
    [instagramResponse addEntriesFromDictionary:responseObject];
    [instapics addObjectsFromArray:responseObject[@"data"]];
    [self updateArrays];
    [self.tableView reloadData];

} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
    NSLog(@"Failure: %@", error);


}];

// InstagramClient的类:

public class RootObject
    {
        public Pagination pagination { get; set; }
        public Meta meta { get; set; }
        public List<Datum> data { get; set; }
    }

public class Datum
{
    public object attribution { get; set; }
    public List<string> tags { get; set; }
    public string type { get; set; }
    public object location { get; set; }
    public Comments comments { get; set; }
    public string filter { get; set; }
    public string created_time { get; set; }
    public string link { get; set; }
    public Likes likes { get; set; }
    public Images images { get; set; }
    public List<object> users_in_photo { get; set; }
    public Caption caption { get; set; }
    public bool user_has_liked { get; set; }
    public string id { get; set; }
    public User user { get; set; }
    public Videos videos { get; set; }

    public override string ToString()
    {
        if (user == null)
        {
            return "User is null";
        }
        return user.full_name;
    }
}

public class Videos
{
    public LowResolution2 low_resolution { get; set; }
    public StandardResolution2 standard_resolution { get; set; }
}

public class StandardResolution2
{
    public string url { get; set; }
    public int width { get; set; }
    public int height { get; set; }
}

public class LowResolution2
{
    public string url { get; set; }
    public int width { get; set; }
    public int height { get; set; }
}

public class User
{
    public string username { get; set; }
    public string website { get; set; }
    public string profile_picture { get; set; }
    public string full_name { get; set; }
    public string bio { get; set; }
    public string id { get; set; }
}

public class Caption
{
    public string created_time { get; set; }
    public string text { get; set; }
    public From from { get; set; }
    public string id { get; set; }
}

public class From
{
    public string username { get; set; }
    public string profile_picture { get; set; }
    public string id { get; set; }
    public string full_name { get; set; }
}

public class Images
{
    public LowResolution low_resolution { get; set; }
    public Thumbnail thumbnail { get; set; }
    public StandardResolution standard_resolution { get; set; }
}

public class StandardResolution
{
    public string url { get; set; }
    public int width { get; set; }
    public int height { get; set; }
}

public class Thumbnail
{
    public string url { get; set; }
    public int width { get; set; }
    public int height { get; set; }
}

public class LowResolution
{
    public string url { get; set; }
    public int width { get; set; }
    public int height { get; set; }
}

public class Likes
{
    public int count { get; set; }
    public List<Datum2> data { get; set; }
}

public class Datum2
{
    public string username { get; set; }
    public string profile_picture { get; set; }
    public string id { get; set; }
    public string full_name { get; set; }
}

public class Comments
{
    public int count { get; set; }
    public List<object> data { get; set; }
}

public class Meta
{
    public int code { get; set; }
}

public class Pagination
{
    public string next_url { get; set; }
    public string next_max_id { get; set; }
}

1 个答案:

答案 0 :(得分:0)

如果可能,我建议使用UiCollectionViewController。或者现在最好是Xamarin.Forms中的等价物。使用UiCollectionViewController,您可以在用户向下滚动时轻松加载更多项目