gpt4 book ai didi

objective-c - 部分而不是行导致崩溃

转载 作者:行者123 更新时间:2023-11-29 11:05:13 25 4
gpt4 key购买 nike

我想在我的 TableView 单元格之间留一些间距,所以我将我的 TableView 代码更改为:

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
// Return the number of sections.
return [_boxs count];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
// Return the number of rows in the section.
return 1;
}

我的问题是我的“添加行\对象”代码崩溃了。我应该对此代码进行哪些更改?

有问题的代码:

SomeClass *newObject = [[SomeClass alloc]initWithTitle:@".....  
[_boxs addObject:newObject];
NSIndexPath *indexPath = [NSIndexPath indexPathForRow:_boxs.count-1 inSection:0];
NSArray *indexPaths = [NSArray arrayWithObject:indexPath];
[self.tableView insertRowsAtIndexPaths:indexPaths withRowAnimation:NO];
[self.tableView selectRowAtIndexPath:indexPath animated:NO scrollPosition:UITableViewScrollPositionMiddle];
[self performSegueWithIdentifier:@"FirstDetailsSegue" sender:self ];

-boxs 是一个 NSMutableArray。

这是我收到的错误:* 由于未捕获的异常“NSInternalInconsistencyException”而终止应用程序,原因:“尝试将第 7 行插入第 0 节,但该节中只有 1 行0更新后'

最佳答案

您已经在创建 NSIndexPath 的行中切换了行和列。您需要将代码修改为,

NSIndexPath *indexPath = [NSIndexPath indexPathForRow:0 inSection:_boxs.count-1];

如果您打算只使用一个部分和多行,那么您应该更改委托(delegate)方法以仅返回 1 个部分和 [_boxs count] 行。在那种情况下,您的代码会起作用。

根据您的评论,您似乎想要插入一个新部分而不是一行。试试这个,

[self.tableView insertSections:[NSIndexSet indexSetWithIndex:_boxs.count-1] withRowAnimation:UITableViewRowAnimationAutomatic];

您可以从代码中删除 insertRow 行。

关于objective-c - 部分而不是行导致崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13960576/

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