gpt4 book ai didi

ios - 无法在 UITableviewcell 的 ios 11 layoutsubview subview 中找到 UITableViewCellDeleteConfirmationView?

转载 作者:可可西里 更新时间:2023-11-01 02:00:34 26 4
gpt4 key购买 nike

我必须覆盖 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 layoutsubview subview 中找到 UITableViewCellDeleteConfirmationView?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46716229/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com