Xamarin C#在一个UITableView中有两个UITableViewCells

时间:2014-08-24 15:39:21

标签: c# ios uitableview xamarin

我正在尝试将两个不同的UITableViewCell添加到一个UITableView但是当我尝试将数据加载到TableSource时它只允许我发送一个List<> 1}}一次。但是我需要两个List<>来显示我的单元格。我的TableSource.cs类中的两个列表是名为instaData和faceData的公共变量。当我运行个人请求获取instaData和faceData时,它可以完美地运行。然后更好地展示了解释:

申请InstaData

var client = new RestClient ("https://api.instagram.com/v1");
    client.ExecuteAsync (request, response => {
        var rootObject = JsonConvert.DeserializeObject<RootObject> (response.Content);
        InstagramObject.next_url = rootObject.pagination.next_url.ToString();
        FLDTRequest.instagramDataCopy = rootObject.data;
        table.InvokeOnMainThread (() => {
        table.Source = new TableSource(stream);
        ((TableSource)table.Source).instaData = rootObject.data;
        table.ReloadData ();
        });
    });

申请FaceData

var client = new RestClient ("https://graph.facebook.com/");
                client.ExecuteAsync (request, response => {
                    var rootObject = JsonConvert.DeserializeObject<Floadt.Core.Facebook.RootObject> (response.Content);
                    FLDTRequest.facebookDataCopy = rootObject.data;
                    table.InvokeOnMainThread (() => {
                        table.Source = new TableSource(stream);
                        ((TableSource)table.Source).faceData = rootObject.data;
                        table.ReloadData ();
                    });
                });

基本上,当我想要获取两个数据时,我会调用这两个方法,但是我通常会从上一次调用的方法中得到一个错误,说该Object不是引用。例如:如果我最后调用faceData请求,则会说Object不是引用。这是我的TableSource班级:

public class TableSource : UITableViewSource 
{
    StreamViewController controller;

    public List<Datum> instaData { get; set; }
    public List<string> twitData { get; set; }
    public List<Floadt.Core.Facebook.Datum> faceData { get; set; }

    public TableSource(StreamViewController stream)
    {
        controller = stream;
    }

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

    public override float GetHeightForRow(UITableView tableView, NSIndexPath indexPath)
    {
        if (indexPath.Row % 2 == 0) {
            return 340;
        } else {
            return 436;
            //return 210;
        }
    }

    public override UITableViewCell GetCell (UITableView tableView, MonoTouch.Foundation.NSIndexPath indexPath)
    {
        if (indexPath.Row % 2 == 0) {
            NetworkCheck netCheck = new NetworkCheck ();
            netCheck.runCheck ();
            // bool log = netCheck.anyLoggedIn ();

            if (tableView.ContentSize.Height - UIScreen.MainScreen.Bounds.Height <= tableView.ContentOffset.Y) {
                BTProgressHUD.Show ("Getting more...");
                FLDTRequest.getInstagramNextPage (tableView);
                BTProgressHUD.Dismiss ();
            }

            var cell = tableView.DequeueReusableCell (InstagramCell.Key) as InstagramCell;

            if (cell == null) {
                cell = new InstagramCell ();
                var views = NSBundle.MainBundle.LoadNib ("InstagramCell", cell, null);
                cell = Runtime.GetNSObject (views.ValueAt (0)) as InstagramCell;
            }

            //Datum h = instaData [indexPath.Row/2];
            //cell.BindData (h);

            return cell;

        } else {
            NetworkCheck netCheck = new NetworkCheck ();
            netCheck.runCheck ();
            // bool log = netCheck.anyLoggedIn ();

            if (tableView.ContentSize.Height - UIScreen.MainScreen.Bounds.Height <= tableView.ContentOffset.Y) {
                BTProgressHUD.Show ("Getting more...");
                FLDTRequest.getInstagramNextPage (tableView);
                BTProgressHUD.Dismiss ();
            }


            var cell = tableView.DequeueReusableCell (FacebookCell.Key) as FacebookCell;

            if (cell == null) {
                cell = new FacebookCell ();
                var views = NSBundle.MainBundle.LoadNib ("FacebookCell", cell, null);
                cell = Runtime.GetNSObject (views.ValueAt (0)) as FacebookCell;
            }

            var fbPhotoCell = tableView.DequeueReusableCell (FacebookPhotoCell.Key) as FacebookPhotoCell;

            if (fbPhotoCell == null) {
                fbPhotoCell = new FacebookPhotoCell ();
                var views = NSBundle.MainBundle.LoadNib ("FacebookPhotoCell", cell, null);
                fbPhotoCell = Runtime.GetNSObject (views.ValueAt (0)) as FacebookPhotoCell;
            }

            Floadt.Core.Facebook.Datum f = faceData [indexPath.Row/2];

            fbPhotoCell.BindData (f);
            return fbPhotoCell;
        }   
    }
}

2 个答案:

答案 0 :(得分:1)

看起来您要设置table.Source两次,每次List一次?您需要将列表合并到一个数据源中,并创建一个可以显示两种数据类型的UITableViewCell

答案 1 :(得分:0)

1)将UITableView拖放到ViewController并添加约束。

2)添加两个Prototype单元格并与两个UITableViewCells相关联。

3)设置UITableViewSource(组合UITableVIewDataSource和UITableViewDelegate)。

    public override void ViewDidLoad ()
            {
                base.ViewDidLoad ();
                // Perform any additional setup after loading the view, typically from a nib.
                this.sampleTableView.Source = new SampleTableViewSource ();     
            }

将Source类添加到ViewController。

//Table Source
        public class SampleTableViewSource : UITableViewSource
        {
            string CellIdentifier = "sampleTableViewCellID";
            string CellIdentifier2 = "sampleTableViewCell2ID";

            public override nint RowsInSection(UITableView tableview, nint section)
            {
                return 2;
            }
            public override nint NumberOfSections (UITableView tableView)
            {
                return 1;
            }
            public override UITableViewCell GetCell(UITableView tableView, NSIndexPath indexPath)
            {
                UITableViewCell cell = new UITableViewCell ();

                //---- if there are no cells to reuse, create a new one

                if (indexPath.Row == 0)
                    {
                    cell = tableView.DequeueReusableCell (CellIdentifier) as SampleTableViewCell;               
                    }
                    else if(indexPath.Row == 1 ) {
                    cell = tableView.DequeueReusableCell (CellIdentifier2) as sampleTableViewCell2;
                    }

                return cell;
            }
            public override void RowSelected (UITableView tableView, NSIndexPath indexPath)
            {
                tableView.DeselectRow (indexPath, true);
        } 
        }