如何仅刷新/重绘uicollectionview中的补充​​视图?

时间:2019-03-10 19:27:12

标签: ios xamarin.ios uicollectionview

是否可以仅刷新或重绘UICollectionView中的补充​​视图?我有一种情况,我将使用行的插入/删除来更新每个节,根据该情况,我需要更新节标题或所谓的补充视图(它具有一个UI元素,其可见性取决于项数)在该部分)。那么有什么方法可以更新UICollectionView的补充视图,而无需更新其他任何内容?

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

SupplementaryView:

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

1 个答案:

答案 0 :(得分:0)

解决方案:

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

我看不到您的代码,您应该知道indexPath中的section,您正在插入/删除数据。

例如:

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