本文介绍了在表视图之间传递数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 29岁程序员,3月因学历无情被辞! 我有一个问题,我创建了一个 问题是要执行这种 segue 与 CoreData 我需要使用 NSManagedObject ,所以我的问题是:我如何获得 indexPath.row 里面的 UISearchBar 里面,使它对应于正确的 indexPath.row of my self.data (这是我用来执行正常 segue 在 UITableView ,看代码)? 我想我可以比较单元格的字符串(cell.textLabel.text),但 不知道如何 > 我添加了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 创建的数组i是为存储来自包含CoreDatas的NSManagedObject的单元格名称而创建的数组。 我也添加了这个 - (void)searchDisplayController:(UISearchDisplayController *)controller didLoadSearchResultsTableView :( UITableView *)tableView { [tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@Cell]; } //在searchBar上处理选择 - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { if(tableView == self.searchDisplayController.searchResultsTableView){ [self performSegueWithIdentifier:@UpdateDatasender: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){ //查看过滤器方法 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; //将字符串存储到tableView数组 [_tableData addObject:string]; } return cell; } EDIT#2: b $ b 我编写此代码 NSIndexPath * indexPath = nil; NSIndexPath * indexPath2 = nil; if([self.searchDisplayController isActive]){ indexPath = [self.searchDisplayController.searchResultsTableView indexPathForSelectedRow]; for(int i = 0; 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,但如果我再做它崩溃给我这个错误 由于未捕获的异常NSRangeException而终止应用程序,原因:'*** - [__ NSArrayM objectAtIndex:]:index 5超出范围[0 .. 4]' pre> 我认为问题是,不从indexPath.row == 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; } } I've got a problem, I created a UITableView using CoreData to pass data between the views (UITableView to DetailView of a row) I used this method:- (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; }}and everything works fine. But later I added a UISearchBar that didn't work with NSManagedObject so i created another NSMutableArray where I saved all the NSStrings composing the cell.textLabel.textand it works. But now I need to modify the prepareForSegue in order to perform a segue whenever I select a row from the SearchBar tableView.The problem is that to perform this kind of segue connecting with the CoreData I need to use the NSManagedObject, so my question is:How can i get the indexPath.row from the UISearchBar inside my UITableView and make it correspond to the right indexPath.row of my self.data (that is the array I used to perform the normal segue in the UITableView, see code) ?I thought I could compare the strings of the cell (cell.textLabel.text) butdon't know how to do.there could be a problem if there are 2 rows with same name I suppose.any advice?EDIT: I added the UISearchBar from the storyboard and this is the code i use to filter the main table view:- (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 *)controllershouldReloadTableForSearchString:(NSString *)searchString{ [self filterContentForSearchText:searchString scope:[[self.searchDisplayController.searchBar scopeButtonTitles] objectAtIndex:[self.searchDisplayController.searchBar selectedScopeButtonIndex]]]; return YES;}Where _searchDati is the array i created for the UISearchBar and _tableData is the array i created to store the name of the cells from the NSManagedObject that contains the CoreDatas.and i also added this- (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]; }}The first because i used this and i need to register the class or gave me an errorstatic NSString *CellIdentifier = @"Cell"; UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];and the second one to handle the selection of the UISearchBarTableView.In here i load the tableView as i said before- (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;}EDIT#2:i Wrote this codeNSIndexPath *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;when i click on a row on the UISearchBar and it WORKS only for the first time! I make the search and click on the row and it goes to the right DetailView, but if i do it again it crashes giving me this errorTerminating app due to uncaught exception 'NSRangeException', reason: '*** -[__NSArrayM objectAtIndex:]: index 5 beyond bounds [0 .. 4]'i think the problem is that does not start from the indexPath.row == 0 but create a sort of pile after the first time.Any suggestion? Thanks in advance :) 解决方案 Try this,- (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; }} 这篇关于在表视图之间传递数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云!
08-13 03:15