UILabel text is not getting changed which is in another class - ios

I have two class
Timeline
Comment
in the Timeline class under every CustomTableViewCell represents a post and there is a option to comment on the post. To comment user has to visit the comment page which is comment class. When the user comments on it i wanted to pass that total comments count to Timeline page where it will display the comment numbers in the cell's comment label
So in my Comment class i did this when the comment is successfully posted:
TimelineViewController *t = (TimelineViewController *)self.navigationController.viewControllers[0];
t.updateWill = YES;
t.updateId = self.index;
t.updateValue =(int)self.response.responseData.count+1;
and in my Timeline class under cellForRowAtIndexPath i tried this:
if(self.updateWill == YES)
{
NSLog(#"YES YES");
data.commentCount= self.updateValue;
}
else
{
data.commentCount = (int)data.comments.count;
}
cell.commentLabel.text = [NSString stringWithFormat:#"%d comments",data.commentCount];
NSLog(#"YES YES") indicates that the logic worked. Beside if i print the self.updatevalue it also shows me the correct result but the label remain as unchanged. What i am doing wrong ??
Update:
on Timeline class at viewDidAppear
(void)viewDidAppear:(BOOL)animated
{
[[ApiAccess getSharedInstance] setDelegate:self];
[[[SocektAccess getSharedInstance]getSocket]setDelegate:self];
[[[SocektAccess getSharedInstance]getSocket] reconnect];
_chatSocket =[[SocektAccess getSharedInstance]getSocket];
if(self.updateWill)
{
NSLog(#"viewDidAppear");
NSIndexPath *indexPath = [NSIndexPath indexPathForRow:self.updateId inSection:0];
NSLog(#"Update Value: %d",self.updateValue);
NSLog(#"Update ID: %d",self.updateId);
TimelineTableViewCell *cell = (TimelineTableViewCell *)[self.tableData cellForRowAtIndexPath:indexPath];
WallPost *data = self.myObject[indexPath.row];
data.commentCount = self.updateValue;
[self.myObject replaceObjectAtIndex:indexPath.row withObject:data];
WallPost *data2 = self.myObject[indexPath.row];
NSLog(#": %d",data2.commentCount);
[self.tableData reloadRowsAtIndexPaths:[NSArray arrayWithObjects:indexPath, nil] withRowAnimation:UITableViewRowAnimationNone];
[self.tableData reloadData];
}
self.updateWill = NO;
}
And cellForRowAtIndexPath in Timeline class:
-(UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
TimelineTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:#"Cell" forIndexPath:indexPath];
self.counter = 0;
WallPost *data = self.myObject[indexPath.row];
cell.image.userInteractionEnabled = YES;
cell.image.tag = indexPath.row;
if(self.updateWill == YES)
{
NSLog(#"YES YES");
NSLog(#"Updated value : %d", (int)self.updateValue);
data.commentCount= (int)self.updateValue;
}
else
{
data.commentCount = (int)data.comments.count;
}

Related

UILabel under a custom tableview cell is not getting updated

My tableview cell is not getting updated on viewDidAppear :
-(void)viewDidAppear:(BOOL)animated
{
[[ApiAccess getSharedInstance] setDelegate:self];
[[[SocektAccess getSharedInstance]getSocket]setDelegate:self];
[[[SocektAccess getSharedInstance]getSocket] reconnect];
_chatSocket =[[SocektAccess getSharedInstance]getSocket];
if(self.updateWill)
{
NSLog(#"viewDidAppear");
NSIndexPath *indexPath = [NSIndexPath indexPathForRow:self.updateId inSection:0];
NSLog(#"Update Value: %d",self.updateValue);
NSLog(#"Update ID: %d",self.updateId);
TimelineTableViewCell *cell = (TimelineTableViewCell *)[self.tableData cellForRowAtIndexPath:indexPath];
WallPost *data = self.myObject[indexPath.row];
data.commentCount = self.updateValue;
[self.myObject replaceObjectAtIndex:indexPath.row withObject:data];
WallPost *data2 = self.myObject[indexPath.row];
NSLog(#": %d",data2.commentCount);
cell.commentLabel.text = #" ";
[self.tableData beginUpdates];
[self.tableData reloadRowsAtIndexPaths:[NSArray arrayWithObjects:indexPath, nil] withRowAnimation:UITableViewRowAnimationNone];
[self.tableData reloadData];
[self.tableData endUpdates];
}
self.updateWill = NO;
}
The self.updateWill is a boolean and has been triggered from another class :
if(self.responseAdd.responseStat.status)
{
self.commentTxt.text=#"";
[self getData];
TimelineViewController *t = (TimelineViewController *)self.navigationController.viewControllers[0];
t.updateWill = YES;
t.updateId = self.index;
t.updateValue =(int)self.response.responseData.count+1;
NSLog(#"response %d",(int)self.response.responseData.count);
}
as you can see i have created a object of my Main class which is TimelineViewController and has added the value of updateWill and other needed properties. But the cell is not getting reloaded!! whats i am doing wrong in here ??
update
(UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
TimelineTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:#"Cell" forIndexPath:indexPath];
self.counter = 0;
WallPost *data = self.myObject[indexPath.row];
cell.image.userInteractionEnabled = YES;
cell.image.tag = indexPath.row;
if(self.updateWill == YES)
{
NSLog(#"YES YES");
data.commentCount= (int)self.updateValue;
[self.tableData beginUpdates];
[self.tableData reloadRowsAtIndexPaths:[NSArray arrayWithObjects:indexPath, nil] withRowAnimation:UITableViewRowAnimationNone];
[self.tableData reloadData];
[self.tableData endUpdates];
cell.commentLabel.text = [NSString stringWithFormat:#"%d comments",data.commentCount];
}
else
{
data.commentCount = (int)data.comments.count;
cell.commentLabel.text = [NSString stringWithFormat:#"%d comments",data.commentCount];
}
This is what you need to workaround in objective c
-(void)viewDidAppear:(BOOL)animated
{
[super viewDidAppear:animated];
[self.tableData reloadData];
dispatch_async(dispatch_get_global_queue( DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^(void){
//Background Thread
dispatch_async(dispatch_get_main_queue(), ^(void){
//Run UI Updates
NSArray *paths = [self.tableData indexPathsForVisibleRows];
for (NSIndexPath *path in paths)
{
//get desired cell here
CustomCell *cell = (CustomCell *)[(UITableView *)self.view cellForRowAtIndexPath: path
];
cell.labeView //now you can update here on your cell items
}
});
});
}
and this is my original logic in Swift
override func viewDidAppear(animated: Bool)
{
super.viewDidAppear(animated)
tableView.reloadData()
dispatch_async(dispatch_get_main_queue(), { () -> Void in
//get visible cells indexpaths
if let indices = self.tableView.indexPathsForVisibleRows
{
for index in indices
{
if index.section == 0 //this is specific to me, checking if section 0 is visible
{
//get desired cell here
let cell = self.tableView.cellForRowAtIndexPath(NSIndexPath(forRow: 0,inSection: 0)) as! CustomCell
cell.myImageView.userInteractionEnabled = true //now i can update imageview on table cell
}
}
}
})
}
In viewDidLoad
[self.tableData reloadData];
I hope it will be useful.

UISearchbarController is returning a wrong indexpath?

Im working on a tableview with a searchBardisplayController but when I type a search this shows the correct results but when I select the cell shows me a different selection (such as if I select a cell when the tableview is normal, the normal indexpath)
So heres my code:
number of rows
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
// NSLog(#"%i", [[InfoWebDoctores sharedInstance]numberOfInfo]);
if (tableView == self.searchDisplayController.searchResultsTableView) {
return [displayObject count];
} else {
return [allObject count];
}
}
Configure the cell:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"docCell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if(!cell)
{
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:#"docCell"];
}
return cell;
}
Make the search:
- (BOOL)searchDisplayController:(UISearchDisplayController *)controller shouldReloadTableForSearchString:(NSString *)searchString
{
if([searchString length] == 0)
{
[displayObject removeAllObjects];
[displayObject addObjectsFromArray:allObject];
}
else
{
[displayObject removeAllObjects];
for(NSDictionary *tmpDict in allObject)
{
NSString *val = [tmpDict objectForKey:doctorname];
NSRange r = [val rangeOfString:searchString options:NSCaseInsensitiveSearch];
NSString *val2 = [tmpDict objectForKey:doctoresp];
NSRange r2 = [val2 rangeOfString:searchString options:NSCaseInsensitiveSearch];
if(r2.location != NSNotFound || r.location != NSNotFound)
{
[displayObject addObject:tmpDict];
}
}
}
return YES;
}
And finally sending the data to the detailViewcontroller (Ive tryied with push connection but when I select a searchresult Cell not even get the push view...)
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
DetailTableViewController *detail = [self.storyboard instantiateViewControllerWithIdentifier:#"detailController"];
NSIndexPath *index = [self.searchDisplayController.searchResultsTableView indexPathForSelectedRow];
//NSIndexPath *index = [self.tableView indexPathForSelectedRow];
int row = [index row];
EntryJsonDoctores *entry = [[InfoWebDoctores sharedInstance]entryAtIndex:row];
detail.modal = #[entry.nombre,entry.especialidad, entry.especialidad2, entry.especialidad3, entry.cel, entry.mail, entry.tel1, entry.tel2, entry.ext,entry.hospital,entry.direccion];
[self presentViewController:detail animated:YES completion:^{
}];
}
So Ive tryied with the indexpathforselectedrow from the tableview and the indexpath from searchresultscontroller, whats wrong? I need someones help please
If you set the search display controller's resultsTableView's delegate to this View Controller, then the results TableView will be looking for the didSelectRowAtIndexPath.
You can do this in the viewDidLoad and put self.searchDisplayController.searchResultsTableView.delegate = self
Replace NSIndexPath *index = [self.searchDisplayController.searchResultsTableView indexPathForSelectedRow]; with NSIndexPath *index = indexPath;

How to code PrepareForSeque in UITableView that has sections and searching

I have constructed a nice TableView that contains sections, searching, and the alphabetical strip on the right.
I tested it and all seemed well until I noticed that anything after the "A"s was not working correctly. I discovered the reason - in my PrepareForSegue I was passing the object I found at the row, but not paying any attention to the sections.
I searched stack overflow and found some things like I am trying to do, but it seems they are coding in the didSelectRowAtIndexPath, no prepare for segue. And I was unable to get any of the code to work.
My first question is what is the relationship between the two methods? I am using Storyboards so I think I should stick with the segues. But then how do I find out which employee object to pass?
Any help would be greatly appreciated.
Bryan
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPad) {
NSIndexPath *indexPath = [self.tableView indexPathForSelectedRow];
employee *dtlEmployee = [_employees objectAtIndex:indexPath.row];
self.detailViewController.detailItem = dtlEmployee;
}
else
{
// employee *dtlEmployee;
//
// int row = indexPath.row;
// int section = indexPath.section;
//
// NSIndexPath *newIndexPath = [NSIndexPath indexPathForRow:row inSection:section];
//
// [self.tableView deselectRowAtIndexPath:newIndexPath animated:NO];
// UITableViewCell *cell = [tableView cellForRowAtIndexPath:newIndexPath];
//
// dtlEmployee = [_employees objectAtIndex:newIndexPath];
}
}
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
if ([[segue identifier] isEqualToString:#"showDetail"]) {
employee *dtlEmployee;
// int row = [[self.tableView] indexPath.row];
// int section = [self.tableView indexPath.section];
// NSIndexPath *newIndexPath = [NSIndexPath indexPathForRow:row inSection:section];
// dtlEmployee = [_employees objectAtIndex:newIndexPath];
// NSIndexPath *selectedRowIndexPath = [self.tableView indexPathForSelectedRow];
// NSUInteger selectedRow = selectedRowIndexPath.row;
// NSUInteger selectedSection = selectedRowIndexPath.section;
// NSArray *tmpArray = [_employees objectAtIndex:[self.tableView indexPathForSelectedRow]];
// dtlEmployee = [tmpArray objectAtIndex:selectedRowIndexPath.row];
// dtlEmployee = [tmpArray objectAtIndex:indexPath.row];
//
// dtlEmployee = [_employees objectAtIndex:selectedRowIndexPath.]
//
if (isSearching)
{
dtlEmployee = [self.filteredEmployees objectAtIndex:self.searchDisplayController.searchResultsTableView.indexPathForSelectedRow.row];
}
else
{
dtlEmployee = [_employees objectAtIndex:[self.tableView indexPathForSelectedRow].row];
}
[[segue destinationViewController] setDetailItem:dtlEmployee];
}
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [self.tableView dequeueReusableCellWithIdentifier: CellIdentifier];
//cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];
}
if (isSearching)
{employee *thisEmployee = [self.filteredEmployees objectAtIndex:indexPath.row];
cell.textLabel.text = thisEmployee.fullName;}
else
{employee *thisEmployee = [[self.sections valueForKey:[[[self.sections allKeys] sortedArrayUsingSelector:#selector(localizedCaseInsensitiveCompare:)] objectAtIndex:indexPath.section]] objectAtIndex:indexPath.row];
cell.textLabel.text = thisEmployee.fullName;}
return cell;
}
Assuming that your segue is connected to the table view cell, your sender in prepareForSegue: is actually a cell. (Use logging or the debugger to be sure.)
That means you can ask your table view for an index path that matches the cell and use the same logic that you would have in tableView:cellForRowAtIndexPath: for obtaining an object from a path.
Your logic for getting the right employee in tableView:cellForRowAtIndexPath: looks like this:
if (isSearching)
{
employee *thisEmployee = [self.filteredEmployees objectAtIndex:indexPath.row];
cell.textLabel.text = thisEmployee.fullName;
}
else
{
employee *thisEmployee = [[self.sections valueForKey:[[[self.sections allKeys] sortedArrayUsingSelector:#selector(localizedCaseInsensitiveCompare:)] objectAtIndex:indexPath.section]] objectAtIndex:indexPath.row];
cell.textLabel.text = thisEmployee.fullName;
}
But your logic in prepareForSegue:sender: looks like this:
if (isSearching)
{
dtlEmployee = [self.filteredEmployees objectAtIndex:self.searchDisplayController.searchResultsTableView.indexPathForSelectedRow.row];
}
else
{
dtlEmployee = [_employees objectAtIndex:[self.tableView indexPathForSelectedRow].row];
}
You need to make the logic when performing the segue match the one when populating the cell, which from the sound of it works correctly. It appears that you're doing it correctly when isSearching is true, so when it's not true, the following line should be used:
employee *thisEmployee = [[self.sections valueForKey:[[[self.sections allKeys] sortedArrayUsingSelector:#selector(localizedCaseInsensitiveCompare:)] objectAtIndex: [self.tableView indexPathForSelectedRow].section]] objectAtIndex: [self.tableView indexPathForSelectedRow].row];

Getting the Row for Section indexPath in a tableView

Recently I put implemented an indexing feature for the user names in my app. I have been trying to access the rows in each section and add them to my recipients array when the user taps the index path. I have tried passing many different values in my objectAtIndex: method but none seem to be working. I am able to log the correct index row and section in indexPathForRow:inSection: method but the return value is an indexPath and not an integer. So something with my objectForIndex: method is obviously not right. Any help or references would be super. cheers!
This line:
PFUser *user = [self.friends objectAtIndex:indexPath.section];
Returns only the first username in each section, no matter what name I tap in the section. So I need to access the Row in addition, not just the section.
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
[self.tableView deselectRowAtIndexPath:indexPath animated:NO];
UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
int section = indexPath.section;
int row = indexPath.row;
NSIndexPath *newIndexPath = [NSIndexPath indexPathForRow:row inSection:section];
NSLog(#"newIndexPath: %#", newIndexPath);
PFUser *user = [self.friends objectAtIndex:indexPath.section];
if (cell.accessoryType == UITableViewCellAccessoryNone) {
cell.accessoryType = UITableViewCellAccessoryCheckmark;
[self.recipients addObject:user.objectId];
NSLog(#"added:%#",user);
}
else {
cell.accessoryType = UITableViewCellAccessoryNone;
[self.recipients removeObject:user.objectId];
NSLog(#"removed:%#",user);
}
[self.currentUser saveInBackgroundWithBlock:^(BOOL succeeded, NSError *error) {
if (error) {
NSLog(#"Error %# %#", error, [error userInfo]);
}
}];
}
Hi this is enough try and let me know if you face any issues...
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
[self.tableView deselectRowAtIndexPath:indexPath animated:NO];
NSArray * nameArray = [self.sectionsArray objectAtIndex:indexPath.section];
PFUser *user = (PFUser*)[nameArray objectAtIndex:indexPath.row];
// PFUser *user = (PFUser*)[self.friends objectAtIndex:indexPath.section];
if (cell.accessoryType == UITableViewCellAccessoryNone)
{
cell.accessoryType = UITableViewCellAccessoryCheckmark;
[self.recipients addObject:user.objectId];
NSLog(#"added:%#",user);
}
else {
cell.accessoryType = UITableViewCellAccessoryNone;
[self.recipients removeObject:user.objectId];
NSLog(#"removed:%#",user);
}
// this is enough
}
Just like the code Spynet suggested only a little bit different:
NSArray *array = [self.sectionsArray objectAtIndex:indexPath.section];
PFUser *user = [array objectAtIndex:indexPath.row];
Using that code I was able to get the proper indexPath for row in each section. Also, in order to get the cells accessory checkmarks to work correctly I had to create a newIndexPath using the section and row values for each index. My final code is here:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
int row = indexPath.row;
int section = indexPath.section;
NSIndexPath *newIndexPath = [NSIndexPath indexPathForRow:row inSection:section];
NSLog(#"newIndexPath: %#", newIndexPath);
[self.tableView deselectRowAtIndexPath:newIndexPath animated:NO];
UITableViewCell *cell = [tableView cellForRowAtIndexPath:newIndexPath];
NSLog(#"sectionsArray:%#",self.sectionsArray);
NSArray *array = [self.sectionsArray objectAtIndex:indexPath.section];
PFUser *user = [array objectAtIndex:indexPath.row];
NSLog(#"array:%#",array);
NSLog(#"user:%#",user);
if (cell.accessoryType == UITableViewCellAccessoryNone) {
cell.accessoryType = UITableViewCellAccessoryCheckmark;
[self.recipients addObject:user];
NSLog(#"recipients:%#",self.recipients);
}
else {
cell.accessoryType = UITableViewCellAccessoryNone;
[self.recipients removeObject:user];
NSLog(#"recipients:%#",self.recipients);
}
[self.currentUser saveInBackgroundWithBlock:^(BOOL succeeded, NSError *error) {
if (error) {
NSLog(#"Error %# %#", error, [error userInfo]);
}
}];
}

UIsearch bar not returning data to table

Edited code
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [self.tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell==nil)
cell=[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
if (isFiltered) {
int rowCount=indexPath.row;
Aves *filtrada=[filteredTableData objectAtIndex:rowCount];
cell.textLabel.text=filtrada.name;
NSLog(#"mostrando: ");
}else {
int rowCounter=indexPath.row;
Aves *author=[theauthors objectAtIndex:rowCounter];
cell.textLabel.text=author.name;
}
NSLog(#"mostrando: ");
return cell;
}
-(void)searchBar:(UISearchBar*)searchBar textDidChange:(NSString*)text
{
if(text.length == 0)
{
isFiltered = FALSE;
}
else
{
isFiltered = true;
int i;
[filteredTableData removeAllObjects];
for(i=0;[theauthors count]>i;i++)
{
Aves *name=[theauthors objectAtIndex:i];
//NSLog(name.name);
NSRange nameRange = [[name.name lowercaseString] rangeOfString:[text lowercaseString]];
if(nameRange.length>0)
{
[filteredTableData addObject:name];
NSLog(name.name);
}
}
[self.tableView performSelectorOnMainThread:#selector(reloadData) withObject:nil waitUntilDone:NO];
}
}
Edit: After working on it a while I solved some problems.Just updated my code, the problem is the repaint of the tableView, every thing else go ok. Check it and give any ideas you have plz ^^
Thx again for your time.
I assume you're using prototype cells? I just had a similar problem in one of my projects.
When search results are displayed and tableView:cellForRowAtIndexPath: is called, the table view passed in the the table belonging to the search results controller, not your main table view. Problem with that is, the search results table view doesn't know anything about your table's prototype cells, so dequeueReusableCellWithIdentifier: returns nil. But just alloc/init'ing a UITableCellView won't give you one of your prototype cells, so whatever UI you laid out in your storyboard isn't there.
The fix is easy: in tableView:cellForRowAtIndexPath:, don't call dequeueReusableCellWithIdentifier: on the tableview passed in; just call it on your main table view. So basically, just change this:
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell==nil)
cell=[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
to this:
UITableViewCell *cell = [self.tableView dequeueReusableCellWithIdentifier:CellIdentifier];
There's no need for the nil check; Apple's Storyboards Release Notes says:
The dequeueReusableCellWithIdentifier: method is guaranteed to return
a cell (provided that you have defined a cell with the given
identifier). Thus there is no need to use the “check the return value
of the method” pattern as was the case in the previous typical
implementation of tableView:cellForRowAtIndexPath:.
does your app hits this code if(nameRange.location ==0) ?
Change the code piece of
else
{
isFiltered = true;
int i=0;
[filteredTableData removeAllObjects];
filteredTableData = [[NSMutableArray alloc] init];
//for (Aves* name in theauthors)
for(i=0;[theauthors count]>i;i++)
{
Aves *name=[theauthors objectAtIndex:i];
NSRange nameRange = [name.foodName rangeOfString:text ];
if(nameRange.location ==0)
{
[filteredTableData addObject:name];
}
[self.tableView reloadData];
}
}
to
else
{
isFiltered = true;
int i=0;
[filteredTableData removeAllObjects];
//filteredTableData = [[NSMutableArray alloc] init]; not needed
//for (Aves* name in theauthors)
for(i=0;[theauthors count]>i;i++)
{
Aves *name=[theauthors objectAtIndex:i];
NSRange nameRange = [name.foodName rangeOfString:text ];
if(nameRange.location != NSNotFound)
{
[filteredTableData addObject:name];
}
}
[self.tableView reloadData];
}
use this:
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
//If the requesting table view is the search display controller's table view, return the count of the filtered list, otherwise return the count of the main list.
if (isFiltered)
{
return [filteredTableData count];
}
else
{
return [theauthors count];
}
}
Replace method:
-(void)searchBar:(UISearchBar*)searchBar textDidChange:(NSString*)text
{
if(text.length == 0)
{
isFiltered = FALSE;
}
else
{
isFiltered = true;
int i=0;
[filteredTableData removeAllObjects];
filteredTableData = [[NSMutableArray alloc] init];
//for (Aves* name in theauthors)
for(i=0;[theauthors count]>i;i++)
{
Aves *name=[theauthors objectAtIndex:i];
NSRange nameRange = [[name.foodName lowercaseString] rangeOfString:[text lowercaseString]];
if(nameRange.length>0)
{
[filteredTableData addObject:name];
[self.tableView reloadData];
}
}
}
}
finally fixed it. Here is my working code, thx you all =)
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [self.tableView dequeueReusableCellWithIdentifier:CellIdentifier];
cell=[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault
reuseIdentifier:CellIdentifier];
if (isFiltered==TRUE) {
int rowCount=indexPath.row;
//for (rowCount=0; rowCount<[filteredTableData count]; rowCount++) {
Aves *filtrada=[filteredTableData objectAtIndex:rowCount];
cell.textLabel.text=filtrada.name;
//}
}else if(isFiltered==FALSE)
{
int rowCounter=indexPath.row;
Aves *author=[theauthors objectAtIndex:rowCounter];
cell.textLabel.text=author.name;
}
return cell;
}
-(void)searchBar:(UISearchBar*)searchBar textDidChange:(NSString*)text {
[filteredTableData removeAllObjects];
filteredTableData=[[NSMutableArray alloc]init ];
if(text.length == 0)
{
isFiltered = FALSE;
}
else
{
isFiltered = TRUE;
int i;
for(i=0;[theauthors count]>i;i++)
{
Aves * filtrado=[[Aves alloc]init];
filtrado=[theauthors objectAtIndex:i];
//NSLog(filtrado.name);
NSRange nameRange = [[filtrado.name lowercaseString] rangeOfString:[text lowercaseString]];
if(nameRange.length>0)
{
[filteredTableData addObject:filtrado];
}
}
[self.tableView performSelectorOnMainThread:#selector(reloadData) withObject:nil
waitUntilDone:NO];
}
}
You probably want to replace
UITableViewCell *cell = [self.tableView dequeueReusableCellWithIdentifier:CellIdentifier];
cell=[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault
reuseIdentifier:CellIdentifier];
with in your cellForRowAtIndexPath
UITableViewCell *cell=[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault
reuseIdentifier:CellIdentifier];
The reason because you're not getting use of the dequeued cell anyway.

Resources