tableview crashed when press back button of navigation controller? - iphone

the numberOfRowsInSection returns 0 when i press navigationcontroller's back button,
but - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath is called when i put break point...application crashes ..any help please?

you should set the delegate of tableview to nil, dealloc before releasing your table view like this:
- (void)dealloc{
[myTableView setDelegate:nil];
[myTableView release];
myTableView = nil;
}
besides are you trying to reload the table in viewWillDisappear, if yes you should avoid it and find a workaround for this.
Hope this helps.

Try to make the numberOfRowsInSection method return 1
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return 1;
}

Related

How to Hide UITableView?

When pressing a button in my view controller it shows a UIView which has UISearchBar and UISearchDisplayController and UITableView in it(changes the view's hidden property to NO).
When I select a row in the table view I set the view's property to hidden=YES, but it doesn't hide the tableView. What can be the problem?
I implemented the UITableViewDelegate protocol and I receive tableView:didSelectRowAtIndexPath: message(I set the view's hidden=YES there).
Please note that when I call view's hidden=YES from searchBarCancelButtonClicked: it hides successfully.
I think that UISearchDisplayController is causing the problem.
Some code:
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
self.searchView.hidden = YES; // hide successfully the tableView.
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
self.searchView.hidden = YES; //doesn't hide the tableView.
}
EDIT: Found the problem, see my answer below. In short I added [self.searchDisplayController setActive:NO];
Found the problem - I needed to add [self.searchDisplayController setActive:NO]; and now it works.
Code:
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
[self.searchDisplayController setActive:NO]; //Added line of code.
self.searchView.hidden = YES; // Now it works!
}
Try this:
tableview.hidden = YES;
[self.yourview setHidden:Yes];
if the tableview is inside the view it should also hide

UITableview Reload issue iOS

some times reloadData method of UITableView is not working i.e means when i wrote like [tableView reloadData] then sometimes it is calling the datasource methods and sometimes not.
what might be the problem is, can anyone please give me the answer.
Here is my code:
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
// Return YES for supported orientations.
if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad){
[self performSelector:#selector(changeSegmentControllerFrame) withObject:nil afterDelay:0.0001];
[m_kwikiTable reloadData];
return YES; }
else
return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
Please add your codes.Any way check these things in your codes.
You should set cell.accessoryView out of if (cell == nil){}
2.In - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath you can use tableView and not use tableViewDistance (may be it is nil?)
3.In - (void)viewDidLoad you should call first [super viewDidLoad]; and then make customizations.
Go through this link, this will help you to debug:
tableView reloadData not working: cellForRowAtIndexPath not called
put nslog in delegate
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
before returning row count to check if it is retuning 0 or any other value
Try putting a breakpoint at
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
method... because if this returns zero then it will not bother executing the next method i.e.,
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath

Clearing TableView contents

Can any one help me by providing code for how to clear all the table view cell contents.
The tableview when a button is pressed gets reloaded but does not get cleared
Thanks
Rakesh
For clearing a tableView, you just remove all objects from the array that is populating your table and you reload the tableView after that.
[myArray removeAllObjects];
[self.tableView reloadData];
Set a bool tableIsEmpty and set the number of rows in the tableview to either 0 (table is empty) or the arrays count according to this BOOl in
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section;
Of cousre you have to set tableIsEmpty = YES, when the button is pressed and to NO again when theres data loaded.
Then call
[self.tableView reloadData];
Not tested but should work shouldnt it?
You can just add one variable as flag before reloading the table.
makeEmpty = YES;
[tableview reloadData];
makeEmpty = NO;
Then in the tableview delegate method
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
if(makeEmpty)
return 0;
}

How to highlight a row in a UITableView

The code below seems to have no effect. I want it to be highlighed in the same way it highlights when you tap on a row
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
...
[cell.textLabel setHighlighted:YES];
return cell;
}
This line will handle repainting the cell, label and accessory for you:
[tableView selectRowAtIndexPath:indexPath animated:NO scrollPosition:UITableViewScrollPositionNone];
As others have noted, you can programmatically select a cell using this method:
[tableView selectRowAtIndexPath:indexPath animated:NO scrollPosition:UITableViewScrollPositionNone];
However, you should be careful not to call this method too soon.
The first time that the view controller containing said tableView is initialized, the earliest that you should call this method is within viewDidAppear:.
So, you should do something like this:
- (void)viewDidAppear:(BOOL)animated
{
NSIndexPath *indexPath = [NSIndexPath indexPathForRow:0 inSection:0]; // set to whatever you want to be selected first
[tableView selectRowAtIndexPath:indexPath animated:NO scrollPosition:UITableViewScrollPositionNone];
}
If you try putting this call into viewDidLoad, viewWillAppear:, or any other early view lifecycle calls, it will likely not work or have odd results (such as scrolling to the correct position but not selecting the cell).
NSIndexPath *indexPath = [NSIndexPath indexPathForRow:1 inSection: 0];
[tableView selectRowAtIndexPath:indexPath animated:NO scrollPosition:UITableViewScrollPositionNone];
usually works
If you are using interface builder to build your interface, one thing to check is that you specified the view of the table view controller. If you don't actually specify the view, this selection message may go nowhere.
(you specify the view by selecting the table view controller in the interface builder inspector window and dragging the outlet for "view" to the table view you want to do the selection in).
- (BOOL)tableView:(UITableView *)tableView shouldHighlightRowAtIndexPath:(NSIndexPath *)indexPath {
return YES;
}
- (void)tableView:(UITableView *)tableView didHighlightRowAtIndexPath:(NSIndexPath *)indexPath {
// do something here
}
Try this :)
You can useĀ 
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
...
[cell.textLabel setHighlighted:YES];
return cell;
}
You have two choices. Either use the selection of the table view:
[tableView selectRowAtIndexPath: indexPath animated: YES scrollPosition: UITableViewScrollPositionNone];
Or change the background color of the table cell in the table view delegate:
- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath {
cell.backgroundColor = ...
}

Correct operation of Table View and Table Detail View

I need a small help. In my project there is a tab bar. On one of the tab bar items there is a navigation controller, on the view there is a table view.
For TableViewController there are the usual codes:
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return [myData count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
...
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
// here the DetailViewController is activated
}
The construction of DetailViewController is similar to the TableViewController. The data related to the selected row of TableView will be the key data for the SQL query, the result of which is then stored in an array. The only difference is that I use this:
viewWillAppear: includes and SQL query, then loads the result into the array sqlQueryResult.
- (void)viewWillDisappear:(BOOL)animated {
while( [sqlQueryResult count] >0 )
[sqlQueryResult removeObjectAtIndex:0];
}
This works the very first time when the table DetailView is shown. However, if we go back to TableView and select a different row, DetailView appears but the followings will not run again (as they did already): numberOfSectionsInTableView: or, tableView:(UITableView *)tableView numberOfRowsInSection: . I read in the docs that there are methods that are called only once - however I haven't seen such remark for these ones.
How can I "reset" the TableView? Or what methods should I call to set the number of its rows/sections?
What you're looking for I believe is:
[self.tableView reloadData];
You basically need to send a 'reloadData' message to your tableView. It will then call your methods again to refresh its contents.