UITableView -> numberOfRowsInSection:(NSInteger)section returns wrong value - iphone

My 2nd time today... But here are so many good developers...
Hi,
I have some troubles with my tableView... If the view will appear my table reloads its data, but it doesn't update the numbersOfRowsInSection...
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
NSLog(#"COUNTER: %i",[content count]);
return [content count];
}
The log says the first time I load the page the right value. But if I add an object and reload the data, this function isn't calling.
Is there anyone who know the solution?
Thanks,
mavrick3.
EDIT:
Heres how I call [table reloadData]
- (void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];
if ([content count] != 0) {
self.content = [[[[NSArray alloc] initWithContentsOfFile:[FileManager filePath]] objectAtIndex:row_] objectForKey:#"faecher"];
[table reloadData];
NSLog(#"----%#",content);
}
}
And yes, the dataSource and delegate are connected and they are both implemented in my header.

If you call reloadData on a UITableview object and you're it's dataSource then this method should be called called.
Are you sure that you have assigned your table view to your property (i.e. dragged it across in interface builder?)
I suspect that you have only made your controller the dataSource of your table view (hence the first time it loads it gets the value). After that your calls to reloadData won't work because the property in your view controller is still nil.
You can test this by putting an NSLog just before you call reloadData - if it outputs nil then you've not connected it.
NSLog(#"%#", myTable);
[myTable reloadData];

any compiler warnings?
Is "table" actually an instance of UITableView?

Related

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;
}

problem in reloading tableview

HI all
i am using
- (void)viewWillAppear:(BOOL)animated {
[super viewWillAppear:animated];
[tableview reloadData];////not working
}
but my table is not reloading.but when i put
[tableview reloadData];
in viewdidload, my data didnt show up.
BUt on rerunning the app, whole data shown up...
i m confused ,what is happeneing here.plz suggest me a proper way to reload table.
Try
[self.tableView reloadData];
check that you have set the delegates properly
In
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return [datas count];
}
Make sure that you update the mutable array "datas" with the values.Because only with reference to this array count table cells are created.
All the best.

cellForRowAtIndexPath not called; sections returns 1 and rows returns 4

