【问题标题】:Not able find UITableViewCellDeleteConfirmationView in ios 11 layoutsubview subview in UITableviewcell?在 UITableviewcell 的 ios 11 layoutsubview 子视图中找不到 UITableViewCellDeleteConfirmationView?
【发布时间】:2017-10-12 18:08:19
【问题描述】:

我必须覆盖 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);
                        }
                    }
                }
            }
        }

    }

【问题讨论】:

标签: ios swift xamarin.ios ios11


【解决方案1】:

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

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

【讨论】:

  • 我怎样才能从 uitableview 获得 UISwipeActionPullView -> UISwipeActionStandardButton 你能给我任何示例代码吗?它将帮助我很多
  • @Kamalkumar.E 我已附上整个代码,请检查。如果您仍有任何问题,请随时告诉我。
  • @ColeXia-MSFT,在我的情况下,如果我使用 iOS 11 和 xcode9 构建,单元格甚至都不可见。任何的想法 ?我怀疑只有包装视图周围的东西,但无法捕捉到它。看看你能不能帮忙。谢谢
  • @torap 你是用 ios 还是 xamarin.ios 开发的?你能打开一个新线程来描述你的问题吗?
  • child(TableDelegate)中owner的最佳使用方式必须与WeakReference一起,以免引起强引用。像这样; WeakReference<UIViewController> owner;
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-06-29
  • 1970-01-01
  • 2013-06-28
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多