If you ever need to create and dequeue a custom cell programatically (not use storyboards) do this

Screen Shot 2017-06-04 at 2.12.19 PM.png

Code

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *cellIdentifier = @"Cell";

    [tableView registerClass:[TrackCell class] forCellReuseIdentifier:cellIdentifier];
    TrackCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier forIndexPath:indexPath];


    Track *track = [self.tracks objectAtIndex:indexPath.row];

    cell.titleLabel.text = track.title;
    cell.artistLabel.text = track.artist;

    UIImage *albumImage = [UIImage imageNamed:track.albumImage];
    cell.imageView.image = albumImage;

    return cell;
}
Advertisements