在Xamarin.iOS中的UITableView末尾添加自定义页脚
我想在在Xamarin.iOS中的UITableView末尾添加自定义页脚,uitableview,xamarin,xamarin.ios,table-footer,Uitableview,Xamarin,Xamarin.ios,Table Footer,我想在UITableView中添加一个页脚视图,当用户到达列表末尾并将加载新数据时,该视图会显示一个uiprogressindicator,或者当没有更多数据要提取时,会显示一个UILabel 我使用了下面的代码,但什么也没发生: UITableViewHeaderFooterView footer = new UITableViewHeaderFooterView (); UILabel futerlabel = new UILabel (); futerlabel.Text = "Duke
UITableView
中添加一个页脚视图,当用户到达列表末尾并将加载新数据时,该视图会显示一个uiprogressindicator
,或者当没有更多数据要提取时,会显示一个UILabel
我使用了下面的代码,但什么也没发生:
UITableViewHeaderFooterView footer = new UITableViewHeaderFooterView ();
UILabel futerlabel = new UILabel ();
futerlabel.Text = "Duke u ngarkuar";
footer.AddSubview (futerlabel);
任何方法都可以实现这一点。如果您使用的是
UITableViewController
,请尝试以下方法:
TableView.TableFooterView = footer;
更新
仔细考虑后,我建议不要使用页脚,而是在数据末尾添加一个额外的单元格,这样可以得到这个结果
使用此方法检查是否已滚动到最后一项并更新表的数据:
public override void WillDisplay (UITableView tableView, UITableViewCell cell, NSIndexPath indexPath)
{
// if showing last row of last section, load more
if (indexPath.Section == tableView.NumberOfSections()-1 && indexPath.Row == tableView.DataSource.RowsInSection(tableView, indexPath.Section)-1 && !FullyLoaded) {
var growRowSource = tableView.DataSource as GrowRowTableDataSource;
if (growRowSource != null) {
FullyLoaded = growRowSource.LoadNextPage ();
Task.Delay (5000);
tableView.ReloadData ();
}
}
}
然后在委托中检查最后一项并创建不同的单元格,如下所示:
public override UITableViewCell GetCell (UITableView tableView, Foundation.NSIndexPath indexPath)
{
if (indexPath.Row == LoadedItems.Count) {
var loadingCell = tableView.DequeueReusableCell (LoadingCellID, indexPath) as LoadFooterCell;
loadingCell.Loading = !hasNextPage;
return loadingCell;
}
var cell = tableView.DequeueReusableCell (CellID, indexPath) as GrowRowTableCell;
var item = LoadedItems [indexPath.Row];
// Setup
cell.Image = UIImage.FromFile(item.ImageName);
cell.Title = item.Title;
cell.Description = item.Description;
return cell;
}
bool hasNextPage;
我在这里很快模仿了可增长的Xamarin示例代码中的一个示例:
您可以通过几种方式实现这一点,我将在这里介绍几种: 在
TableViewSource
中,可以执行以下操作:
1.)实现:public override UIView GetViewForFooter(UITableView tableView,第九节)
并返回您的UILabel
2.)实现公共覆盖nfloat GetHeightForFooter(UITableView表格视图,第九节)
并为您的UIView返回高度
如果您的页脚将是一个简单的UILabel,您可以将上面的步骤1替换为:
实现公共重写字符串TitleForFooter(UITableView tableView,第九节)
并返回“Duke u ngarkuar”
您仍然需要实现公共覆盖nfloat GetHeightForFooter(UITableView tableView,第九节)
并返回一个高度,否则您的页脚将不会显示
或者,UITableView公开一个名为TableFooterView
的属性,该属性允许您为页脚设置UIView 您可以根据自己的喜好调整大小
var footerView = new UIView(new RectangleF(0, 0, 375, 66));
TableView.TableFooterView = footerView;
不,我在UIViewController中,因为我还有一个搜索栏和一些其他UIView。我有一个UITableViewSource类和一个UITableViewCell自定义类。我已经用一个可能适合的解决方案更新了我的答案,如果您有任何问题,请告诉我。