0

성공적으로 UISearchcontroller 내 UITableview 구현하려면 노력하고 있지만 다음 문제가 있어요, 다음보기 컨트롤러를 indexPath.row 메서드를 사용하여 데이터를 전달하고 검색 할 때 indexPath.row 변경 두 번째로 여러 배열을 사용하고 있습니다. 내 tableview에 내 데이터에 대한, 그리고 내가 한 일은 다른 모든 배열 내용을 보유하고 그것을 통해 검색하는 다른 배열을 만들고, 그것은 indexPath 때문에 내 데이터를 전달하려는 경우를 제외하고는 모두 완벽하게 작동합니다. 행을 변경하는 대신 셀을 유지하는 것입니다. 제 코드를 확인하고 도움을받을 수 있는지 확인하십시오.검색 문제를 사용할 때 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는 검색 결과 테이블에 도청

+0

U 말은, indexpath는 –

+0

이 @Shan 네 그게 전부는 groupedtableview 다른 – user2600115

+0

변화가 일어나는 것은 유 searchdisplaycontroller의의 tableview에 도청 있기 때문이다 뭐죠 –

답변

0
//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) 
+0

this searchResults는 선언되지 않은 식별자이기 때문에 제대로 작동하지 않습니다. 그런 다음 어떻게 이것을 사용하여 데이터를 전달합니까? – user2600115

+0

ok 죄송합니다. 조금 혼란 스럽네요. –

+0

U가 말했습니다. 모든 배열의 객체를 포함하는 배열을 말합니다. –