两个uitableviewcell在一个UITableView
本文关键字:一个 UITableView uitableviewcell 两个 | 更新日期: 2023-09-27 18:19:19
我试图将两个不同的UITableViewCell
's放入一个UITableView
中,但是当我尝试将数据加载到TableSource
中时,它只允许我一次发送一个List<>
。然而,我需要两个List<>
's为我的两个单元格显示。我的TableSource.cs
类中的两个list是名为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 ();
});
});
基本上,当我想获得两个数据时,我调用两个方法,但我通常从我最后调用的方法中得到一个错误,说那个对象不是引用。例如:如果我最后调用faceData请求,它会说对象不是引用。这是我的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;
}
}
}
看起来您正在尝试设置table.Source
两次,一次为每个List
?您需要将您的列表合并到单个数据源中,并创建一个可以可视化这两种数据类型的UITableViewCell
。
1)拖放UITableView到ViewController并添加约束。
2)添加两个原型单元格并关联两个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 ();
}
为ViewController添加Source类
//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);
}
}