0

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?

2 Answers 2

3

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;
}
Sign up to request clarification or add additional context in comments.

7 Comments

You should definitely buy @matt's book if you need answers to ask basic questions like this.
I've seen similar explanations around. This is definitely the best I've seen, however as an experienced programmer I'm still absolutely shocked it takes 3 methods to populate a table with strings.
Where do these methods live? I see that I don't directly call them, but how does my tableview know to use them?
You implement them in whatever object you set as the tableview's datasource / delegate. see this url: developer.apple.com/library/ios/documentation/UserExperience/…
@RhinoFeeder "as an experienced programmer I'm still absolutely shocked" See the first few paragraphs of my discussion here: apeth.com/iOSBook/ch21.html#_table_view_cells Yes, you might expect the data to be stored in the table (I expected that, coming from REALbasic years ago), but it turns out there's a lot more going on here than you think, and it is worth trying to wrap your head around why it's great. This is true model-view-controller architecture, plus the cells are reused (i.e. you can show 1000 rows with just a dozen table cells).
|
0

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.