【问题标题】:Add a Custom Footer at the end of UITableView in Xamarin.iOS在 Xamarin.iOS 中的 UITableView 末尾添加自定义页脚
【发布时间】:2016-04-22 15:26:25
【问题描述】:

我想向UITableView 添加页脚视图,当用户到达列表末尾并且将加载新数据时显示UIProgresIndicator,或者当没有更多数据时显示UILabel被取走。

我使用了下面的代码,但没有任何反应:

UITableViewHeaderFooterView footer = new UITableViewHeaderFooterView ();

UILabel futerlabel = new UILabel ();
futerlabel.Text = "Duke u ngarkuar";

footer.AddSubview (futerlabel);

如何实现这一点。

【问题讨论】:

    标签: uitableview xamarin xamarin.ios table-footer


    【解决方案1】:

    如果您在 UITableViewController 中,试试这个:

            TableView.TableFooterView = footer;
    

    更新

    经过考虑后,我建议不要使用页脚,而是在数据末尾使用额外的单元格,这将得到 effect

    使用此方法检查您是否已滚动到最后一项并更新表格的数据:

    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 ();    
                }
    
            }
        }
    

    然后在 Delegate 中检查最后一项并创建一个不同的单元格,如下所示:

    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;
    

    我在这里快速模拟了 GrowTable Xamarin 示例代码中的一个示例: https://github.com/b099l3/LoadingTableExample

    【讨论】:

    • 不,我在 UIViewController 中,因为我还有一个 SearchBar 和其他一些 UIView。我有一个 UITableViewSource 类和一个 UITableViewCell 自定义类
    • 我已经用可能适合的解决方案更新了我的答案,如果您有任何问题,请告诉我。
    【解决方案2】:

    您可以通过多种方式完成此操作,我将在此处进行一些布局:

    在您的TableViewSource 中,您可以这样做:

    1.) 实施:public override UIView GetViewForFooter(UITableView tableView, nint section) 并返回您的 UILabel

    2.) 实现 public override nfloat GetHeightForFooter(UITableView tableView, nint section) 并为您的 UIView 返回一个高度


    如果您的页脚将是一个简单的 UILabel,您可以将上面的步骤 1 替换为:

    实现public override string TitleForFooter(UITableView tableView, nint section) 并返回“Duke u ngarkuar”。

    您仍然需要实现 public override nfloat GetHeightForFooter(UITableView tableView, nint section) 并返回一个高度,否则您的页脚将不会显示。


    另外,UITableView 公开了一个名为TableFooterView 的属性,允许您为页脚设置 UIView。

    【讨论】:

      【解决方案3】:

      您可以根据自己的喜好调整大小。

      var footerView = new UIView(new RectangleF(0, 0, 375, 66));
      TableView.TableFooterView = footerView;
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2014-06-26
        • 2020-08-25
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多