ios - UImenucontroller Action 使用委托(delegate)来设置 tableView 正在编辑不工作_swift

标签 ios swift autolayout uitableview

我有一个 tableView 和 tableViewCell。我创建 MenuController 来设置 TableView 编辑。但它似乎失败了。但是我使用导航按钮添加“testFunc”的 Action 。它可以更改多选编辑。我想念什么?我已经添加了 tableView 的 subview 。而且我也关注UITableViewDataSource,UITableViewDelegate。

class ViewController: UIViewController,DelegateA, UITableViewDataSource, UITableViewDelegate{ 

lazy var tableView:UITableView = { ()->UITableView in
    let ui:UITableView = UITableView()
    ui.delegate = self
    ui.dataSource = self
    ui.separatorStyle = .none
    ui.backgroundColor = defaultBackgroundColor
    return ui
}()

override func viewDidLoad() {
    super.viewDidLoad()

    self.tableView.allowsMultipleSelectionDuringEditing = true
}

func testFunc() {

    print("123") //it print "123" successful

    //not working
    if(self.tableView.isEditing == false) {
        self.tableView.setEditing(true, animated:true)
    }
    else {
        self.tableView.setEditing(false, animated:true)
    }
}

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
//..........
}

func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    // .........
}

}

这是自定义tableViewCell的标签

class UITalkContent:UILabel {

var delegate:DelegateA?

override init(frame: CGRect) {
    super.init(frame:frame)
    translatesAutoresizingMaskIntoConstraints = false
    numberOfLines = 0
    isUserInteractionEnabled = true
    addGestureRecognizer(UILongPressGestureRecognizer(target: self, action: #selector(gestureLongPress)))
}

required init?(coder aDecoder: NSCoder) {
    fatalError("init(coder:) has not been implemented")
}

override func drawText(in rect: CGRect) {
    let insets: UIEdgeInsets = UIEdgeInsets(top: defaultContentPadding, left: defaultPadding, bottom: defaultContentPadding, right: defaultPadding)
    super.drawText(in: UIEdgeInsetsInsetRect(rect, insets))
}

func gestureLongPress(sender:UILongPressGestureRecognizer) {

    if (sender.state == .ended) {
        return
    }

    becomeFirstResponder()


    let menu = UIMenuController.shared


    menu.menuItems = [
        UIMenuItem(title: localString(string: "FORWARD"), action: #selector(forward)),
    ]


    menu.setTargetRect(bounds, in: self)


    menu.setMenuVisible(true, animated: true)
}

func forward(menu :UIMenuController ) {
    print("321") //print "321" successfully 
    self.delegate = ViewController()
    self.delegate?.testFunc()

}

override var canBecomeFirstResponder: Bool{
    return true
}

override func canPerformAction(_ action: Selector, withSender sender: Any?) -> Bool {
    if (action == #selector(UIResponderStandardEditActions.copy(_:)) || action == #selector(forward)) {
        return true
    }
    return false
}

override func copy(_ sender: Any?) {
    UIPasteboard.general.string = text
}
}



protocol DelegateA {
func testFunc()
}

class ContentTableViewCell: UITableViewCell {

var labelContent:UILabel = { ()->UILabel in
    let ui:UILabel = UITalkContent()
    ui.textColor = UIColor(red:0.20, green:0.20, blue:0.20, alpha:1.00)
    ui.backgroundColor = UIColor.white
    ui.font = defaultTextFont
    ui.numberOfLines = 0
    ui.lineBreakMode = NSLineBreakMode.byCharWrapping
    ui.layer.cornerRadius = defaultButtonRadius
    ui.layer.masksToBounds = true
    ui.isHidden = false
    return ui
}()

    override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
    super.init(style: style, reuseIdentifier: reuseIdentifier)
    loadContent()
}

required init?(coder aDecoder: NSCoder) {
    super.init(coder: aDecoder)
}

override func layoutSubviews() {
    super.layoutSubviews()
    loadVFL()
}

func loadContent() {
    contentView.addSubview(labelContent)
}

func loadVFL() {
    labelContent.frame = CGRect(x: 0, y: 0, width: contentWidth, height: CGFloat.greatestFiniteMagnitude)
    labelContent.sizeToFit() 

}
}

我想点击“前进”菜单来显示这样的 View 。
谢谢。
Hello here is the image what i want.please help me thanks.

最佳答案

@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad
{
    [super viewDidLoad];
    dataArray=[[NSMutableArray alloc]init];

    UIButton *btn = [[UIButton alloc]init];
    btn.backgroundColor = [UIColor blueColor];
    [btn setTitle:@"Click Me" forState:UIControlStateNormal];
    [btn addTarget:self action:@selector(didTapBringCheckBoxBtn) forControlEvents:UIControlEventTouchUpInside];
    btn.frame = CGRectMake(0, 0, self.view.frame.size.width, 100);
    [self.view addSubview:btn];

    [dataArray addObject:@"Apple"];
    [dataArray addObject:@"Mango"];
    [dataArray addObject:@"Papaya"];
    [dataArray addObject:@"Guava"];
    [dataArray addObject:@"Pineapple"];
    [dataArray addObject:@"Strawberry"];
    [dataArray addObject:@"Banana"];
    [dataArray addObject:@"Grapes"];
    [dataArray addObject:@"Pomegranate"];
    [dataArray addObject:@"Green Tea"];
    [dataArray addObject:@"Raisin"];

    self.mTableView = [[UITableView alloc]initWithFrame:CGRectMake(0, btn.frame.size.height, self.view.frame.size.width, self.view.frame.size.height) style:UITableViewStylePlain];
    self.mTableView.delegate = self;
    self.mTableView.dataSource = self;
    [self.view addSubview:self.mTableView];
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
}

#pragma mark - Check Box Button Action

- (void)didTapBringCheckBoxBtn
{
    [self.mTableView setEditing:YES animated:YES];
}

#pragma mark - UITableView DataSource Methods

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return [dataArray count];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *cellIdentifier = @"cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
    if (cell == nil)
    {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
    }
    cell.textLabel.text = [dataArray objectAtIndex:indexPath.row];
    return cell;
}

#pragma mark - UITableView Delegate Methods

- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 3;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSLog(@"user selected %@",[dataArray objectAtIndex:indexPath.row]);
}

- (void)tableView:(UITableView *)tableView didDeselectRowAtIndexPath:(NSIndexPath *)indexPath
{
     NSLog(@"user de-selected %@",[dataArray objectAtIndex:indexPath.row]);
}

@end

关于ios - UImenucontroller Action 使用委托(delegate)来设置 tableView 正在编辑不工作_swift,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44658426/

相关文章:

xcode - 使用 Autolayout 将 XIB 的 Root View 动态调整为其内容的大小

ios - 自动布局约束没有得到尊重

ios - 具有 anchor 样式自动布局约束的 UIButtons 未显示

ios - 如何在 Swift 3 中制作嵌套的 JSON

ios - 带后台循环的 for 循环,有完成吗?

ios - UIViewController 类型的值永远不能为零,不允许比较

swift - 如何使用 Swift 更改工具栏按钮项目标题颜色

iphone - 使用字典数组中的数据填充 Tapku 日历

swift - iOS 12.2 中 WebKitLegacy 框架的应用程序崩溃

ios - Swift:如何直接设置对象的属性?