IndexPath.row在使用搜索问题时会发生更改



我正试图在我的UITableview中成功地实现一个UISearch控制器,但我遇到了以下问题,我使用indexPath.row方法将数据传递给下一个视图控制器,当我搜索时indexPath.rw会发生变化,其次,我在表视图中使用多个数组来处理我的数据,因此,我所做的是创建另一个数组,它将所有其他数组的内容保存在一个数组中,并在其中进行搜索,除了当我想传递数据时,它都能完美地工作,因为indexPath.row会发生变化,而不是停留在单元格中(这是我需要做的)。请你检查一下我的代码,看看你能不能帮忙?

#import "AbsViewController.h"
@interface AbsViewController ()
@end
@implementation AbsViewController {
//these are all my arrays
//The arrays that carry for no equipment.
NSArray *tableData;
NSArray *thumbnails;
NSArray *sets;
NSArray *reps;
NSArray *instructions;
NSArray *materials;
NSArray *status;
NSArray *tips;
NSArray *difficulty;
NSArray *target;
//The arrays that carry for equipment.
NSArray *tableData1;
NSArray *thumbnails1;
NSArray *sets1;
NSArray *reps1;
NSArray *instructions1;
NSArray *materials1;
NSArray *status1;
NSArray *tips1;
NSArray *difficulty1;
NSArray *target1;
//The arrays that carry for bosu ball.
NSArray *tableData2;
NSArray *thumbnails2;
NSArray *sets2;
NSArray *reps2;
NSArray *instructions2;
NSArray *materials2;
NSArray *status2;
NSArray *tips2;
NSArray *difficulty2;
NSArray *target2;
//The arrays that carry for physio ball.
NSArray *tableData3;
NSArray *thumbnails3;
NSArray *sets3;
NSArray *reps3;
NSArray *instructions3;
NSArray *materials3;
NSArray *status3;
NSArray *tips3;
NSArray *difficulty3;
NSArray *target3;
//The arrays that carry for weighted.
NSArray *tableData4;
NSArray *thumbnails4;
NSArray *sets4;
NSArray *reps4;
NSArray *instructions4;
NSArray *materials4;
NSArray *status4;
NSArray *tips4;
NSArray *difficulty4;
NSArray *target4;
//Search array
NSArray *tableData5;
NSArray *status5;
NSArray *thumbnails5;
//The array for the search results.
NSArray *searchResults;
}

- (void)viewDidLoad
{
[super viewDidLoad];
self.title = @"Abdominal";
//Here im initializing my arrays.
// Find out the path of my array.
NSString *path = [[NSBundle mainBundle] pathForResource:@"Abs" ofType:@"plist"];
// Load the file content and read the data into arrays
NSDictionary *dict = [[NSDictionary alloc] initWithContentsOfFile:path];
tableData = [dict objectForKey:@"name"];
thumbnails = [dict objectForKey:@"imageFile"];
status = [dict objectForKey:@"status"];
// Find out the path of recipes.plist
NSString *path1 = [[NSBundle mainBundle] pathForResource:@"Abs1" ofType:@"plist"];
// Load the file content and read the data into arrays
NSDictionary *dict1 = [[NSDictionary alloc] initWithContentsOfFile:path1];
tableData1 = [dict1 objectForKey:@"name"];
thumbnails1 = [dict1 objectForKey:@"imageFile"];
status1 = [dict1 objectForKey:@"status"];
// Find out the path of recipes.plist
NSString *path2 = [[NSBundle mainBundle] pathForResource:@"Abs2" ofType:@"plist"];
// Load the file content and read the data into arrays
NSDictionary *dict2 = [[NSDictionary alloc] initWithContentsOfFile:path2];
tableData2 = [dict2 objectForKey:@"name"];
thumbnails2 = [dict2 objectForKey:@"imageFile"];
status2 = [dict2 objectForKey:@"status"];

// Find out the path of recipes.plist
NSString *path3 = [[NSBundle mainBundle] pathForResource:@"Abs3" ofType:@"plist"];
// Load the file content and read the data into arrays
NSDictionary *dict3 = [[NSDictionary alloc] initWithContentsOfFile:path3];
tableData3 = [dict3 objectForKey:@"name"];
thumbnails3 = [dict3 objectForKey:@"imageFile"];
status3 = [dict3 objectForKey:@"status"];
NSString *path4 = [[NSBundle mainBundle] pathForResource:@"Abs4" ofType:@"plist"];
// Load the file content and read the data into arrays
NSDictionary *dict4 = [[NSDictionary alloc] initWithContentsOfFile:path4];
tableData4 = [dict4 objectForKey:@"name"];
thumbnails4 = [dict4 objectForKey:@"imageFile"];
status4 = [dict4 objectForKey:@"status"];

//this is the array that carries all the content.
NSString *path5 = [[NSBundle mainBundle] pathForResource:@"AbsTotal" ofType:@"plist"];
// Load the file content and read the data into arrays
NSDictionary *dict5 = [[NSDictionary alloc] initWithContentsOfFile:path5];
tableData5 = [dict5 objectForKey:@"name"];
status5 = [dict5 objectForKey:@"status"];
thumbnails5 = [dict5 objectForKey:@"thumbnails"];

}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
if (self.searchDisplayController.isActive) {
    return 1;
}
return 5 ;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
if (tableView == self.searchDisplayController.searchResultsTableView) {
    return [searchResults count];
}
else if (section == 0) {
    return [tableData count];
}
else if (section == 1) {
    return [tableData1 count];
}
else if (section == 2) {
    return [tableData2 count];
}
else if (section == 3) {
    return [tableData3 count];
}
else if (section == 4) {
    return [tableData4 count];
}
else {
    return [tableData5 count];
}
}
//this is my cellForRowAtIndexPath method.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *simpleTableIdentifier = @"ExerciseCell";
 UITableViewCell *cell = (UITableViewCell *)[tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];
