UITableView not able to fetch data from array - iphone

i am making iPad application, in which i am fetching data from Url, after fetching data from URL,
i am storing into array,
when i write NSLOG inside this two TableView method,
it works properly,
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
NSLOG(#"ARRAY=%#",arrayname);
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
NSLOG(#"ARRAY=%#",arrayname);
}
but when i write this NSLOG inside this method thn it shows error,
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
NSLOG(#"ARRAY=%#",arrayname);
}
i also declared nsmutablearray properly....
it shows EXC_BAD_ACCESS
why this any idea ?

The method cellForRowAtIndexPath must return the cell, otherwise you'll see the error you see. But if the code above is 'metacode', and you put you NSLog somwhere between the proper strings of code, then you can follow Alex Reynolds' advice.
Check if your array is still alive. E.g. you can set a breakpoint and check if the program falls because of turning to 'arrayname', and if so, try to find where it could be released in your code by this moment. Also, perhaps you're just create your array with wrongly.

Related

cellForRowAtIndexPath Usage Pattern

In the below code I want to perform some DB actions when a cell is deleted, therefore I need to send my Server information about the cell being deleted. If I remember correctly cellForRowAtIndexPath should never be called directly, However I cannot think of any other way to get cell info in the below method, so my question is:
Is it acceptable to call cellForRowAtIndexPath manually below:
[tableView cellForRowAtIndexPath:indexPath]);
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
if (editingStyle == UITableViewCellEditingStyleDelete) {
[localGlobalNotifications removeObjectAtIndex:indexPath.row];
[notificationTableView beginUpdates];
[notificationTableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationLeft];
[self postLeaveRequest];
NSLog(#"Row is : %#", [tableView cellForRowAtIndexPath:indexPath]);
[notificationTableView endUpdates];
}
}
To clarify: I understand that I can invoke a delegate call of cellForRowAtIndexPath by calling reloadData, what I'm trying to do is access the cell being deleted within commitEditingStyle. I'm not trying to reload my tableView, instead I want to get a reference to the cell being deleted. - Is it acceptable to obtain a reference to said cell by calling cellForRowAtIndexPath directly?
There's nothing wrong with asking the TableView to give you the cell, just as you do in your sample code.
Here's the documentation for the return value:
An object representing a cell of the table or nil if the cell is not visible or indexPath is out of range.
If you're annotating the cell with 'model' data then I think you're breaking the MVC pattern. Your view doesn't need to know about the model data in this way, and so querying the view to make a database change will make life difficult you in the future (readability, extensibility and reusability for example)
You would be better off having your DB metadata stored in a collection such as an NSArray - or an NSArray of NSArrays.
Then you could get all the data you need with something like:
id modelData = myModel[indexPath.section][indexPath.row];
Yes you can use cellForRowAtIndexPath: as they have also used in apple docs or you can create an array of your cells then you can delete it from there and reflect it in your database.
You can call cellForRowAtIndexPath: method using following line, here it will automatically calls all delegate and datasource methods.
[tableView reloadData];
or
[tableViewObjct reloadData];
Hope this solves your problem!

What should cellForRowAtIndexPath return if the table is empty?

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
I think if the table is empty, namely that
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return [[BNUtilitiesQuick getBizs] count];
}
always return 0
I would expect that -(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath should not be called at all
This is important. I am making a program to search through things and sometimes after some searching, there is no result being returned.
However, is called anyway and I got an exception. What should I do?
-tableview:cellForRowAtIndexPath: may be called before the table view has realised it has zero rows and so shouldn't be called.
Therefore your implementation needs to check that the value of the row being passed in is not outside the bounds of your array. If it is, you need to return an empty cell (the documentation states returning nil will raise an exception).
There is simply no way that tableView:cellForRowAtIndexPath: will be called when the tableView:numberOfRowsInSection: method returns 0. It is likely that the count is being returned incorrectly.
Often you would have a giant switch statement in a cellForRow... Meaning that you have to have a value to return in the default case. I usually return nil there.
This is required by the compiler to not show a warning, but it should never be actually called. This method is only called for row indexes that are possible due to what you reeturn in the numberOfSections and numberOdRowsInSection methods.
First print [[BNUtilitiesQuick getBizs] count];
in NSLog and see if it really returns 0.
If it shows 0, and if the cellForRowAtIndexPath: still gets called, I suspect there is some ghost hanging around there. :-)
What about returning 1 as count (or 10 for example), and that one cell will display dimmed text with "No Results" ?
(open your contacts app and search for something.. it will have about 10 rows , 9 are empty and 1 displays "no results")

