Button action is not happen for particular index when search on UITableView
I implemented search display controller to UITableView in xib. When i
search, table view functionality is not working on search.
Here my code i did..
//to set itemcount zero for all items initially
- (void)setAllElementofArrayToZero
{
for(int i = 0;i < itemsarry ;i++)
{
[array addObject:[NSNumber numberWithInteger:0]];
}
}
//table view delegate methods
- (NSInteger) numberOfSectionsInTableView:(UITableView *)tableView
{
return 1;
}
- (NSInteger) tableView:(UITableView *)tableView
numberOfRowsInSection:(NSInteger)section
{
if (tableView==self.searchDisplayController.searchResultsTableView) {
return [searcharray count];
} else {
return [itemsarry count];
}
}
- (CGFloat)tableView:(UITableView *)tableView
heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
return 75;
}
- (UITableViewCell *) tableView:(UITableView *)tableView
cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *cellIdentifier = @"Cell";
UITableViewCell *cell = [tableView
dequeueReusableCellWithIdentifier:cellIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc]
initWithStyle:UITableViewCellStyleDefault
reuseIdentifier:cellIdentifier];
UILabel *NameLbl =[[UILabel alloc]init];
[NameLbl setFrame:CGRectMake(0, 2, 110, 59)];
[NameLbl setTextColor:[UIColor blackColor]];
[NameLbl setBackgroundColor:[UIColor clearColor]];
[NameLbl setTextAlignment:NSTextAlignmentLeft];
[NameLbl setFont:[UIFont boldSystemFontOfSize:15.0]];
NameLbl.lineBreakMode=NSLineBreakByWordWrapping;
NameLbl.numberOfLines=3;
NameLbl.tag=2;
[cell.contentView NameLbl];
UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(2, 60, 110,
12)];
[label setFont:[UIFont fontWithName:@"Arial-BoldMT" size:14]];
label.tag = 1;
label.backgroundColor = [UIColor clearColor];
label.numberOfLines = 0;
[cell.contentView addSubview:label];
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
[button setTitle:@"add" forState:UIControlStateNormal];
//set the position of the button
[button setFrame:CGRectMake(190, 0, 35, 60)];
[button addTarget:self action:@selector(increaseItemCount:)
forControlEvents:UIControlEventTouchUpInside];
button.backgroundColor= [UIColor redColor];
[cell.contentView addSubview:button];
}
UILabel *label = (UILabel *)[cell viewWithTag:1];
UILabel *NameLbl = (UILabel *)[cell viewWithTag:2];
if (tableView==self.searchDisplayController.searchResultsTableView) {
NameLbl.text=[[searcharray
objectAtIndex:indexPath.row]objectForKey:@"name"];
} else {
NameLbl.text= [[itemsarry objectAtIndex:indexPath.row]
objectForKey:@"name"];
}
label.text = [NSString stringWithFormat:@"%i",[[array
objectAtIndex:indexPath.row] intValue]];
return cell;
}
//increase count on button action
-(void)increaseItemCount:(UIButton *)sender
{
UITableViewCell *cell = (UITableViewCell *)sender.superview.superview;
NSIndexPath *path = [table indexPathForCell:cell];
[array replaceObjectAtIndex:path.row withObject:[NSNumber
numberWithInteger:[[array objectAtIndex:path.row] intValue]+1 ]];
[table reloadData];
}
// search implement
-(void) filterForSearchText:(NSString *) text scope:(NSString *) scope
{
[searcharray removeAllObjects];
NSPredicate *filterPredicate = [NSPredicate
predicateWithFormat:@"SELF.name contains[c] %@",text];
searcharray = [NSMutableArray arrayWithArray:[itemsarry
filteredArrayUsingPredicate:filterPredicate]];
}
-(BOOL) searchDisplayController:(UISearchDisplayController *)controller
shouldReloadTableForSearchString:(NSString *)searchString
{
[self filterForSearchText:searchString scope:[[[[self
searchDisplayController] searchBar] scopeButtonTitles]
objectAtIndex:[[[self searchDisplayController] searchBar]
selectedScopeButtonIndex] ]];
return YES;
}
-(BOOL) searchDisplayController:(UISearchDisplayController
*)controller shouldReloadTableForSearchScope:(NSInteger)searchOption
{
[self
filterForSearchText:self.searchDisplayController.searchBar.text
scope:
[[self.searchDisplayController.searchBar scopeButtonTitles]
objectAtIndex:searchOption]];
return YES;
}
//in ViewDidLoad method
searcharray = [[NSMutableArray alloc]initWithCapacity:[itemsarry
count]];
The above code is working fine for table view. When search same funtion is
not happen on search.
Any suggestions or code.
No comments:
Post a Comment