Ios viewWithTag在cellForRowAtIndexPath方法中返回nil

Ios viewWithTag在cellForRowAtIndexPath方法中返回nil,ios,objective-c,xcode,storyboard,Ios,Objective C,Xcode,Storyboard,我有一个带有自定义原型单元的UITableViewController。在原型单元内,我有两个标签和一个图像。图像标记为100,标签标记为101和102。我正在尝试访问此方法中的标记 - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { if ([indexPath row] == 0) { static NSStr

我有一个带有自定义原型单元的UITableViewController。在原型单元内,我有两个标签和一个图像。图像标记为100,标签标记为101和102。我正在尝试访问此方法中的标记

- (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;
    }
}
这里有一个故事板看


我遇到的问题是,我从标记中查找视图的行返回nil。我以前做过这种类型的查找,但我不明白为什么它们返回nil。任何帮助都将不胜感激。我甚至不确定有什么好的调试方法。我是一名试图学习objective-C/ios编程的C#开发者。谢谢

我给你一个选择。为
UITableViewCell
创建一个自定义类,并在其中声明其视图;然后将每个单元格的子视图与这些属性连接起来,并直接访问它们,而无需调用viewWithTag

例如,在自定义单元格中:

@interface MyCustomCell : UITableViewCell

@property (strong, nonatomic) UIImageView *albumArtworkImageView;
@property (strong, nonatomic) UILabel *albumArtistLabel;
@property (strong, nonatomic) UILabel *albumInfoLabel;
在您的
cellforrowatinexpath
方法中:

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];
记住在故事板中的单元格中设置MyCustomCell


希望这有帮助

更改为
[cell.contentView view with tag:101]
。你的所有子视图都应该在单元格的内容视图中,而不是直接在单元格中。我试过了,但仍然返回nil。还有什么我可能做错的吗?你怎么知道带tag:的视图是否返回零?您是通过日志记录还是调试器确认的?我正在调试器中查看值。谢谢您的回复。这些房产是否应该出售?如何连接它们?我已经实现了这一点,并意识到当我将iPhone切换到横向模式时,它可以工作,但当我将其设置为纵向模式时,它就不工作了。你知道为什么会这样吗?谢谢你能把他们联系起来吗?Command+单击并从所有者(即带有黄色图标的viewController)拖动到子视图(标签、imageView等)。什么东西在纵向模式下不工作?是的,它连接在一起,在横向模式下工作得很好,但在纵向模式下没有显示任何东西。不确定到底是什么原因造成的,那是另一个问题。我认为您正在使用Xcode 6创建一个全新的项目,因此您可以在Interface Builder中将UIViewController视为方形视图。你不觉得吗?