无法删除表视图行

时间:2016-03-24 14:54:40

标签: ios objective-c uitableview

我尝试了很多方法来删除表视图中的行,但是现在,这些都不起作用。 我只想在用户对其施加长压时删除tableView行。

ChecklistViewController.h

#import <UIKit/UIKit.h>

@interface CheckListViewController : UIViewController<UITableViewDelegate,UITableViewDataSource>

@end

ChecklistViewController.m

@interface CheckListViewController ()
@property (weak, nonatomic) IBOutlet UITableView *tableView;
@end

@implementation CheckListViewController {
NSMutableDictionary *_checkFlag;
NSArray *_menuData;
NSMutableArray *_edit;
NSMutableArray *_inputCell;
NSInteger rowToremove;
NSIndexPath *theIndexPathe;
int _maxInputCell;
}

- (IBAction)barAddTapped:(id)sender {

if (_maxInputCell < 3){

_maxInputCell +=1;
[_inputCell addObject:[NSNull null]];
[self.tableView reloadData];

CGPoint offset = CGPointMake(0, self.tableView.contentSize.height -     self.tableView.frame.size.height);
[self.tableView setContentOffset:offset animated:YES];
}
}

- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
_maxInputCell = 1;
_checkFlag = [[NSMutableDictionary alloc]init];
_inputCell =  [[NSMutableArray alloc]initWithCapacity:_maxInputCell];

for(int i=0;i<_maxInputCell;i++)
    [_inputCell addObject:[NSNull null]];

_menuData = @[
              @"Passeport",@"Carte membre",@"Carte d'embarquement",@"Visa",@"Autorisation",@"Meddicaments autorisés",@"Produits autorisés"];

_edit = [NSMutableArray arrayWithObjects:@"text1",nil];

self.tableView.backgroundColor = [UIColor colorWithWhite:255 alpha:.6f];
self.tableView.layer.cornerRadius=10.0f;
self.tableView.clipsToBounds=YES;

UILongPressGestureRecognizer *lpgr = [[UILongPressGestureRecognizer alloc]
                                      initWithTarget:self action:@selector(handleLongPress:)];
lpgr.minimumPressDuration = 1.5; //seconds
lpgr.delegate = self;
[self.tableView addGestureRecognizer:lpgr];
 }
 - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return 1;
 }

 - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
 {

NSLog(@"%d",[_menuData count] + _maxInputCell);
return [_menuData count] + _maxInputCell;
 }

 - (IBAction)returnKeyPressed:(id)sender {
 [sender resignFirstResponder];
 }

 - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *cell;
long textFieldIndex = indexPath.row - [_menuData count];

if(textFieldIndex>= 0 && textFieldIndex < _maxInputCell) {
    //Input cell
    cell = [tableView dequeueReusableCellWithIdentifier:@"textCell"];
    UITextField *textField;

    if(!_inputCell[textFieldIndex]) {
        _inputCell[textFieldIndex] = (UITextField *)[cell viewWithTag:1];
    }
    textField = _inputCell[textFieldIndex];
} else {
    //Label cell
    cell = [tableView dequeueReusableCellWithIdentifier:@"thisCell"];
    cell.textLabel.text = [_menuData objectAtIndex:indexPath.row];
}

if(_checkFlag[indexPath]!=nil)
    cell.accessoryType = UITableViewCellAccessoryCheckmark;
else  cell.accessoryType = UITableViewCellAccessoryNone;

return cell;
}

- (NSIndexPath *)tableView:(UITableView *)tableView willSelectRowAtIndexPath:(NSIndexPath *)indexPath{
if(_checkFlag[indexPath]!=nil) {
    [_checkFlag removeObjectForKey:indexPath];
}else _checkFlag[indexPath]=@"1";

[self.tableView reloadData];
return nil;
}

