【问题标题】:Populating table with 2 arrays用 2 个数组填充表
【发布时间】:2015-03-25 14:45:45
【问题描述】:

我正在尝试使用来自 2 个不同数组的对象填充 tableview 控制器,但它崩溃并出现此错误 “由于未捕获的异常'NSRangeException'而终止应用程序,原因:' - [__NSArrayM objectAtIndex:]:索引1超出范围[0 .. 0]”***

我该如何解决这个问题?以下是我的代码:

(void)viewDidAppear:(BOOL)animated{
//[super viewDidAppear:<#animated#>];

NSManagedObjectContext  *bookmanagedObjectContext = [self managedObjectContext];
NSFetchRequest  *bookfetchRequest = [[NSFetchRequest alloc] initWithEntityName:@"Book"];
NSPredicate *bookpredicate =[NSPredicate predicateWithFormat:@" bookref.toproject contains[cd]%@",self.projectdb];
[bookfetchRequest setPredicate:bookpredicate];
NSSortDescriptor *booksortDescriptor = [[NSSortDescriptor alloc]initWithKey:@"authorSurname" ascending:YES];
NSArray *booksortDescriptors = [[NSArray alloc]initWithObjects:booksortDescriptor, nil];
[bookfetchRequest setSortDescriptors:booksortDescriptors];

self.BookrefArray = [[bookmanagedObjectContext executeFetchRequest:bookfetchRequest error:nil] mutableCopy];

NSManagedObjectContext  *journalmanagedObjectContext = [self managedObjectContext];
NSFetchRequest  *journalfetchRequest = [[NSFetchRequest alloc] initWithEntityName:@"Journal"];
NSPredicate *journalpredicate =[NSPredicate predicateWithFormat:@" journalref.toproj contains[cd]%@",self.projectdb];
[journalfetchRequest setPredicate:journalpredicate];
NSSortDescriptor *journalsortDescriptor = [[NSSortDescriptor alloc]initWithKey:@"surname" ascending:YES];
NSArray *journalsortDescriptors = [[NSArray alloc]initWithObjects:journalsortDescriptor, nil];
[journalfetchRequest setSortDescriptors:journalsortDescriptors];

self.JournalrefArray = [[journalmanagedObjectContext executeFetchRequest:journalfetchRequest error:nil] mutableCopy];
[self.tableView reloadData];}

(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
 #warning Potentially incomplete method implementation.
  // Return the number of sections.
  return 1;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
#warning Incomplete method implementation.
// Return the number of rows in the section.
return (self.BookrefArray.count + self.journalrefArray.count);
}



-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    static NSString *CellIdentifier = @"Cells";
    UITableViewCell *cell = [tableView  dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
    Journal *myjournal =[self.journalrefArray objectAtIndex:indexPath.row];
    [cell.detailTextLabel setText:[myjournal valueForKey:@"journalname"]];
    [cell.textLabel setText:[NSString stringWithFormat:@"%@, %@",[myjournal valueForKey:@"surname"],[myjournal valueForKey:@"firstname"]]];
    Book *mybook =[self.BookrefArray objectAtIndex:indexPath.row];
    // Configure the cell...
    [cell.detailTextLabel setText:[mybook valueForKey:@"bookTitle"]];
    [cell.textLabel setText:[NSString stringWithFormat:@"%@, %@",[mybook valueForKey:@"authorSurname"],[mybook valueForKey:@"authorOthernames"]]];

    return cell;
} 

【问题讨论】:

  • 您需要提供有关您的数组的更多详细信息 - 它们在哪里初始化?
  • 我编辑了上面的代码

标签: objective-c iphone uitableview


【解决方案1】:

您遇到此错误是因为您尝试访问不存在的数组元素。

事实上,您说您的表格具有“table1.count + table2.count”元素,并且对于每个单元格,您都尝试在 2 个表格中使用当前行获取一个元素。 例如,如果 table1 有 2 个项目,table2 有 5 个项目 ,您的 tableView 将有 7 行,“cellForRowAtIndexPath”将被调用 7 次。因此,对于索引 3,您将收到此错误,因为您的 table1 只有 2 个项目。

要解决这个问题,你应该得到一本书或一本期刊在这一行的功能。 例如,您可以尝试这样的任何事情:

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

  if (indexPath.row < self.BookrefArray.count)
  {
    Book *mybook =[self.BookrefArray objectAtIndex:indexPath.row];
    // Configure your cell with a book
  }
  else
  {
    Journal *myjournal =[self.journalrefArray objectAtIndex:indexPath.row - self.BookrefArray.count];
    // Configure your cell with a journal
  }

  return cell;
} 

您还可以更改顺序以在书籍之前显示期刊。

【讨论】:

  • 感谢代码有效...但是我如何让它允许删除 BookrefArray...使用 (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath ???
  • 在commitEditingStyle函数中使用相同的方法,“if (indexPath.row
【解决方案2】:

答案:

你需要区分-cellForRowAtIndexPath中的BookJournal

说明:

您对 tableview 的 -cellForRowAtIndexPath 的调用将取决于您从 tableview 的 -numberOfRowsInSection 方法返回的数字。

因此,例如,您在 BookrefArray 中有 3 个对象,在 journalrefArray 中有 2 个对象,您的tableview 的 -numberOfRowsInSection 方法将返回 5,这意味着 -cellForRowAtIndexPath 将被调用 5 次。

让我们来看看这条线:

Journal *myjournal =[self.journalrefArray objectAtIndex:indexPath.row];

在这里,它会在一种情况下获得 indexPath.row = 5,而您的 journalrefArray 仅包含 2 个对象。所以,你得到了"index .. beyond bounds" 错误。

更新:

您可以简单地将两个数组合并为一个。

[documentsArray addObjectsFromArray:self.BookrefArray];
[documentsArray addObjectsFromArray:self.journalrefArray];

然后在-cellForRowAtIndexPath,你可以这样做:

id document = [documentsArray objectAtIndex:indexPath.row];
if ([document isKindOfClass:Book])
{
    Book *mybook = (Book *)document;
    // Do something with mybook.
}
else
{
    Journal *myjournal = (Journal *)document;
    // Do something with myjournal.
}

【讨论】:

  • 问题是对象的数量取决于输入...并且输入的数量没有限制...并且 indexPath.row = 5 不起作用...
猜你喜欢
  • 2021-08-09
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-01-09
  • 1970-01-01
  • 2020-03-30
  • 2021-09-22
相关资源
最近更新 更多