if (cell == nil)
{
    cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle   reuseIdentifier:simpleTableIdentifier];
}
if (tableView == self.searchDisplayController.searchResultsTableView) {
    cell.textLabel.text = [searchResults objectAtIndex:indexPath.row];
}
else if (indexPath.section == 0) {
    cell.textLabel.text = [tableData objectAtIndex:indexPath.row];
    cell.detailTextLabel.text = [NSString stringWithFormat:@"Type: %@", [status objectAtIndex:indexPath.row]];
    cell.imageView.image = [UIImage imageNamed:[thumbnails objectAtIndex:indexPath.row]];
}
else if (indexPath.section == 1) {
    cell.textLabel.text = [tableData1 objectAtIndex:indexPath.row];
    cell.detailTextLabel.text = [NSString stringWithFormat:@"Type: %@", [status1 objectAtIndex:indexPath.row]];
    cell.imageView.image = [UIImage imageNamed:[thumbnails1 objectAtIndex:indexPath.row]];
}
else if (indexPath.section == 2) {
    cell.textLabel.text = [tableData2 objectAtIndex:indexPath.row];
    cell.detailTextLabel.text = [NSString stringWithFormat:@"Type: %@", [status2 objectAtIndex:indexPath.row]];
    cell.imageView.image = [UIImage imageNamed:[thumbnails2 objectAtIndex:indexPath.row]];
}
else if (indexPath.section == 3) {
    cell.textLabel.text = [tableData3 objectAtIndex:indexPath.row];
    cell.detailTextLabel.text = [NSString stringWithFormat:@"Type: %@", [status3 objectAtIndex:indexPath.row]];
    cell.imageView.image = [UIImage imageNamed:[thumbnails3 objectAtIndex:indexPath.row]];
}
else if (indexPath.section == 4) {
    cell.textLabel.text = [tableData4 objectAtIndex:indexPath.row];
    cell.detailTextLabel.text = [NSString stringWithFormat:@"Type: %@", [status4 objectAtIndex:indexPath.row]];
    cell.imageView.image = [UIImage imageNamed:[thumbnails4 objectAtIndex:indexPath.row]];
}
else {
    cell.textLabel.text = [tableData objectAtIndex:indexPath.row];
    cell.detailTextLabel.text = [NSString stringWithFormat:@"Type: %@", [status    objectAtIndex:indexPath.row]];
    cell.imageView.image = [UIImage imageNamed:[thumbnails objectAtIndex:indexPath.row]];
}