-(void)handleLongPress:(UILongPressGestureRecognizer *)gestureRecognizer
{
CGPoint p = [gestureRecognizer locationInView:self.tableView];

NSIndexPath *indexPath = [self.tableView indexPathForRowAtPoint:p];
if (indexPath == nil) {
    NSLog(@"long press on table view but not on a row");
} else if (gestureRecognizer.state == UIGestureRecognizerStateBegan && indexPath.row > 6) {
    NSLog(@"long press on table view at row %ld", (long)indexPath.row);
    UIAlertView *alert = [[UIAlertView alloc]initWithTitle:nil message:@"Voulez-vous supprimer cette ligne ?" delegate:self cancelButtonTitle:@"OK" otherButtonTitles:@"Annuler",nil];
    alert.tag = 70;
    [alert show];
    rowToremove = indexPath.row - 7;
    theIndexPathe = indexPath;
}
 else {
    NSLog(@"gestureRecognizer.state = %ld", (long)gestureRecognizer.state);
}
}

- (void)alertView:(UIAlertView * )alertView clickedButtonAtIndex:(NSInteger)buttonIndex{
if (buttonIndex == 0 && alertView.tag == 70){
    NSLog(@"Yeah, %ld",(unsigned long)[_inputCell count]) ;
    [self.tableView beginUpdates];
    [_inputCell removeObjectAtIndex:rowToremove];

    [self.tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:theIndexPathe] withRowAnimation:UITableViewRowAnimationFade];
    [self.tableView endUpdates];

}
if (buttonIndex == 1 && alertView.tag == 70){
    NSLog(@"Annuler, %ld",(long)rowToremove);

}
}

@end

如果有人可以帮助我, 谢谢你的提前。

亲切的问候

4 个答案:

答案 0 :(得分:0)

在删除行之前,但在开始更新之后,您还应该更新数据源以返回(新)正确的行数。你试过吗?

答案 1 :(得分:0)

如果您想在任何特定单元格上执行操作,您应该在单元格中添加手势,而不是在tableview中实现手势

您的代码将是这样的

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    UITableViewCell *cell;
    long textFieldIndex = indexPath.row - [_menuData count];

    if(textFieldIndex>= 0 && textFieldIndex < _maxInputCell) {
        //Input cell
        cell = [tableView dequeueReusableCellWithIdentifier:@"textCell"];
        UITextField *textField;

        if(!_inputCell[textFieldIndex]) {
            _inputCell[textFieldIndex] = (UITextField *)[cell viewWithTag:1];
        }
        textField = _inputCell[textFieldIndex];
    } else {
        //Label cell
        cell = [tableView dequeueReusableCellWithIdentifier:@"thisCell"];
        cell.textLabel.text = [_menuData objectAtIndex:indexPath.row];
    }

    if(_checkFlag[indexPath]!=nil)
        cell.accessoryType = UITableViewCellAccessoryCheckmark;
    else  cell.accessoryType = UITableViewCellAccessoryNone;

    //****
    //Added gesture UILongPressGestureRecognizer here instead of tableview

    UILongPressGestureRecognizer *lpgr = [[UILongPressGestureRecognizer alloc]
                                          initWithTarget:self action:@selector(handleLongPress:)];
    lpgr.minimumPressDuration = 1.5; //seconds
    lpgr.delegate = self;
    [cell addGestureRecognizer:lpgr];

    return cell;
}

你可以像在旧代码中一样处理'handleLongPress'


注意:如果没有要求设置长按删除tableview有默认选项也删除所选行,这将是这样,滑动以获取删除选项

Default delete tableview

删除按钮的代码可以是这样的:

- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
    return YES;
}

- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
    [tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationLeft];
}

答案 2 :(得分:0)

好的,我发现了自己的错误。我必须在clickedButtonAtIndex方法中添加$.removeCookie('hotel-comparison'); 。菜鸟错了。感谢所有花时间帮助我的人。 问候。

答案 3 :(得分:-1)

我不确定你是否为allowEditing实现了UITableViewDelegate方法,如果你还没有实现它,请实现

override func tableView(tableView: UITableView, commitEditingStyle editingStyle: UITableViewCellEditingStyle, forRowAtIndexPath indexPath: NSIndexPath) {
    if editingStyle == UITableViewCellEditingStyle.Delete {
      numbers.removeAtIndex(indexPath.row)    
      tableView.deleteRowsAtIndexPaths([indexPath], withRowAnimation: UITableViewRowAnimation.Automatic)
    }
  }

在滑动时会带来删除选项,但当然您也可以为长按手势调用此委托方法。