【问题标题】:How to refresh/redraw only the supplementary views in a uicollectionview?如何仅刷新/重绘 uicollectionview 中的补充视图?
【发布时间】:2019-03-10 19:27:12
【问题描述】:

是否可以只刷新或重绘 UICollectionView 中的补充视图?我有一个场景,我将使用插入/删除行来更新每个部分,基于此我需要更新部分标题或所谓的补充视图(它有一个 UI 元素,其可见性取决于项目的数量在该部分)。那么有什么方法可以更新 UICollectionView 的补充视图而不需要更新其他任何东西?

另外,如果特定部分不包含任何数据,我需要将补充视图的大小设置为空?

补充视图:

using System;
using System.Collections.Generic;
using Foundation;
using IpcCommon;
using IpcCommon.Enumerations;
using IpcCommon.Model;
using ObjCRuntime;
using UIKit;

namespace ipc_offline_app.iOS.Portal.CustomCells.Dashboard.Redesign
{
    public partial class DashboardHeader : UICollectionReusableView
    {
        public static readonly NSString Key = new NSString("DashboardHeader");
        public static readonly UINib Nib;

        private DashCategories _dashboardCategory;
        private bool _hideViewAll;
        private IShelfItemClickListener _listener;
        private string _title;

        static DashboardHeader()
        {
            Nib = UINib.FromName("DashboardHeader", NSBundle.MainBundle);
        }

        protected DashboardHeader(IntPtr handle) : base(handle)
        {
            // Note: this .ctor should not contain any initialization logic.
        }

        public static DashboardHeader CreateCell()
        {
            var array = NSBundle.MainBundle.LoadNib("DashboardHeader", null, null);
            var cell = Runtime.GetNSObject<DashboardHeader>(array.ValueAt(0));
            return cell;
        }

        public void PopulateCell(string title, IShelfItemClickListener listener, bool hideViewAll, DashCategories category = DashCategories.NONE)
        {
            _title = title;
            _dashboardCategory = category;
            _listener = listener;
            ViewAllButton.Hidden = hideViewAll;
            _hideViewAll = hideViewAll;
            TitleButton.SetTitle(_title, UIControlState.Normal);
            UpdateViewAllVisibility();
        }

        [Export("awakeFromNib")]
        public new void AwakeFromNib()
        {
            ViewAllButton.Layer.CornerRadius = 5;
            ViewAllButton.Layer.BorderWidth = 1;
            ViewAllButton.Layer.BorderColor = Utils.ColorFromHex(Colors.SILVER).CGColor;
        }

        partial void OpenSection(NSObject sender)
        {
            if (_listener != null && _dashboardCategory != DashCategories.NONE)
                _listener.OnShelfItemClicked(_dashboardCategory, IpcCommon.Constants.VIEW_TYPE_GALLERY);
        }

        public void UpdateViewAllVisibility(Dictionary<DashCategories, List<Assignment>> filteredAssignments = null)
        {
            if (filteredAssignments != null && filteredAssignments[_dashboardCategory].Count > IpcCommon.Constants.MAX_SECTION_ITEMS)
                _hideViewAll = false;
            ViewAllButton.Hidden = _hideViewAll;
            NSLayoutConstraint trailingConstraint;
            if (_hideViewAll)
                trailingConstraint = NSLayoutConstraint.Create(TitleButton, NSLayoutAttribute.Trailing, NSLayoutRelation.Equal, ViewAllButton, NSLayoutAttribute.Trailing, 1, 0);
            else
                trailingConstraint = NSLayoutConstraint.Create(TitleButton, NSLayoutAttribute.Trailing, NSLayoutRelation.Equal, ViewAllButton, NSLayoutAttribute.Leading, 1, -10);
            AddConstraint(trailingConstraint);
            LayoutIfNeeded();
            LayoutSubviews();
        }
    }
}

【问题讨论】:

  • 有没有类似 CollectionView.ReloadItems 用于重新加载补充视图?

标签: ios xamarin.ios uicollectionview


【解决方案1】:

解决方案:

您可以获得对您插入/删除数据的那个部分的每个可见补充视图的引用。

我没有看到您的代码,您应该知道您正在插入/删除数据的sectionindexPath

例如:

   // indexP here is an example
   NSIndexPath indexP = NSIndexPath.FromRowSection(0,0);         
   Header supplementaryView = CollectionView.GetSupplementaryView(myelementkind, indexP) as Header;

   //perform your own action here    
   supplementaryView.checkvisibility();

【讨论】:

  • 您好,我已经更新了这个问题。问题是可以从节中添加或删除数据,如果某个节中没有数据,我们需要隐藏节头本身,即标题引用大小应该为空,稍后如果我们添加数据,它应该再次显示
  • @MidhunKumar 您应该重写此方法getreferencesizeforheader 将标题的高度更改为 0 以隐藏标题。
  • 我已经覆盖了那个方法。事情是在 api 带来新数据并将其他行插入或删除行之后我将如何启动对 getreferencesizeforheader 方法的调用?
  • UICollectionvView.CollectionViewLayout.InvalidateLayout();确保调用 getreferencesizeforheader。但它不会使视图再次重绘。也许像你建议的那样,我可能必须有自己的方法
  • 也尝试调用不同的方法,但没有效果。更新了相关课程。与 ReloadItem 类似,我们可以调用什么来更新 siupplementaryView?
猜你喜欢
  • 1970-01-01
  • 2017-07-24
  • 1970-01-01
  • 2013-02-20
  • 2015-12-22
  • 2015-01-15
  • 1970-01-01
  • 1970-01-01
  • 2016-07-23
相关资源
最近更新 更多