Not able find UITableViewCellDeleteConfirmationView in ios 11 layoutsubview subview in UITableviewcell?

后端 未结 1 761
深忆病人
深忆病人 2021-01-07 05:28

I have to override the height swipe delete button in Tableviewcell i used the below code it\'s work fine ios 10 but in ios11 i can\'t able find the UITableViewCellDeleteConf

相关标签:
1条回答
  • 2021-01-07 05:36

    The view hierarchy inside tableview has been changed after iOS10.

    iOS8 - iOS10

    UITableView -> UITableViewCell -> UITableViewCellDeleteConfirmationView -> _UITableViewCellActionButton

    iOS11

    1. work with Xcode8

      UITableView -> UITableViewWrapperView -> UISwipeActionPullView -> UISwipeActionStandardButton

    2. work with Xcode9

      UITableView -> UISwipeActionPullView -> UISwipeActionStandardButton

    Solution:

    I make the code work both at iOS8 - iOS11, and I put all the code at ViewWillLayoutSubviews in ViewController , but first , we need to know which cell we are selecting.

    public class TableDelegate : UITableViewDelegate
    {
        YourViewController owner;
    
        public TableDelegate(YourViewController vc){
            owner = vc;
        }
    
        public override UITableViewRowAction[] EditActionsForRow(UITableView tableView, NSIndexPath indexPath)
        {
            UITableViewRowAction hiButton = UITableViewRowAction.Create(
                UITableViewRowActionStyle.Default,
                "Hi",
                delegate {
                    Console.WriteLine("Hello World!");
                });
            return new UITableViewRowAction[] { hiButton };
        }
    
        public override void WillBeginEditing(UITableView tableView, NSIndexPath indexPath)
        {
            owner.selectIndexPath = indexPath;
            owner.View.SetNeedsLayout();
        }
    
        public override void DidEndEditing(UITableView tableView, NSIndexPath indexPath)
        {
            owner.selectIndexPath = null;
        }
    }
    
    public class TableSource : UITableViewSource
    {
    
        string[] TableItems;
        string CellIdentifier = "TableCell";
    
        public TableSource(string[] items)
        {
            TableItems = items;
        }
    
        public override nint RowsInSection(UITableView tableview, nint section)
        {
            return TableItems.Length;
        }
    
        public override UITableViewCell GetCell(UITableView tableView, NSIndexPath indexPath)
        {
            UITableViewCell cell = tableView.DequeueReusableCell(CellIdentifier);
            string item = TableItems[indexPath.Row];
    
            //---- if there are no cells to reuse, create a new one
            if (cell == null)
            { cell = new UITableViewCell(UITableViewCellStyle.Default, CellIdentifier); }
    
            cell.TextLabel.Text = item;
    
            return cell;
        }
    }
    
    public partial class ViewController : UIViewController
    {
        protected ViewController(IntPtr handle) : base(handle)
        {
            // Note: this .ctor should not contain any initialization logic.
        }
    
        public NSIndexPath selectIndexPath { get; set; }
    
        public override void ViewDidLoad()
        {
            base.ViewDidLoad();
            // Perform any additional setup after loading the view, typically from a nib.
            string[] tableItems = new string[] { "Vegetables", "Fruits", "Flower Buds", "Legumes", "Bulbs", "Tubers" };
            tableview.Source = new TableSource(tableItems);
            tableview.Delegate = new TableDelegate(this);
        }
    
        public override void ViewWillLayoutSubviews()
        {
            base.ViewWillLayoutSubviews();
    
            if (this.selectIndexPath != null)
            {
                if (UIDevice.CurrentDevice.CheckSystemVersion(11, 0))
                {
                    // Code that uses features from iOS 11.0 and later
                    foreach (UIView subview in tableview.Subviews)
                    {
                        if (subview.Class.Name.ToString() == "UISwipeActionPullView")
                        {
                            foreach (var buttonViews in subview.Subviews)
                            {
                                if (buttonViews.Class.Name.ToString() == "UISwipeActionStandardButton")
                                {
                                    //operate what you want.
                                }
                            }
                        }
                    }
                }
                else
                {
                    // Code to support earlier iOS versions
                    UITableViewCell cell = tableview.CellAt(this.selectIndexPath);
                    foreach (UIView subview in cell.Subviews)
                    {
                        if (subview.Class.Name.ToString() == "UITableViewCellDeleteConfirmationView")
                        {
                            foreach (var buttonViews in subview.Subviews)
                            {
                                if (buttonViews.Class.Name.ToString() == "_UITableViewCellActionButton")
                                {
                                    //operate what you want.
                                }
                            }
                        }
                    }
                }
            }
        }
    }
    
    0 讨论(0)
提交回复
热议问题