用2个数组填充表

时间:2015-03-25 14:45:46

标签: objective-c iphone uitableview

我正在尝试使用来自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;
} 

2 个答案:

答案 0 :(得分:0)

答案:

您需要在-cellForRowAtIndexPath中区分 Book Journal

说明:

您对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.
}

答案 1 :(得分:0)

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

事实上你说你的桌子有&#34; table1.count + table2.count&#34;元素和每个单元格,您尝试使用当前行获取2个表中的元素。 例如,如果table1有2个项目,table2有5个项目 ,你的tableView将有7行和&#34; cellForRowAtIndexPath&#34;将被召唤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;
} 

您还可以更改订单以在图书之前显示日记帐。