【问题标题】:UImenucontroller action use delegate to set tableView be editing not working_swiftUImenucontroller 操作使用委托设置 tableView 正在编辑不工作_swift
【发布时间】:2017-06-20 16:26:45
【问题描述】:

我有一个 tableView 和 tableViewCell。我创建了 MenuController 来进行 setTableView 编辑。但似乎失败了。但我使用导航按钮添加“testFunc”的操作。它可以改变多选编辑。我想念什么?我已经添加了 tableView 的子视图。而且我也跟着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() 

}
}

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

【问题讨论】:

    标签: ios swift uitableview autolayout tableviewcell


    【解决方案1】:
    @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
    

    【讨论】:

    • 这不是菜单控制器。
    • 这是一个在objectiveC中批量选择的uiviewcontroller。
    • 感谢您的建议。但我的观点是为什么 menucontroller 工作而 viewcontroller 的 tableview 不工作。
    • 这也是带有tableview的UIViewcontroller。
    • 好的,谢谢。 @Mohamed Raffi
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-07-18
    • 1970-01-01
    相关资源
    最近更新 更多