【问题标题】:Fetched results in a section crash获取的结果导致部分崩溃
【发布时间】:2012-07-06 15:54:21
【问题描述】:

我正在使用核心数据来获取实体。我只希望这些结果显示在我的表格视图的第二部分中,而其他内容显示在另一部分中......我的应用程序没有崩溃,但获取的数据没有显示在表格视图中......我也很确定我正在正确获取数据。

这是一些代码。

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

static NSString *CellIdentifier = @"Cell";

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
    cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];
}

if (indexPath.section==0){
    switch (indexPath.row) {
        case 0:
            cell.textLabel.text = _team.teamName;
            break;
        case 1:
            cell.textLabel.text = _team.headCoach;
            break;
        default:
            break;
    }

}

if (indexPath.section ==1) {
    Player *p = [_fetchedResultsController objectAtIndexPath: indexPath];
    cell.textLabel.text = p.firstName;
    cell.detailTextLabel.text = p.team.teamName;
}       

return cell;

}

【问题讨论】:

  • FRC 的 if/then 位于第一部分的 if/then 内。
  • 好的,我编辑了它,但现在我再次遇到同样的错误 index 1 越界 [0 .. 0]'
  • 在下面查看我的编辑。问题在于将 indexPath 与 FRC 的 objectAtIndexPath 方法一起使用
  • 非常感谢。非常感谢您抽出时间来帮助我...一切顺利。

标签: core-data nsfetchedresultscontroller nsindexpath


【解决方案1】:

有几个问题,首先您应该只有一个部分,因此您不需要访问部分属性。所以试试这个

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    switch(section){
        case 0:
            return 7;
        case 1:
            return [self.fetchedResultsController.fetchedObjects count];
    }
    return 0;
}

其次,您在使用以下代码时遇到了问题:

Player *p =[_fetchedResultsController objectAtIndexPath: indexPath];

这是导致问题的原因,因为您在两个部分都调用它,而您的 fetch 只有一个部分。

要修复崩溃,请使用检查正确 indexPath.section 的条件对其进行包装,或者将其放在第 1 部分的 switch/case 语句中。您可以执行以下操作:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

    static NSString *CellIdentifier = @"Cell";

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];
    }

    if (indexPath.section==0){
        switch (indexPath.row) {
        case 0:
            cell.textLabel.text = _team.teamName;
            break;
        case 1:
            cell.textLabel.text = _team.headCoach;
            break;
        default:
            break;
        }

    }else{
        Player *p = [self.fetchedResultsController.fetchedObjects objectAtIndex: indexPath.row];
        cell.textLabel.text = p.firstName;
        cell.detailTextLabel.text = p.team.teamName;
    }       

    return cell;

}

祝你好运

T

【讨论】:

  • 是的,我知道那里正在发生崩溃...修复它的最佳方法是什么?
  • 我有点不确定该怎么做...你能提供一些示例代码吗?
  • 是的,我已经尝试过这样做,但我仍然收到错误 -[__NSArrayM objectAtIndex:]: index 1 beyond bounds [0 .. 0]'
  • 您在实现 numberOfRowsInSection 方法时遇到问题。见编辑。
  • 好的,感谢您的帮助。我更改了我的代码,查看我的问题中的编辑...该应用程序不再崩溃,但播放器不再显示在该部分中。
猜你喜欢
  • 2019-12-24
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-02-04
相关资源
最近更新 更多