Why table view not showing any data in iphone - ios

When parsing a local XML file i can see the data in console using NSLog. While in the simulator am getting only the plain table view without any data parsed from xml. Am not understanding where am missing it.
- (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];
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
}
theLists = [app.listArray objectAtIndex:indexPath.row];
cell.textLabel.text = theLists.title;
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
return cell;
}

I think you are not reloading your table view.
after completing your parsing just reload your table view.
[reload tablename];
then check your table view delegates are calling or not after this method.
I think this will help you.
happy coding.

It seems you are reloading in the DidLoad.But After parsing and getting the data from xml, You have to reload that data with TableView.
[yourtableView reloadData];
Before doing thing, be sure that you have set Delegate and DataSource.

I think there are two expected reasons for that:
1- May be the number of sections and number of row are not set with the correct value.
2- may be you need to reload the table view data.
Please check these two methods:
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return 5; // or any number you want
}

I got the answer finally where i didn't properly connected the tableview controller class with corresponding controller. I followed the link Trouble making connections with story board
And finally it showed the data in the table view.
Hope this answer might help others. Thanks for all the people who gave their suggestions in this post.

Figured I should add this, if you are having trouble with data not showing up or loading.
Make sure your tableview delegate and data source are set to self.
So in the viewcontroller's viewDidLoad() function, make sure you add
tableView.delegate = self
tableView.dataSource = self

Related

Is cellForRowAtIndexPath called when you use a custom cell?

