NSMutableArray to table view - iphone

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.

Related

UITableView not able to fetch data from array

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.

How can I centralize cellForRowAtIndexPath?

I'm looking to have a single - (UITableViewCell *)cellForRowAtIndexPath:(NSIndexPath *)indexPath method that all UITableView's will use for formatting, etc.
What is the best way to accomplish this?
Right now I'm thinking I could use a separate class or put delegate methods in my app delegate and hook the UITableView's file's owner to that. But then I'd have to provide those classes/methods access to the data that would normally otherwise be right in the ViewController subclass.
How have others accomplished this sort of thing?
EDIT: To clarify, I'm not looking for a way to return a common cell itself, but rather a way to have a single cellForRowAtIndexPath method in my entire app.
If you just want to centralize all of the formatting but have the data sources separate in each class you could create a factory class for your table view cells which provides a method
+ (UITableViewCell* ) tableViewCellForTableView:(UITableView *)tableView withType:(int)type;
By creating type costants which get passed in, you can create a set of basic table view cells that can then be returned by this class. It is important to pass in the tableView to make use of reusable cells. A cellForRowAtIndexPath could now fetch cells from there and configure them depending on the data
- (UITableViewCell *)tableView:(UITableView *)tableView
cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = [MyCellFactory tableViewCellForTableView:tableView withType:kMyTypePlain];
cell.textLabel.text = ....;
}
You may consider using an objective-c category on your ViewController which implements the cellForRowAtIndexPath method.
If you're doing something very complex in this method however, I suggest you just create a nib file which contains your cell and then load it in cellFoRowAtIndexPath. You could even create a class that inherits from UITableViewCell and stick your own methods/properties in there. I highly recommend this approach.
You can load the nib, say "MyCustomTableCell.nib" which contains your customised UITableViewCell like so:
NSArray *topLevelObjects = [[NSBundle mainBundle] loadNibNamed:#"MyCustomTableCell"
owner:self
options:nil];
MyCustomTableCell* cell = [topLevelObjects objectAtIndex:0];
I think you can use the answer from #TriPhoenix for most of this.
The only additional thing I would add is that you could potentially send the data along with the call to the factory.
[MyCellFactory cellForTableView:tableView withType:kMyTypePlain data:data];
You would of course have to ensure that the data responds to a common interface to ensure that you can set things easily e.g. making sure each object responds to a similar method like
cell.textLabel.text = data.textLabelText;
You could do this using a protocol if you like. It's up to you how you structure your data.

Populating UITableView with Dictionary Data

My Situation:
I have an NSDictionary object. Keyed by NSNumber. Values are custom objects.
I want to put dictionary values into a UITableView. As far as I can tell, UITableView requires that its source collection be indexed so when cellForRowAtIndexPath is called, you can use the indexPath to look up the value.
Problem is that when didSelectRowAtIndexPath is called, I want to look up the object from the dictionary, but I don't have the key. All I have is the indexPath.row.
My solution:
I create an array of keys. I use the index of the array to get the key, and then use the key to get the object out of the dictionary.
My problem:
This seems kind of sloppy especially since this is a routine task (populating the UITableView and then responding when someone touches a cell). Is this the way it's designed to work or is there a better way?
The problem is that dictionaries don't have an order, while a table view does. The answers to this question should give you some ideas for alternative ways of handling this.
As mentioned in another answer, an NSDictionary's keys are not ordered, therefore you are not guaranteed to get the rows in a particular order. That said, it is quite easy to use a dictionary for use with a UITableView.
- (UITableViewCell *)tableView:(UITableView *)tableView
cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
// tableview cell setup
NSArray* keys = [self.data allKeys];
cell.textLabel.text = [self.data objectForKey:[keys objectAtIndex:indexPath.row]];
return cell;
}
If you need the list to be ordered according to how you entered them into the NSDictionary, Matt Gallagher from Cocoa With Love offers an elegant solution with his take on OrderedDictionary. You can read about it here.

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.

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/