return cell;
}
//this is where i've attempted to fix the issue but didn't work.
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath   *)indexPath {
NSInteger rowNumber = 0;
for (NSInteger i = 0; i < indexPath.section; i++) {
    rowNumber += [self tableView:tableView numberOfRowsInSection:i];
}
rowNumber += indexPath.row;
NSLog(@"%ld",(long)rowNumber);

}
//this is where i filter my tableview
- (void)filterContentForSearchText:(NSString*)searchText scope:(NSString*)scope
{
NSPredicate *resultPredicate = [NSPredicate predicateWithFormat:@"SELF contains[cd] %@", searchText];
searchResults = [tableData5 filteredArrayUsingPredicate:resultPredicate];
}
-(BOOL)searchDisplayController:(UISearchDisplayController *)controller
shouldReloadTableForSearchString:(NSString *)searchString
{
[self filterContentForSearchText:searchString
                           scope:[[self.searchDisplayController.searchBar   scopeButtonTitles]
                                  objectAtIndex:[self.searchDisplayController.searchBar
                                                 selectedScopeButtonIndex]]];

return YES;
}
//Here i create my view for my header/
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
// create the parent view that will hold header Label
UIView* customView = [[UIView alloc] initWithFrame:CGRectMake(0,0,320,25)];
customView.backgroundColor = [UIColor belizeHoleColor];
UILabel *headerLabel = [[UILabel alloc] initWithFrame:CGRectZero];
headerLabel.backgroundColor = [UIColor belizeHoleColor];
headerLabel.font = [UIFont fontWithName:@"Avenir-Black" size:14];
headerLabel.frame = CGRectMake(6,0,320,25);
if (section == 1) {
    headerLabel.text =  @"";
}
if (section == 2) {
    headerLabel.text =  @"";
}
if (section == 3) {
    headerLabel.text =  @"";
}
if (section == 4) {
    headerLabel.text =  @"";
}
if (section == 0) {
    headerLabel.text =  @"";
}
headerLabel.textColor = [UIColor cloudsColor];
[customView addSubview:headerLabel];
return customView;
}

如果有人能帮忙,我将不胜感激。提前谢谢。

  //u can do this on - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath   *)indexPath method first get 
    if (tableView == self.searchDisplayController.searchResultsTableView)
    {
      NSString *PassData = [tableData5 objectAtIndex:indexPath.row];
  //    NSString *passData = nil; //COMMENT THIS LINE SINCE IT CONTAINS SELECTED VALUE FROM SEARCH DISPLAY TABLEVIEW NO NEED TO BE ST NULL AGAIN "COMMENT THIS LINE AND TRY"
      NSString *selectedStatus = nil;
      UIImage *selectedThumbImage = nil;
  //assuming ur array's contains equal number of objects
      if([tableData containsObject:passData]) //if the search results contains an object in this array
  {
    //by using passData from search results tableview's tapped row u cn get all the below grouped tableview data now do for other array's 
    int index = [tableData indexOfObject:passData]; //get the index of selected array
    selectedStatus = [status objectAtIndex:index];  //from that index get the status data
    selectedThumbImage  = [thumbnails objectAtIndex:index];//get thumbnil image from index
  }
else if ([tableData1 containsObject:passData])
 {
    int index = [tableData1 indexOfObject:passData]; //get the index of selected array
    selectedStatus = [status1 objectAtIndex:index];  //from that index get the status data
    selectedThumbImage  = [thumbnails1 objectAtIndex:index];//get thumbnil image from index
}
else if ([tableData2 containsObject:passData])
{
    int index = [tableData2 indexOfObject:passData]; //get the index of selected array
    selectedStatus = [status2 objectAtIndex:index];  //from that index get the status data
    selectedThumbImage  = [thumbnails2 objectAtIndex:index];//get thumbnil image from index
 }
 else if (//check for otehr remaining array)
 {
  //grab all details
 }
 //at the end your passData contains title , selectedStatus contains status , and selectedThumbImage contains thumb image use it to pass 
}//end  if (tableView == self.searchDisplayController.searchResultsTableView)

最新更新