在TableView中按“编辑”按钮时无法启动自编辑模式

时间:2011-10-24 12:45:13

标签: ios objective-c iphone uitableview

我正在尝试在iPhone App中创建可编辑的表格。我有两个问题。

  1. 当我按下编辑按钮进入编辑模式时,我无法删除一行。

  2. 如果有办法添加“添加新单元格”功能而不是我在代码中的方式? (我添加了一个addButtonAction方法。)

  3. 这是我的.h

    #import <UIKit/UIKit.h>
    
    @interface FirstViewController : UITableViewController <UITableViewDelegate, UITableViewDataSource>
    {
        UITableView *table;
        NSMutableArray *tableDataSource;
    }
    
    -(IBAction)addButtonAction:(id)sender;
    
    @end
    

    这是.m

    #import "FirstViewController.h"
    
    @implementation FirstViewController
    
    - (IBAction)addButtonAction:(id)sender
    {
        if(tableDataSource != nil)
        {
            [tableDataSource addObject:@"New City"];
        }
        [table reloadData];
    }
    
    - (id)initWithStyle:(UITableViewStyle)style
    {
        self = [super initWithStyle:style];
        if (self) {
            // Custom initialization
        }
        return self;
    }
    
    - (void)didReceiveMemoryWarning
    {
        // Releases the view if it doesn't have a superview.
        [super didReceiveMemoryWarning];
    
        // Release any cached data, images, etc that aren't in use.
    }
    
    #pragma mark - View lifecycle
    
    - (void)viewDidLoad
    {
        [super viewDidLoad];
    
        tableDataSource = [[NSMutableArray alloc]init];
        [tableDataSource addObjectsFromArray:[NSArray arrayWithObjects: @"Canada", @"United States", @"Australia", @"United Kingdom", nil]];
    
        table = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, 320, 367) style:UITableViewStyleGrouped];
        [table setDataSource:self];
        [table setDelegate:self];
        [self.view addSubview:table];
    
        [[self navigationItem] setLeftBarButtonItem:[self editButtonItem]];
    
        UIBarButtonItem *addButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAdd
                                  target:self
                                  action:@selector(addButtonAction:)];
        [[self navigationItem] setRightBarButtonItem:addButton];
    }
    
    - (void)viewDidUnload
    {
        [super viewDidUnload];
        // Release any retained subviews of the main view.
        // e.g. self.myOutlet = nil;
    }
    
    - (void)viewWillAppear:(BOOL)animated
    {
        [super viewWillAppear:animated];
        [[self tableView] reloadData];
    }
    
    - (void)viewDidAppear:(BOOL)animated
    {
        [super viewDidAppear:animated];
    }
    
    - (void)viewWillDisappear:(BOOL)animated
    {
        [super viewWillDisappear:animated];
    }
    
    - (void)viewDidDisappear:(BOOL)animated
    {
        [super viewDidDisappear:animated];
    }
    
    - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
    {
        // Return YES for supported orientations
        return (interfaceOrientation == UIInterfaceOrientationPortrait);
    }
    
    #pragma mark - Table view data source
    
    - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
    {
        return 1;
    }
    
    - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
    {
        return [tableDataSource count];
    }
    
    - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
    {
        static NSString *CellIdentifier = @"Cell";
    
        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
        if (cell == nil) {
            cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
        }
    
        // Configure the cell...
        cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
        cell.textLabel.text = [tableDataSource objectAtIndex:indexPath.row];
    
        return cell;
    }
    
    #pragma - Enable/Disable Edit Button
    
    - (void)setEditing:(BOOL)editing animated:(BOOL)animated
    {
        [super setEditing:editing
                 animated:animated];
    
        [table setEditing:editing animated:animated];
    }
    
    #pragma - Editing Style
    
    // Override to support conditional editing of the table view.
    - (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath
    {
       return YES;
    }
    
    - (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
    {
        if (editingStyle == UITableViewCellEditingStyleDelete) 
        {
            // Delete the row from the data source
            [tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
        }   
        else if (editingStyle == UITableViewCellEditingStyleInsert) 
        {
            // Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view
        }   
    }
    
    #pragma - Can Move Row
    
    // Override to support rearranging the table view.
    - (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath
    {
        NSString *item = [tableDataSource objectAtIndex:fromIndexPath.row];
        [tableDataSource removeObject:item];
        [tableDataSource insertObject:item atIndex:toIndexPath.row];
    }
    
    // Override to support conditional rearranging of the table view.
    - (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath
    {
        return YES;
    }
    
    #pragma mark - Table Title
    
    - (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
    {
        if(section == 0)
        {
            return @"City";
        }
    
        else 
        {
            return @"Nothing here";
        }
    }
    
    #pragma mark - Table view delegate
    
    - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
    {
        if (indexPath.section == 0)
        {
            //TBD....
        }
    
        [tableView deselectRowAtIndexPath:indexPath animated:YES];
    }
    
    @end
    

1 个答案:

答案 0 :(得分:0)

  1. 确保从tableView中删除行也从NSArray中删除对象。
  2. 这个addButton I thik可以工作但是这个

    if(cell == nil){         cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];     }

  3. 这样做:

    cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];

    只要删除,如果有statament ...... 希望我理解你的问题蚂蚁这个帮助。