iOS delete a tableview row

前端 未结 6 1198
北荒
北荒 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-04 00:07

    Try this sample tutorial,

    ViewController.h

    #import 
    
    @interface ViewController : UIViewController
    {
      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;
    }
    

提交回复
热议问题