I am using a custom cell class in a tableview controller.
When I include a statement in the tableviewcontroller in cellForRowAtIndexPath NSLog(#"method called"): it does not seem to get called.
Is it possible that this method is not called when you have a custom cell?
Edit:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
NSLog(#"cell for row at index path called");
NSDictionary *item= [self.getItems objectAtIndex:indexPath.row];
//This sets place in storyboard VC
IDTVCell *cell = [self.tableView dequeueReusableCellWithIdentifier:#"Cell"];
cell.item = item;
if (cell == nil) {
cell = [[IDTVCell alloc] initWithStyle:UITableViewCellStyleDefault
reuseIdentifier:#"Cell"];
}
return cell;
}
cellForRowAtIndexPath is not called if no rows are returned.
-tableView:cellForRowAtIndexPath: is not getting called
That is what happened in my case.
It can also not get returned if you reload table on wrong thread and in certain other scenarios.
cellForRowAtIndexPath: not called
However, a custom cell per se does not cause this..
To answer your question - Yes, it is.
There could be n-number of reasons why cellForRowAtIndexPath: is not getting called. This may be because delegate / dataSource is not set or UITableView frame is not set... etc. etc.
You should easily find a solution with more online research and closure look at your code.

Two UITableView's in one View Controller

So I've found many questions and answers on this issue but I still dont seem to be able to fix my issue.
I've got this:
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
if(tableView == self.moduleTableView){
UITableViewCell *cell = [_moduleTableView dequeueReusableCellWithIdentifier:#"TableIDCell"];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:#"TableIDCell"];
}
Thats all fine and works for the moduleTableView and I have code to fill the individual cells.
However, I later have an if(tableView == theOtherTableview) and the code is never executed, (have used break points to check). Because of this the second tableview never loads and fills.
I've done for the same if statements in the numberOfRows method and I declare:
self.tblEvents.delegate = self;
self.tblEvents.dataSource = self;
self.otherTblEvents.delegate = self;
self.otherTblEvents.dataSource = self;
and it still doesn't work, am I missing anything? I think I've linked the implementation correctly to the storyboard, I just can't get it to reload the cellForRowAtIndexPath: method while (tableView == OtherTableView)
Thanks
I had the exact same issue as you.
I found that the cleanest solution is to make 2 datasource classes. That way there is alot less code in your view controller.
Basically something like this
DatasourceTblEvents *ds1 = [DatasourceTblEvents new];
DatasourceotherTblEvents *ds2 = [DatasourceotherTblEvents new];
self.tblEvents.dataSource = ds1;
self.otherTblEvents.dataSource = ds2;
This dramatically reduces the amount of if statements within the datasource protocol methods. And this way you can just implement the datasource methods in each datasource class like you would with one table view.
Hope this helps.
Check if - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section method returns zero value (empty array count), Cell for Row Method will be called only if numberOfRowsInSection method returns non zero value.
numberOfRowsInSection should return a non zero value

UITableViewCell and label not displaying text

I have a UITableView with a cell that has two labels in it. The table view is linked to the dataSource and delegate, the labels in the cell are linked to an IBOutlet, what not. It looks to me like this should work, but this code below is not running so the Cell or labels are not populated with text. Any ideas? Or anything I'm missing?
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath: (NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"theLabelCell";
CustomClassCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
CustomClassText *customText = _arrayThatHasText[indexPath.row];
if (![self isSelectionMode]) {
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
}
cell.TitleLabel.text = customText.firstLabel;
cell.TextLabel.text = customText.secondLabel;
return cell;
}
Did you remember to also register the cell identifier for reuse?
- (void)viewDidLoad
{
[super viewDidLoad];
[self.tableView registerNib:[UINib nibWithNibName:#"CustomClassCell" bundle:nil] forCellReuseIdentifier:#"theLabelCell"];
}
If you are not seeing any cells, then check whether numberofSections and numberOfRowsInSection delegate methods are not returning 0
When does your _arrayThatHasText get populated? The issue might very well be that the data source (_arrayThatHasText) is getting instantiated before the numberofSections and numberOfRowsInSection delegate methods are being called and then after these methods are called the data source is being populated with actual data -> which would result in the 0 values in the delegate methods as you are experiencing.
You might want to try putting a [self.tableView reloadData] call at the end of ViewWillAppear or in ViewDidAppear method and see if that helps.

How to Initialize my UITableView table to have a certain number of rows based on input from user and a Button

I'm really new to Objective-C here so what I'm asking may be trivial to most of you but any guidance will help.
Here's a picture of my storyboad.
My current objective is to allow for the user to enter in the number of sets (NSInteger *numReps) and then press the "Log Reps" button and have the table initialize with numReps cells that look like the prototype cell.
Now where I'm at a loss for the implementation. I've never done this kind of thing before so I'm not exactly sure what the best way to go about it is. I have thought of making a custom class for the UITableView table that would take info from the view after the Log Reps button is pushed. I'm not entirely sure how this would need to be implemented. Or can I simply add the table to the properties of the view controller and setup the table within the view controller? That was my initial idea and seems ideal so that I would have everything in one place.
Pleas advise. I am new to all of this and come from a C++ background so I'm still learning a lot of the notation.
Try this:
-(IBAction)btnLogClicked {
int iSet = 4 //Number of row in table
UITableView *tblView= [[UITab;eView alloc]initWithFrame:CGRectMake(0,50,320,100)];
tblView.delegate = self;
tblView.dataSource = self;
[self.view addSubView:tblView];
}
Table View Data Source and Delegate Methods
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return iSet;
}
- (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];
}
// Display what you want to display in each cell
return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
[tableView deselectRowAtIndexPath:indexPath animated:YES];
}
It's not clear what you want to present in your prototype cells, but you need an array (or probably an array of dictionaries) to populate the cells. The number of rows is determined by the number of entires (the count) of that array. So, if you take the number entered by the user, and add that many object to your array, then call reloadData on the table, you will get the number of rows that you want. What those object are that you add to the array, depends on what you're trying to show there.
you could start reading: Table View Programming Guide for iOS
But I can answer you:
You can add the UITableView to the UIViewController, but you need set your UIViewController like the TableView's delegate and dataSource. Your ViewController need to implement the protocol: UITableViewDataSource
The method that you are looking for is: tableView:numberOfRowsInSection:
But I really recommend you that read the Apple Reference.

How to preserve the value of a uitextfield during a scroll

I was having troubles using the dequeue mechanism in my tableview, i have a custom cell with a uiTextField.
When a put some value on it and do a scroll, the value of the TextField goes to another cell.
Anybody can help me on it ?
Thanks guys.
Here is the code:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"MyPedidoItemCell";
PedidoItemCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
// Configure the cell...
if (!cell) {
cell = [[PedidoItemCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
Produto *produto = [self.fetchedResultsController objectAtIndexPath:indexPath];
cell.lblNome.text = produto.nome;
cell.lblCodigo.text = produto.codigo;
cell.lblFuncao.text = produto.funcao;
return cell;
}
When you call dequeueReusableCellWithIdentifier it may return a cell that has scrolled out of view but still contains data in the UITextField. If you don't clear the content of the UITextField or set it to the proper value for the row, you may see unwanted data.
The code you showed is using CoreData to load data for cell, but you mentioned that you don't store the data anywhere.
I suspect that the you are getting object with no data from your fetchedResultsController because you did not store any data.
So quite possibly the produto is nil and you are not setting any values on cell.
Eventually the cell you updated manually gets reused and you see the text again.
You need to first store the data that you typed into the UITextField before you can retrieve it from core data.

Resources