After parsing JSON data in a Data class, I set the UIViewController's NSArray *headlines property in a fillArrays method of the same Data class. In the viewDidAppear method of my UIViewController, I call reloadData on my UITableView. numberOfSectionsInTableView fires and returns 1, then numberOfRowsInSection fires and returns an array count of 4 (for 4 strings in the array). However, control never gets to cellForRowAtIndexPath and I'm having the hardest time understanding why, especially since I have valid sections and rows. The cells are all visible.
I've added the UITableViewDataSource and UITableViewDelegate protocols to the UIViewController interface and set the UITableView's delegate and dataSource to self in viewDidLoad (which also is verified by the row and section count methods being called).
I'm wondering if it has something to with me reinitializing the UIViewController in Data.m in order to set its properties.
In Data.m:
- (void)fillArrays:(NSArray *)jsonObjs {
NSLog(#"fillArrays");
HeadlinesRootViewController *hrvc = [[HeadlinesRootViewController alloc] init];
hrvc.headlines = [self getJsonValuesForKey:#"headline" inArrayOfObjects:jsonObjs];
[hrvc viewDidAppear:NO];
}
In ViewController.m:
- (void)viewDidLoad {
[super viewDidLoad];
NSLog(#"viewDidLoad");
// Table view
headlineTableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 180, self.view.bounds.size.width, 300) style:UITableViewStylePlain];
[headlineTableView setDelegate:self];
[headlineTableView setDataSource:self];
// Temporary
self.headlines = [[NSMutableArray alloc] initWithObjects:#"headline1", #"headline2", #"headline3", #"headline4", nil];
[self.view addSubview:headlineTableView];
self.headlineTableView = headlineTableView;
[headlineTableView release];
}
- (void)viewDidAppear:(BOOL)animated {
[super viewDidAppear:animated];
NSLog(#"viewdidappear");
NSLog(#"headlines: %#", self.headlines); // Returns an array of 4 headlines
if( [self.headlines count] != 0 ){
[self.headlineTableView reloadData];
}
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
NSLog(#"numberOfSectionsInTableView: 1");
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
NSLog(#"numberOfRowsInSection: %d", [self.headlines count]);
return [self.headlines count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
NSLog(#"cellForRowAtIndexPath");
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:CellIdentifier] autorelease];
}
cell.text = [[NSString alloc] initWithFormat:#"%#", [self.headlines objectAtIndex:indexPath.row]];
return cell;
}
In fillArrays, you create another view controller - but you never do anything with it or its view, you would never see that view. You would never call viewDidAppear manually either, that happens automatically when a view controllers view is displayed (ONLY in the context of a navigation controller though).
Normally the flow is, you create a view controller and either add that view as a subview of a current view, or push it as a new window via a navigation controller. I'm pretty sure your whole issue is that they table is never added to a view anyone actually sees, so the table calls the other methods but never calls cellForRow because its layoutSubviews code is simply not being called.
Have you added your tableView to the view of UIViewController?
It happened to me, and when I added this
[self.view addSubview:table];
[table release];
then cellForRowAtIndexPath started working.
For Google's sake:
If tableView:numberOfRowsInSection returns zero for whatever reason tableView:cellForRowAtIndexPath will not get called because there are no rows to call it for.
Check to make sure that the tableView delegate and dataSource are pointed to the viewController
I cannot see anything wrong with the code as-is, have you verified with breakpoints that cellForRow is never reached (even though I see you have a log statement)?
Also I would try just for a sanity check to return "1" explicitly in rowsInSection, and hardcode a string in the cell you are returning in cellForRow.
If all else fails, create a new table view controller from the XCode templates and put your calls in there - then when that works, work backwards to why your code does not.
Also, it would be good to see your viewDidLoad setup code (add to answer above please).
if you're setting the delegate and datasource at viewDidLoad, then that may be the source of your bug. Can you set the datasource and delegate in init?
I'm not sure that you add your UITableView as subview to UIViewController.view. This was my approach anyway.
In this approach, I found execution did not get into cellForRowAtIndexPath until I sent UIViewController.view to the back after adding UITableView as subview.
Getting this far was only part of the problem. At this point, it seemed that my other view controllers no longer respond to touch events. I found that when I also add the UITableView as a subview to the rootViewController, all my views got the appropriate touch events.
Thank you so much pxl. When I move the UITableView initialization from viewDidLoad to:
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil"
it works perfectly when ever I make delete or update some rows the UITableView gets reloded to my UIView.
Swift version
Add self.table.layoutIfNeeded() and then self.tableView.reloadData()

TableView not displaying parsed data from xml?

i have tabbar controller, first tab is for Tableview controller.but i do xml parsing in appldidfinish method , in the xml parse didEndElement, i calculate items count and i give
it into first tab's Tableview controller's numberOfRowsInSection,but after xml parsing finished, the following method wont be called. tableview is empty....?
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return [myParser.items count];
}
i have declared in view did load method of that table view controller
myParser = [[XMLMyParser alloc] init];
any help please.....
When the XML parsing is complete, you need to inform the table view that the data has changed.
You do this by sending the reloadData message to your table view instance:
[myTableView reloadData];
Assuming you're using the NSXMLParser, you will need to add this in parserDidEndDocument message of your NSXMLParserDelegate implementation

When is UITableView finished updating?

My problem seems quite simple but I have failed to find a solution here or elsewhere.
I have a UITableView as a subclass in one of my UIViews. When the application finishes the last selected table cell is saved to NSUserDefaults and when the aplication restarts I want to set the seleced cell as it was before. However, this causes problems when I do it too early as the number of sections are unknown, i.e. the table hasn't loaded its data yet. So I decided to set it in the function numberOfRowsInSection, which works but I am sure is not the correct place to do this.
- (NSInteger)tableView:(UITableView *)tableView
numberOfRowsInSection:(NSInteger)section
{
int iNofRows = 0; //Default
// It is not great doing this here but....
if(bSetDefaultSelection == YES)
{
bSetDefaultSelection = NO; // Stop recursion
**NSIndexPath* indexPath = [NSIndexPath indexPathForRow:(NSUInteger)l last_sel_row inSection:(NSUInteger)0];
[self selectRowAtIndexPath:indexPath animated:NO scrollPosition:UITableViewScrollPositionMiddle];**
}
return iNofRows;
}
I think the place you're looking for is
- (void)viewDidAppear:(BOOL)animated;
// Called when the view has been fully transitioned onto the screen. Default does nothing
(See UIViewController for more information)
I've just debugged my app and that method is called after the one you've mentioned:
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
In your case you'd have to write something like:
- (void)viewDidAppear:(BOOL)animated{
NSIndexPath* indexPath = [NSIndexPath indexPathForRow:(NSUInteger)l last_sel_row inSection:(NSUInteger)0];
[self selectRowAtIndexPath:indexPath animated:NO scrollPosition:UITableViewScrollPositionMiddle];
}
Cheers!
Hey thanks for the reply. I thought nobody answered so much appreciate the effort. The solution with '(void)viewDidAppear:(BOOL)animate' only works with UITableViewController but I am dealing with UITableView.
But I decided to post a notification in my main controller that triggers the table to select and that works fine.
Thanks again.