iOS delete a tableview row

前端 未结 6 1186
北荒
北荒 2020-12-03 23:14

I had my app working fine on iOS 6. With the upgrade I stopped being able to delete rows from my UITableView.

I\'ve a button in my prototype cell.

相关标签:
6条回答
  • 2020-12-03 23:50

    Use the tableview commiteditingstyle method to delete a cell/row of the tableviewcontroller.

    Here is the workable code snip:

    -(void) tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
    
    {
    
        [ArrayHoldsCellObj removeObjectAtIndex:indexPath.row];
    
        [tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationAutomatic];
    }
    

    Note that the ArrayHoldsCellObj is the Array object you must declare and assign each cell to the area index.

    0 讨论(0)
  • 2020-12-03 23:54
    - (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
    if (editingStyle == UITableViewCellEditingStyleDelete) {
    //add code here for when you hit delete
    [dataSourceArray removeObjectAtIndex:indexPath.row];
    [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationAutomatic];
    
    }    
    }
    
    0 讨论(0)
  • 2020-12-03 23:57

    You can set the button tag = cell.index in cellForRowAtIndexPath.

    And then

    UITableViewCell *cell = (UITableViewCell *)
            [tableView cellForRowAtIndexPath:
            [NSIndexPath indexPathForRow:btn.tag inSection:0]];
    

    so you can get the index

    0 讨论(0)
  • 2020-12-03 23:58

    To show delete button on cell swipe you must implement this delegate method.

    - (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
        // Return YES if you want the specified item to be editable.
        return YES;
    }
    

    Try this, i hope this will work for you.

    0 讨论(0)
  • 2020-12-04 00:07

    Try this sample tutorial,

    ViewController.h

    #import <UIKit/UIKit.h>
    
    @interface ViewController : UIViewController<UITableViewDelegate,UITableViewDataSource>
    {
      NSMutableArray *arryData1;
      NSMutableArray *arryData2;
      IBOutlet UITableView *tableList;
    }
    
    @end
    

    ViewController.m

    #import "ViewController.h"
    
    @interface ViewController ()
    
     @end
    
    @implementation ViewController
    
    -(void)viewDidLoad
    {
    [super viewDidLoad];
    
    arryData1 = [[NSMutableArray alloc] initWithObjects:@"MCA",@"MBA",@"BTech",@"MTech",nil];
    arryData2 = [[NSMutableArray alloc] initWithObjects:@"Objective C",@"C++",@"C#",@".net",nil];
    tableList.editing=YES;
    
    }
    
    -(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
    {
    return 1;
    }
    
    -(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
    {
    return [arryData1 count];
    }
    
    -(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
    {
    static NSString *CellIdentifier= @"Cell";
    
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil)
    {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:CellIdentifier] ;
    }
    cell.textLabel.text = [arryData1 objectAtIndex:indexPath.row];
    cell.detailTextLabel.text = [arryData2 objectAtIndex:indexPath.row];
    return cell;
    }
    
    - (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
    
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        int index = indexPath.row;
        [arryData1 removeObjectAtIndex:index];
        [arryData2 removeObjectAtIndex:index];
    
        [tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath]
                         withRowAnimation:UITableViewRowAnimationFade];
    
    
    }
    }
    
    - (UITableViewCellEditingStyle)tableView:(UITableView *)aTableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath
    {
    if (tableList.editing)
    {
        return UITableViewCellEditingStyleDelete;
    }
    
    return UITableViewCellEditingStyleNone;
    }
    
    - (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath
    {
    return YES;
    }
    
    0 讨论(0)
  • 2020-12-04 00:10

    I quess the problem is you're not reloading data again thus it's staying in cache memory

    You can try to reload data with [tableView reloadData]; this method to the below of [_tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationRight]; this code line in your second solution.

    0 讨论(0)
提交回复
热议问题