1

我遇到了一个问题,我创建了一个UITableView用于CoreData在视图之间传递数据的方法(UITableViewto DetailViewof a row)我使用了这种方法:

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    if ([[segue identifier] isEqualToString:@"UpdateData"]) {
        NSManagedObject *selectedDevice = [self.datas objectAtIndex:
                                 [[self.tableView indexPathForSelectedRow] row]];
        DetailViewController *destViewController = segue.destinationViewController;
        destViewController.datas = selectedDevice;

     }
}

一切正常。但是后来我添加了一个UISearchBar不起作用的,NSManagedObject所以我创建了另一个NSMutableArray,我保存了所有的NSStrings作曲cell.textLabel.text并且它可以工作。但是现在我需要修改prepareForSegue,以便segue每当我从 SearchBar tableView 中选择一行时执行。

问题是要执行这种segueCoreData我需要使用的连接NSManagedObject,所以我的问题是:如何indexPath.rowUISearchBar我的内部获取UITableView并使其对应于我的右侧indexPath.rowself.data即我用来执行的数组中的正常segueUITableView见代码)?我以为我可以比较单元格的字符串(cell.textLabel.text)但是

  1. 不知道该怎么做。
  2. 如果我想有 2 行同名,可能会出现问题。

有什么建议吗?

编辑:我从情节提要中添加了 UISearchBar,这是我用来过滤主表视图的代码:

- (void)filterContentForSearchText:(NSString*)searchText scope:(NSString*)scope
{
    [_searchDati removeAllObjects];
    NSPredicate *resultPredicate = [NSPredicate predicateWithFormat:@"SELF contains[cd] %@", searchText];
    _searchDati = [NSMutableArray arrayWithArray:[_tableData filteredArrayUsingPredicate:resultPredicate]];
}

-(BOOL)searchDisplayController:(UISearchDisplayController *)controller
shouldReloadTableForSearchString:(NSString *)searchString
{
    [self filterContentForSearchText:searchString
                               scope:[[self.searchDisplayController.searchBar scopeButtonTitles]
                                      objectAtIndex:[self.searchDisplayController.searchBar
                                                     selectedScopeButtonIndex]]];

    return YES;
}

_searchDati我为 UISearchBar 创建的数组在哪里,并且_tableData是我创建的用于存储包含 CoreDatas 的 NSManagedObject 中的单元格名称的数组。

我还添加了这个

- (void)searchDisplayController:(UISearchDisplayController *)controller didLoadSearchResultsTableView:(UITableView *)tableView
{
    [tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"Cell"];
}

//Handle selection on searchBar
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (tableView == self.searchDisplayController.searchResultsTableView) {
        [self performSegueWithIdentifier: @"UpdateData" sender: self];
    }
}

第一个是因为我使用了这个,我需要注册课程或给我一个错误

static NSString *CellIdentifier = @"Cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];

第二个处理 UISearchBarTableView 的选择。

在这里我加载了我之前说的tableView

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


    if (tableView == self.searchDisplayController.searchResultsTableView) {
       // see filter method
        cell.textLabel.text = [_searchDati objectAtIndex:indexPath.row];

    } else {
        NSString *string;
        NSManagedObject *data = [self.datas objectAtIndex:indexPath.row];
        string = [NSString stringWithFormat:@"%@", [data valueForKey:@"name"]];
        [cell.detailTextLabel setText:nil];
        cell.textLabel.text = string;

        //store the string to the tableView array
        [_tableData addObject:string];
    }

    return cell;
}

编辑#2:

我写了这段代码

NSIndexPath *indexPath = nil; NSIndexPath *indexPath2 = nil;

if ([self.searchDisplayController isActive]) {

    indexPath = [self.searchDisplayController.searchResultsTableView indexPathForSelectedRow];

    for(int i = 0 ; i < _tableData.count ; i++){
        if([self.searchDati[indexPath.row] isEqualToString:_tableData[i]]){
            indexPath2 = [NSIndexPath indexPathForRow:i inSection:1];
        }
    }
    NSManagedObject *selectedDevice = [self.datas objectAtIndex:indexPath2.row];
    destViewController.datas = selectedDevice;

当我单击 UISearchBar 上的一行时,它仅第一次起作用!我进行搜索并单击该行,它会转到右侧的 DetailView,但如果我再次这样做,它会崩溃,给我这个错误

Terminating app due to uncaught exception 'NSRangeException', reason: '*** -[__NSArrayM objectAtIndex:]: index 5 beyond bounds [0 .. 4]'

我认为问题在于不是从 indexPath.row == 0 开始,而是在第一次之后创建了一堆。有什么建议吗?提前致谢 :)

4

1 回答 1

0

尝试这个,

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    if ([[segue identifier] isEqualToString:@"UpdateData"]) {
        NSIndexPath *indexPath = [self.tableView indexPathForCell:sender];
NSManagedObject *selectedDevice = [self.datas objectAtIndex:indexPath.row];
        DetailViewController * destViewController = [segue destinationViewController];
destViewController.datas = selectedDevice;

     }
}
于 2013-10-16T05:32:52.703 回答