gpt4 book ai didi

ios - 我有一个问题 "index 3 beyond bounds [0 .. 2]'“

转载 作者:塔克拉玛干 更新时间:2023-11-02 21:04:52 24 4
gpt4 key购买 nike

#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

我更改了“部分中的行数”并插入了新行但是问题“index 3 beyond bounds [0 .. 2] ”,我更改了“部分中的行数”没有增量,出现问题“原因:'尝试将第3行插入第0节,但更新后第0节中只有3行'”我仔细检查了一千次仍然找不到问题,帮助谁能够找到代码中的问题。

最佳答案

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;
}

关于ios - 我有一个问题 "index 3 beyond bounds [0 .. 2]'“,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38311394/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com