我想在项目中使用“轻扫删除”选项。
-(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;
}
我正在使用此代码,但它提供了我不想要的以下输出。
我不希望左侧减号在单元格上。 我只想要滑动并显示删除按钮。 我在之前的项目中使用的代码相同,并且工作正常(即只滑动以显示删除按钮,左侧没有减号)
请帮我解决这个问题。
答案 0 :(得分:9)
您正在覆盖“滑动删除”功能的正确委托方法。关于减号:
像这样隐藏减号:
self.yourTableView.editing = NO;
//or simply remove this line of code altogether because this property is NO by default
显示这样的减号:
self.yourTableView.editing = YES;
答案 1 :(得分:3)
首先创建表视图并将delegate和datasource方法设置为该视图。
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和数据源方法,删除覆盖以下方法。
- (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 = NO;
谢谢 hw731 提醒我这个属性,我浪费了半天时间。
答案 2 :(得分:1)
试试这个:
- (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
}
}
答案 3 :(得分:0)
听起来你正在打开tableView.editing
标志,所以行显示的是减号。尝试搜索self.tableView.editing = YES;
并将其设为NO
,或者只是对该行进行评论。