Understanding the delegates in UITableView and UITableViewController

I am learning how to use the UITableView and UITableViewController in the iOS and I think I may have confused myself. I have created a simple TableView and I have 2 sections. Nothing complicated.
I have the following defined and it builds fine:
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 2;
}
- (NSInteger)tableView:(UITableView *)tableView
numberOfRowsInSection:(NSInteger)section
{
// Section is going to be either 0 or 1, high or low
if (section == 0) {
return 1;
}
else {
return 2;
}
}
However what I don't understand is the definitions of the methods. Both methods have to return an integer so I understand the starting (NSInteger). The numberOfRowsInSection starts with tableView:(UITableView *)tableView and I don't understand why?
I'm am new to programming the iOS so be gentle :-) All help greatly appreciated.
Mike
The method name is "tableView:numberOfRowsInSection:". The first argument is the instance if UITableView which is asking the data source for the number of rows in a particular section. This is a useful convention as you might have a single object act as the data source for many table views or want to update the table view in some way when a delegate method is called. By passing the calling object to the delegate you avoid needing to have the delegate maintain an additional reference to that object.
Take a look at the NSURLConnection delegate methods dealing with authentication for an example of where this is really necessary.
tableView:(UITableView *)tableView is helpful if you need to know which tableView sent that delegate method.
This is Apple's naming convention for delegate and data source methods. numberOfSectionsInTableView: has no arguments other than the table view, so that argument is added at the end. tableView:numberOfRowsInSection: takes another argument, the index of the section in question. Apple has decided that, when there are other arguments, the calling object should go first, and the arguments come after that.
Check out the UITableViewController Class Reference
- (NSInteger)tableView:(UITableView *)tableView
The first part, NSInteger lets you know that you need to return a number return 1;, the second part (UITableView *)tableView lets you know that you are dealing with the UITableView class.

NSMutableArray to table view

i am new to the iPhone development.
i need to display the NSMutableArray contents into UITableViewCell..
it is quite simple.. but, i want to know, how to add the NSMutableArray contents into table view at runtime?
please anyone help me..
thank you very much..!
Have you read the Table View Programming Guide?
In particular the section that talks about Creating and Configuring a Table View.
The little code snippets on those guides provide examples that deal with the simplest case, which is an array.
In short, you provide a "data source", which the table view asks for each row from. That data source is usually (but doesn't have to be) the ViewController that uses the table.
Make sure to check out the example applications linked to there if you need to see some working examples in Xcode itself.
You want to add total array to single cell or a tableView. If the answer is tableView, then you can directly give the values in the following method like:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
cell.textLabel.text = [array objectAtIndex: indexPath.row];
return cell;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return [array count];
}
you can use like that.

Customize height of n cells in UITableView, but not all the same

thanks to the folks here I already learned quite a lot on my way to a cool iPhone App I am working on. However, I was wondering if anyone found out how to manipulate a UITableView, so that a cell (any or, if that is not possible, it could only be the selected one) can have a different height.
I know I can use something like this:
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
return 70;
}*/
To modify the whole TableView. But how would I address this to a single, specific cell?
The final goal is to achieve a "OS X dock"-like zoom effect when scrolling through a table...
Any help is appreciated.
Best regards,
J*
The method that you're citing there in your question is exactly the method you want to use. The code you posted always returns a fixed value. But the indexPath parameter is there so that you can use that in whatever conditional processing you might want to do. For example, determine if that row is selected, and return a different height.
You'll also want to take care that the cell you return from -[UITableView cellForRowAtIndexPath:] matches this height.
You use the indexPath row and section to determine and return the height for the cell.
All of the methods related to UITableViewController give you an indexPath that will correspond to the cell when asking for specific information. For example, when asking for the actual cell to return:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
What to do when a cell was selected:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
Height of the cell:
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
And several others. Check out a UITableViewController tutorial to get the hang of how it works as a delegate and a dataSource for a UITableView.
Here is a good one: http://adeem.me/blog/2009/05/19/iphone-programming-tutorial-part-1-uitableview-using-nsarray/