UITableView不进入编辑模式

时间:2013-10-26 18:03:41

标签: objective-c uitableview

我已多次尝试创建表格视图并删除某些行,我甚至在此之前提出了这个问题,并实施了他们建议的内容但却未能获得我的表格

ViewController.h

   @interface XYZViewController : UIViewController<UITableViewDataSource,UITableViewDelegate>

        @property (strong, nonatomic) UITableView *myTable;
        @property (strong, nonatomic) NSMutableArray *names;
        @property (weak, nonatomic) IBOutlet UIBarButtonItem *editButton;

    - (IBAction)editMyTable:(id)sender;

    @end

ViewController.m

@implementation XYZViewController

@synthesize names, myTable, editButton;

- (void)viewDidLoad
{
    [super viewDidLoad];

    //input values into the mutable array
    names = [[NSMutableArray alloc]initWithObjects:

             @"Bob",
             @"Chris",
             @"Tom"

              , nil];

    editButton = self.editButtonItem;

}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{

    return 1;

}

- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{

    return @"Friends";

}

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

    return [self.names count];

}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{

    //create an identifier
    static NSString *identifier;

    //create the cell with the identifier
    UITableViewCell *cell = [myTable dequeueReusableCellWithIdentifier:identifier];

    //check if cell is nil
    if (cell == nil) {

        //assign cell
        cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier];

    }

    //assign the names of the array to each cell
    cell.textLabel.text = [names objectAtIndex:indexPath.row];

    return cell;

}

- (IBAction)editMyTable:(id)sender
{

    [editButton setTitle:@"Done"];
    [myTable setEditing:YES animated:YES];

}

- (void)setEditing:(BOOL)editing animated:(BOOL)animated {

    [super setEditing:editing animated:animated];
    [myTable setEditing:editing animated:animated];

}

- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath
{

    return YES;

}

- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{

    if (editingStyle == UITableViewCellEditingStyleDelete) {

        //remove from array
        [names removeObjectAtIndex:indexPath.row];

        //remove from tableView
        [myTable deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];

    }

}

- (BOOL)tableView:(UITableView *)tableView shouldIndentWhileEditingRowAtIndexPath:(NSIndexPath *)indexPath
{
    return NO;
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end

图片:http://postimg.org/image/g9wqwuo6v/

非常感谢任何帮助!先感谢您! :)

1 个答案:

答案 0 :(得分:1)

您的问题似乎是您没有连接myTable财产。它不是IBOutlet,您的代码中没有任何地方建立连接。当您致电[myTable setEditing:YES animated:YES];时,它会将其发送到nil表。您可以在调用编辑方法之前打印myTable的值来验证这一点:NSLog(@"%@", myTable);

此外,您应该删除覆盖setEditing:animated:,因为您是UIViewController子类而不是UITableView子类。只需在IBAction中进行初始调用即可。

相关问题