Reputation: 149
I copied sample code to create an objective-c table in my app (http://www.appcoda.com/uitableview-tutorial-storyboard-xcode5/). The example works great.
The issue I have is that the example uses a predefined array of items, but my app generates the array item list when the view controller is loaded.
My app is generating a list of movie credits in:
NSMutableArray *creditList;
In viewDidLoad I have:
creditList = [NSMutableArray arrayWithObjects:@"Test Movie 1", nil];
Then I generate the movie list. When the loop is complete, the list is stored correctly in array creditList as determined by:
NSLog(@"creditList array: %@", creditList);
How do I now populate the table with the list of items generated and where would I put that code?
Thanks in advance, Lee
Upvotes: 1
Views: 5031
Reputation: 322
So here are the steps
Since you already have the array and the values in the array you have now to set the sections and rows as you can see in this code
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return creditList.count;
}
now you have to set up the actual cell with this code
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell" forIndexPath:indexPath];
cell.textLabel.text = [[creditList objectAtIndex:indexPath.row] capitalizedString];
return cell;
}
Upvotes: 4
Reputation: 1961
To populate a table with your data a common design pattern to use is UITableViewDelegate, in your implementation (.m) file add the delegates like so :
@interface ViewController () <UITableViewDelegate, UITableViewDataSource>
Then call two functions in the same ViewController.m file, the first of which is :
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
return [_creditList count];
}
The second function :
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
static NSString *cellName = @"cellNamedInStoryBoard";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellName];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellName];
}
cell.textLabel.text = [_creditList objectAtIndex:indexPath.row];
NSLog(@"cell label created");
return cell;
}
You will need to set dataSource and the delegate in your storyboard which you can you can do by right clicking and dragging your table view to the yellow ViewController icon and selecting both the delegate and dataSource.
Once you've done that, provided your array is populated with strings , you should see the items listed in table view when you run the app.
Upvotes: 0
Reputation: 10327
You can use NSMutableArray
same as NSArray
. No need to do anything more.
After the items are generated. You just need to call:
[tableView reloadData];
Upvotes: 1