iOS engineer who likes clean, simple designs and sans-serif.

Loading

UITableView Cheatsheet

My cheatsheet for a basic setup with UITableView:

1) Conform header to UITableViewDataSource and UITableViewDelegate in .h

@interface LLViewController : UIViewController <LLAddTaskViewControllerDelegate, UITableViewDataSource, UITableViewDelegate>

2) Set delegate and datasource to self in .m

-(void)viewDidLoad:
{
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
}

3) Set up the methods for number of rows and sections:

#pragma mark - UITableViewDataSource

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

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

4) Populate the table view with data from object.

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"Cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
    CCTask *task = [self.taskObjects objectAtIndex:indexPath.row];
    cell.textLabel.text = task.title;
    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
    [formatter setDateFormat:@"yyyy-MM-dd"];
    NSString *stringFromDate = [formatter stringFromDate:task.date];
    cell.detailTextLabel.text = stringFromDate;
    return cell;
}

Leave a Comment