Alex
Alex

Reputation: 1110

Populate UI Table View with array of strings

I can't find a simple, concise answer anywhere and I refuse to believe that XCode makes things as hard as other tutorials I've found out there...

Say I have the following array

NSArray* days = [NSArray arrayWithObjects:@"Sunday",@"Monday",@Tuesday",@"Wednesday",@"Thursday",@"Friday",@"Saturday",nil];

I have a UI Table View, table_Days, that I would like to simply show the items from my array. What is the proper way to go about populating my table?

Upvotes: 0

Views: 524

Answers (2)

Rich
Rich

Reputation: 8202

You should populate your table view using the data source methods. Returning the count of the array for the number of rows.

If you need to detect when a user taps on a cell you can use the delegate methods.

@interface ViewController<UITableViewDataSource, UITableViewDelegate>

@property (nonatomic, copy) NSArray *days;
@property (nonatomic, strong) UITableView *tableDays;

@end

@implementation ViewController

-(void)viewDidLoad
{
    [super viewDidLoad];

    UITableView *tableDays; // Set this up
    [tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"Cell"];
    tableDays.delegate = self;
    tableDays.dataSource = self;

    [self.view addSubview:tableDays];
    self.tableDays = tableDays;

    self.days = @[@"Sunday", @"Monday", @"Tuesday", @"Wednesday", @"Thursday", @"Friday", @"Saturday"];
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return [self.days count];
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Cell" forIndexPath:indexPath];

    cell.textLabel.text = self.days[indexPath.row];

    return cell;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSString *day = self.days[indexPath.row];
    NSLog(@"Day tapped: %@", day);
}

@end

You should consider using a UITableViewController if you just want to show a table view.

Note that its better practice to use camel case for variables.

Upvotes: 0

matt
matt

Reputation: 536026

Here's my full explanation, starting with a case extremely similar to yours:

http://www.apeth.com/iOSBook/ch21.html#_table_view_data

So suppose days is stored as an instance variable accessed through a property self.days. Then set self as the table view's datasource and use this code:

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    if (!self.days) // data not ready?
        return 0;
    return 1;
}
- (NSInteger)tableView:(UITableView *)tableView
        numberOfRowsInSection:(NSInteger)section {
    return [self.days count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView
         cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    UITableViewCell *cell =
        [tableView dequeueReusableCellWithIdentifier:@"Cell"
                                        forIndexPath:indexPath];
    cell.textLabel.text = (self.days)[indexPath.row];
    return cell;
}

Upvotes: 3

Related Questions