我有一个问题"指数3超出界限[0 .. 2]' "

时间:2016-07-11 16:01:39

标签: ios objective-c uitableview

#import "MasterTableViewController.h"

@interface MasterTableViewController ()

@end

@implementation MasterTableViewController

- (void)viewDidLoad {
    [super viewDidLoad];

    self.navigationItem.rightBarButtonItem = self.editButtonItem;
    [self.tableView setDelegate:self];
    [self.tableView setDataSource:self];
}

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];

    [self.tableView reloadData];
}

#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return [self isEditing] ? self.wishListItems.count + 1 : self.wishListItems.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Cell" forIndexPath:indexPath];

    WishListItem *item = self.wishListItems[indexPath.row];

    if (indexPath.row >= [self.wishListItems count] && self.tableView.isEditing) {
        cell.textLabel.text = @"New subject";
        cell.imageView.image = nil;
    } else {
        cell.textLabel.text = item.name;
        cell.imageView.image = item.photo;
    }
    return cell;
}

- (void)setEditing:(BOOL)editing animated:(BOOL)animated {
    [super setEditing:editing animated:animated];

    if (editing) {
        [self.tableView beginUpdates];
        [self.tableView insertRowsAtIndexPaths:@[[NSIndexPath indexPathForRow:self.wishListItems.count inSection:0]] withRowAnimation:UITableViewRowAnimationAutomatic];
        [self.tableView endUpdates];
    } else {
        [self.tableView beginUpdates];
        [self.tableView deleteRowsAtIndexPaths:@[[NSIndexPath indexPathForRow:self.wishListItems.count inSection:0]] withRowAnimation:UITableViewRowAnimationAutomatic];
        [self.tableView endUpdates];
    }
}

- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        [self.wishListItems removeObjectAtIndex:indexPath.row];
        [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationAutomatic];
        [self.tableView reloadData];
    } else if (editingStyle == UITableViewCellEditingStyleInsert) {
        WishListItem *newItem = [[WishListItem alloc] initWithName:@"New subject" photo:nil price:0.0 andNotes:@"Empty"];
        [self.wishListItems insertObject:newItem atIndex:indexPath.row];
        [tableView insertRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationAutomatic];
        [self.tableView reloadData];
    }
}

- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.row >= [self.wishListItems count]) {
        return UITableViewCellEditingStyleInsert;
    } else {
        return UITableViewCellEditingStyleDelete;
    }
}

//- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
//    [self.tableView deselectRowAtIndexPath:indexPath animated:true];
//    if (indexPath.row >= self.wishListItems.count && self.editing) {
//        [self tableView:tableView commitEditingStyle:UITableViewCellEditingStyleInsert forRowAtIndexPath:indexPath];
//    }
//}

#pragma mark - Navigation

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    if ([[segue identifier] isEqualToString:@"showDetail"]) {
        NSIndexPath *indexPath = [self.tableView indexPathForSelectedRow];
        WishListItem *item = self.wishListItems[indexPath.row];
        DetailViewController *detailViewController = (DetailViewController *)segue.destinationViewController;
        detailViewController.item = item;
    }
}


@end

我改变了"第34节中的行数;并插入新行但问题 "索引3超出界限[0 .. 2]",我改变了#34;部分行数#34; 没有增量,出现问题"原因:'尝试将第3行插入第0部分,但在更新后第0部分只有3行' " 我仔细检查了一千次仍然找不到问题,帮助谁能够找到代码中的问题。

1 个答案:

答案 0 :(得分:1)

cellForRowAtIndexPath:中,您在进行边界检查之前访问阵列,因此在您的表格进行编辑时会崩溃。

仅在边界检查后执行数组访问:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Cell" forIndexPath:indexPath];

    if (indexPath.row >= [self.wishListItems count] && self.tableView.isEditing) {
        cell.textLabel.text = @"New subject";
        cell.imageView.image = nil;
    } else {
        WishListItem *item = self.wishListItems[indexPath.row];
        cell.textLabel.text = item.name;
        cell.imageView.image = item.photo;
    }
    return cell;
}