【问题标题】:Adding a delete button to UiTableViewCell向 UiTableViewCell 添加删除按钮
【发布时间】:2014-04-07 12:23:36
【问题描述】:

由于我在我的应用程序中使用滑出式菜单控制器 - 在 UITablewViewCell 上滑动删除不再起作用,因为平移手势用于打开/关闭侧菜单。

所以,我正在考虑添加一个删除按钮以始终显示在每个单元格上 - 因此用户只需点击删除即可删除该单元格。

我将此代码添加到 UItableView cellforRowAtIndexPath 方法:

    /* Remove Button */
UIButton *removeButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
removeButton.frame = CGRectMake(200.0f, 5.0f, 75.0f, 30.0f);
[removeButton setTitle:@"Remove" forState:UIControlStateNormal];
removeButton.tintColor = [UIColor colorWithRed:0.667 green:0.667 blue:0.667 alpha:1]; /*#aaaaaa*/
removeButton.titleLabel.font = [UIFont systemFontOfSize:15];
[cell addSubview:removeButton];
[removeButton addTarget:self
                 action:@selector(removeItem:)
          forControlEvents:UIControlEventTouchUpInside];

这会添加按钮,并且在删除方法中我不确定如何实际删除正确的单元格。

任何人都可以在这里指出正确的方向吗?

谢谢!

【问题讨论】:

    标签: ios objective-c uitableview


    【解决方案1】:

    基本上你需要cell 的索引,按下删除时必须删除它。

    您可以设置tag 属性,当按下按钮时,您可以检查正在发送事件的按钮的标签属性。

    见下面的代码,

    UIButton *removeButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    removeButton.tag = indexPath.row;
    removeButton.frame = CGRectMake(200.0f, 5.0f, 75.0f, 30.0f);
    [removeButton setTitle:@"Remove" forState:UIControlStateNormal];
    removeButton.tintColor = [UIColor colorWithRed:0.667 green:0.667 blue:0.667 alpha:1]; /*#aaaaaa*/
    removeButton.titleLabel.font = [UIFont systemFontOfSize:15];
    [cell addSubview:removeButton];
    [removeButton addTarget:self
                     action:@selector(removeItem:)
              forControlEvents:UIControlEventTouchUpInside];
    
    
    -(void) removeItem:(id) sender
    {
     UIButton *button = (UIButton*)sender;
    
     int index = button.tag;
    }
    

    【讨论】:

      【解决方案2】:

      试试这个,

      - (IBAction)removeItem:(id)sender {
           UIButton *button = (UIButton *)sender;
           CGPoint pointInTable = [button convertPoint:button.bounds.origin toView:_tableView];    
           NSIndexPath *indexPath = [_tableView indexPathForRowAtPoint:pointInTable];
           //Remove the cell at indexPath
      }
      

      【讨论】:

        【解决方案3】:

        //1.给按钮添加标签,以便以后识别

        removeButton.tag = indexPath.row;
        

        //2.获取单元格

        UITableViewCell *cellToBeDeleted = [tableView cellForRowAtIndexPath:sender.tag];
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2018-01-27
          相关资源
          最近更新 更多