0

我已经尝试过多次创建表格视图并删除某些行,我什至在这里问过这个问题并实施了他们的建议,但甚至没有得到我的表格

视图控制器.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

视图控制器.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/

任何帮助深表感谢!先感谢您!:)

4

1 回答 1

1

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

此外,您应该删除您的覆盖,setEditing:animated:因为您是UIViewController子类而不是UITableView子类。只需拨打您的初始电话IBAction就足够了。

于 2013-10-26T18:16:53.343 回答