user1585542
user1585542

Reputation: 35

viewWithTag returning nil inside cellForRowAtIndexPath method

I have a UITableViewController with a custom prototype cell. Inside the prototype cell I have 2 labels and an image. These are tagged with 100 for the image, 101 and 102 for the label. I am trying to access that tags inside this method

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

    if ([indexPath row] == 0) {
        static NSString *CellIdentifier = @"InfoCell";
        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];

        UIImageView *albumArtworkImageView = (UIImageView *)[cell viewWithTag:100];
        albumArtworkImageView.image = [self getAlbumArtworkWithSize:albumArtworkImageView.frame.size];

        UILabel *albumArtistLabel = (UILabel *)[cell viewWithTag:101];
        albumArtistLabel.text = [self getAlbumArtist];

        UILabel *albumInfoLabel = (UILabel *)[cell viewWithTag:102];
        albumInfoLabel.text = [self getAlbumInfo];

        return cell;
    } else {

        static NSString *CellIdentifier = @"Cell";
        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];


        MPMediaQuery *audiobookQuery = [MPMediaQuery audiobooksQuery];
        MPMediaPropertyPredicate *albumPredicate = [MPMediaPropertyPredicate predicateWithValue: audiobookTitle forProperty: MPMediaItemPropertyAlbumTitle];
        [audiobookQuery addFilterPredicate:albumPredicate];
        NSArray *albumTracks = [audiobookQuery items];

        NSUInteger trackNumber = [[[albumTracks objectAtIndex:(indexPath.row-1)]  valueForProperty:MPMediaItemPropertyAlbumTrackNumber] unsignedIntegerValue];

        if (trackNumber) {
            cell.textLabel.text = [NSString stringWithFormat:@"%i. %@", trackNumber, [[[albumTracks objectAtIndex:(indexPath.row-1)] representativeItem] valueForProperty:MPMediaItemPropertyTitle]];
        } else {
            cell.textLabel.text = [[[albumTracks objectAtIndex:(indexPath.row-1)] representativeItem] valueForProperty:MPMediaItemPropertyTitle];
        }


        if ([self sameArtists]) {

            cell.detailTextLabel.text = @"";

        } else {

            if ([[[albumTracks objectAtIndex:(indexPath.row-1)] representativeItem] valueForProperty:MPMediaItemPropertyArtist]) {
                cell.detailTextLabel.text = [[[albumTracks objectAtIndex:(indexPath.row-1)] representativeItem] valueForProperty:MPMediaItemPropertyArtist];
            } else {
                cell.detailTextLabel.text = @"";
            }

        }

        return cell;
    }
}

heres a look at the storyboard

Storyboard

The issue I am having is the lines where I look up the view from the tags are returning nil. I have done this type of lookup before and I can't figure out why they are returning nil. Any help would be very appreciated. I'm not even sure of a good way to debug. I am a C# developer trying to learn objective-c/ios programming. Thanks!

Upvotes: 0

Views: 1627

Answers (1)

Leandro Fournier
Leandro Fournier

Reputation: 957

I give you an alternative. Make a custom class for UITableViewCell and declare its views there; then connect each cell's subviews with these properties and access them directly, without calling viewWithTag.

For example, in your custom cell:

@interface MyCustomCell : UITableViewCell

@property (strong, nonatomic) UIImageView *albumArtworkImageView;
@property (strong, nonatomic) UILabel *albumArtistLabel;
@property (strong, nonatomic) UILabel *albumInfoLabel;

and in your cellForRowAtIndexPath method:

MyCustomCell *cell = (MyCustomCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
cell.albumArtworkImageView.image = [self getAlbumArtworkWithSize:albumArtworkImageView.frame.size];
cell.albumArtistLabel.text = [self getAlbumArtist];
cell.albumInfoLabel.text = [self getAlbumInfo];

Remember to set MyCustomCell in your cell in the storyboard.

Hope this helps!

Upvotes: 2

Related Questions