【问题标题】:Swipe to delete option in UITableView issues滑动删除 UITableView 问题中的选项
【发布时间】:2013-08-13 14:23:50
【问题描述】:

我想在我的项目中使用“滑动删除”选项。

-(void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath 
{

    if (editingStyle == UITableViewCellEditingStyleDelete)
    {
        NSDictionary *userData = [_contactsArray objectAtIndex:indexPath.row];
        NSLog(@"delete row %@",userData);
    }
}

- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath
{

    return YES;
}

我正在使用这段代码,但它给出了我不想要的以下输出。

我不希望单元格上的左侧减号。 我只想滑动并显示删除按钮。 我在上一个项目中使用的相同代码,它工作正常(即仅滑动以显示删除按钮,左侧没有减号)

请帮我解决这个问题。

【问题讨论】:

标签: iphone ios objective-c uitableview


【解决方案1】:

您正在为“滑动删除”功能覆盖正确的委托方法。关于减号:

像这样隐藏减号:

self.yourTableView.editing = NO;
//or simply remove this line of code altogether because this property is NO by default

像这样显示减号:

self.yourTableView.editing = YES;

【讨论】:

    【解决方案2】:

    首先创建您的表视图并将委托和数据源方法设置为。

     self.contacts=[[UITable alloc]init];
        self.contacts.dataSource=self;
        self.contacts.delegate=self;
        self.contacts.userInteractionEnabled=YES;
        //self.contacts.editing = YES;
        [self.view addSubview:self.contacts];
    

    然后重写Delegate和数据源方法,为delete重写下面的方法。

    - (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
            // Return YES if you want the specified item to be editable.
            return YES;
        }
    
        // Override to support editing the table view.
        - (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
            if (editingStyle == UITableViewCellEditingStyleDelete) {
                //add code here for when you hit delete
            }    
        }
    

    对于左侧的减号,请使用 self.contacts.editing = YES; 在我的情况下,我不想要这个减号,所以不要使用该属性或设置 self .contacts.editing = 否;

    感谢hw731提醒我这个楼盘,我在这上面浪费了半天时间。

    【讨论】:

      【解决方案3】:

      试试这个:

       - (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
              // Return YES if you want the specified item to be editable.
              return YES;
          }
      
          // Override to support editing the table view.
          - (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
              if (editingStyle == UITableViewCellEditingStyleDelete) {
                  //add code here for when you hit delete
              }    
          }
      

      【讨论】:

        【解决方案4】:

        听起来您正在打开tableView.editing 标志,因此行显示为减号。尝试搜索 self.tableView.editing = YES; 并将其设为 NO 或简单地注释掉该行。

        【讨论】:

          猜你喜欢
          • 2012-12-28
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2012-07-07
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多