Advertise Mobile SDKs Books Events Forum News Social Networking Support Us
Follow @iphonedevsdk on Twitter

Mockup & CodeGen, iPhone & iPad
($9.99)

Make your own iPhone apps
and run them live!
(free)

Manu
($0.99)

Want your application or service advertised on iPhone Dev SDK?

Go Back   iPhone Dev SDK Forum > iPhone SDK Development Forums > iPhone SDK Development

Reply
 
LinkBack Thread Tools Display Modes
Old 12-19-2008, 05:53 AM   #1 (permalink)
New Member
 
Join Date: Oct 2008
Posts: 30
Default UITableView + NSMutableArray

Anybody have a tutorial on how to populate a table view with an NSMutableArray. And a tutorial on how to add/remove objects based on adding/removing rows from the table? :/
xspyk is offline   Reply With Quote
Old 12-19-2008, 11:36 AM   #2 (permalink)
Registered Member
 
Join Date: Dec 2008
Posts: 495
Default

Here's a tutorial on hooking up an array to a table view. I used it as a basis for learning and found it very helpful:
iPhone Programming Tutorial - Populating UITableView With An NSArray | iCodeBlog

I'm not aware offhand of being able to just programmatically say something like:
UITableView *x = [[UITableView alloc] init];
[x setData:myArrayData];

In a nutshell, create a new class that derives from UITableViewController. There are a couple of important methods : numberOfRowsInSection and cellForRowAtIndexPath.

numberOfRowsInSection needs to have a number returned to tell how many rows there are. This would typically be the number of items in your array. That array could be internal or inside your delegate. Here's an example of using the count from a data array inside the delegate:
Code:
- (NSInteger) tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    MyAppDelegate *delegate = (MyAppDelegate *)[[UIApplication sharedApplication] delegate];
    return [delgate.myDataArray count];
}
The other one is what actually requests ( on a cell by cell basis ) the data that should go in that cell. Using the same example of having a data array inside your delegate, it would look something like this:
Code:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    // lines for setting up the cell resource itself
    // ....
    // now set the cell data
    MyAppDelegate *delegate = (MyAppDelegate *)[[UIApplication sharedApplication] delegate];
    [cell setText:[delegate.myDataArray objectAtIndex:indexPath.row]];

    return cell;
}
Hopefully that helps!
exorcyze is offline   Reply With Quote
Old 12-19-2008, 08:46 PM   #3 (permalink)
New Member
 
Join Date: Oct 2008
Posts: 30
Default

Quote:
Originally Posted by exorcyze View Post
Here's a tutorial on hooking up an array to a table view. I used it as a basis for learning and found it very helpful:
iPhone Programming Tutorial - Populating UITableView With An NSArray | iCodeBlog

I'm not aware offhand of being able to just programmatically say something like:
UITableView *x = [[UITableView alloc] init];
[x setData:myArrayData];

In a nutshell, create a new class that derives from UITableViewController. There are a couple of important methods : numberOfRowsInSection and cellForRowAtIndexPath.

numberOfRowsInSection needs to have a number returned to tell how many rows there are. This would typically be the number of items in your array. That array could be internal or inside your delegate. Here's an example of using the count from a data array inside the delegate:
Code:
- (NSInteger) tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    MyAppDelegate *delegate = (MyAppDelegate *)[[UIApplication sharedApplication] delegate];
    return [delgate.myDataArray count];
}
The other one is what actually requests ( on a cell by cell basis ) the data that should go in that cell. Using the same example of having a data array inside your delegate, it would look something like this:
Code:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    // lines for setting up the cell resource itself
    // ....
    // now set the cell data
    MyAppDelegate *delegate = (MyAppDelegate *)[[UIApplication sharedApplication] delegate];
    [cell setText:[delegate.myDataArray objectAtIndex:indexPath.row]];

    return cell;
}
Hopefully that helps!
Sorry, I got it eventually, and that's exactly the tutorial I used :P.
xspyk is offline   Reply With Quote
Reply

Bookmarks

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



» Advertisements
» Online Users: 253
18 members and 235 guests
2WeeksToGo, @sandris, AdamL, ADY, BrianSlick, Dani77, F_Bryant, GHuebner, HDshot, headkaze, mer10, prchn4christ, Rudy, smithdale87, Thompson22, timle8n1, Touchmint, vigu360
Most users ever online was 1,187, 10-11-2011 at 08:09 AM.
» Stats
Members: 158,880
Threads: 89,228
Posts: 380,748
Top Poster: BrianSlick (7,129)
Welcome to our newest member, @sandris
Powered by vBadvanced CMPS v3.1.0

All times are GMT -5. The time now is 12:54 PM.
Powered by vBulletin® Version 3.8.0
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.3.0