ios - 在 UITableviewcell 的 ios 11 布局 subview subview 中找不到 UITableViewCellDeleteConfirmationView?

标签 ios swift xamarin.ios ios11

我必须覆盖Tableviewcell中的高度滑动删除按钮,我使用了下面的代码,它在ios 10上工作正常,但在ios11中我无法在layoutsubview类中找到UITableViewCellDeleteConfirmationView

 foreach (var views in this.Subviews)
    {
        if (views.Class.Name.ToString() == new NSString("UITableViewCellDeleteConfirmationView"))
        {
            CGRect newFrame = views.Frame;
            CGRect newframe1 = new CGRect(newFrame.X, 6, newFrame.Size.Width, 59);
            views.Frame = newframe1;

            foreach (var getButtonviews in views.Subviews)
            {
                Console.WriteLine("x:"+getButtonviews.Frame.X);
                Console.WriteLine("W:"+getButtonviews.Frame.Width);
                if (getButtonviews.Class.Name.ToString() == "_UITableViewCellActionButton")
                {
                    UIImage image = UIImage.FromBundle("img_line");
                    UIButton button = (UIButton)getButtonviews;
                    UIImageView imageview = new UIImageView();
                    imageview.Frame = new CGRect(getButtonviews.Frame.X + 120, 0, 1, getButtonviews.Frame.Height);
                    imageview.Image = image;
                    button.AddSubview(imageview);

                    foreach (var getButton in getButtonviews.Subviews)
                    {
                        if (getButton.Class.Name.ToString() == "UIButtonLabel")
                        {
                            UILabel label = (UILabel)getButton;
                            label.Font = UIFont.FromName("ProximaNova-Regular", 13);
                        }
                    }
                }
            }
        }

    }

最佳答案

iOS10之后tableview内部的 View 层次结构发生了变化。

iOS8 - iOS10

UITableView -> UITableViewCell -> UITableViewCellDeleteConfirmationView -> _UITableViewCellActionButton

iOS11

  1. 使用 Xcode8

    UITableView -> UITableViewWrapperView -> UISwipeActionPullView -> UISwipeActionStandardButton

  2. 使用 Xcode9

    UITableView -> UISwipeActionPullView -> UISwipeActionStandardButton

解决方案:

我使代码在 iOS8 - iOS11 上都能工作,并将所有代码放在 ViewController 中的 ViewWillLayoutSubviews 中,但首先,我们需要知道我们是哪个单元格选择。

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.
                            }
                        }
                    }
                }
            }
        }
    }
}

关于ios - 在 UITableviewcell 的 ios 11 布局 subview subview 中找不到 UITableViewCellDeleteConfirmationView?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46716229/

相关文章:

ios - UIBackButton 背景图像未出现

ios - lldb 类具有不兼容的父类(super class)

ios - Swift:如何在firebase中根据用户id读取特定用户数据?

ios - 从 UITapGestureRecognizer 中删除目标

ios - 如何在 ios 的 UIPICKER VIEW 中禁用滚动效果和阴影效果?

ios - MKMapView MKCircle 渲染一个半径太大的圆

swift - 当 SwiftUI View 关闭时,如何发送最后一条消息并关闭套接字?

xamarin.forms - MvvmCross如何实现 View 模型的条件呈现?

ios - Xamarin.iOS 未看到对 iOS 绑定(bind)库的引用

ios - 剩余的 UITextView 字符数