简体   繁体   English

viewWithTag在cellForRowAtIndexPath方法内返回nil

[英]viewWithTag returning nil inside cellForRowAtIndexPath method

I have a UITableViewController with a custom prototype cell. 我有一个带有自定义原型单元的UITableViewController。 Inside the prototype cell I have 2 labels and an image. 在原型单元内,我有2个标签和一个图像。 These are tagged with 100 for the image, 101 and 102 for the label. 这些标签的图像标记为100,标签的标记为101和102。 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 这里看故事板

故事板

The issue I am having is the lines where I look up the view from the tags are returning nil. 我遇到的问题是我从标记中查找视图的行返回nil。 I have done this type of lookup before and I can't figure out why they are returning nil. 我之前做过这种类型的查询,但无法弄清楚为什么它们返回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. 我是一名C#开发人员,试图学习Objective-C / iOS编程。 Thanks! 谢谢!

I give you an alternative. 我给你一个选择。 Make a custom class for UITableViewCell and declare its views there; UITableViewCell一个自定义类,并在其中声明其视图; then connect each cell's subviews with these properties and access them directly, without calling viewWithTag. 然后使用这些属性连接每个单元格的子视图并直接访问它们,而无需调用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: 并在您的cellForRowAtIndexPath方法中:

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. 请记住在情节提要中的单元格中设置MyCustomCell。

Hope this helps! 希望这可以帮助!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM