情节提要表视图控制器不显示用户定义的音乐列表



我可能错过了一些非常明显的东西,但是:我正在尝试将项目迁移到故事板中,但是我正在努力让我的表视图控制器显示用户定义的数据。

基本上,该项目使用媒体选择器来创建歌曲队列,该应用程序正在完美播放。 当我尝试在表格视图中显示此用户定义的列表时,我的麻烦就开始了,它根本不显示任何信息!(我的旧 .xib 文件工作正常!.

我的表视图控制器的 .h:

#import <UIKit/UIKit.h>
#import <MediaPlayer/MediaPlayer.h>
#import "MusicViewController.h"
@protocol MusicTableViewControllerDelegate; // forward declaration
@interface MusicTableViewController : UITableViewController     <MPMediaPickerControllerDelegate, UITableViewDelegate> {
__unsafe_unretained id <MusicTableViewControllerDelegate>   delegate;
}

@property (nonatomic, assign) id <MusicTableViewControllerDelegate> delegate;
@property (nonatomic, strong) NSMutableArray *mediaItemCollectionTable;
@end
@protocol MusicTableViewControllerDelegate
// implemented in MainViewController.m
- (void) musicTableViewControllerDidFinish: (MusicTableViewController *) controller;
- (void) updatePlayerQueueWithMediaCollection: (MPMediaItemCollection *) mediaItemCollection;
@end

和我的 .m:

#import "MusicTableViewController.h"
#import "MusicViewController.h"
@interface MusicTableViewController ()
@end
@implementation MusicTableViewController
- (id)initWithStyle:(UITableViewStyle)style
{
self = [super initWithStyle:style];
if (self) {
}
return self;
}
@synthesize delegate;           // The main view controller is the delegate for this class.
@synthesize mediaItemCollectionTable;   // The table shown in this class's view.

// Configures the table view>
- (void)viewDidLoad
{
[super viewDidLoad];
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
#pragma mark - Table view data source
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
 // Return the number of sections.
 return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
// Return the number of rows in the section.
MusicViewController *mainViewController = (MusicViewController *) self.delegate;
MPMediaItemCollection *currentQueue = mainViewController.userMediaItemCollection;
return [currentQueue.items count];

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

if (cell == nil) {
    cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault
                                 reuseIdentifier: @"Cell"];
}
MusicViewController *mainViewController = (MusicViewController *) self.delegate;
MPMediaItemCollection *currentQueue = mainViewController.userMediaItemCollection;
MPMediaItem *anItem = (MPMediaItem *)[currentQueue.items objectAtIndex:indexPath.row];
    cell.textLabel.text = [anItem valueForProperty:MPMediaItemPropertyTitle];
return cell;
}
#pragma mark - Table view delegate
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
[tableView deselectRowAtIndexPath: indexPath animated: YES];
@end

我完全错过了为什么它不返回任何东西!

在情节提要中,检查表的内容是否设置为静态单元格 如果是,请将其更改为动态原型。

此外,在您的代码中,在cellForRowAtIndexPath中,您在哪里声明了onbject 'cell'。我看到直接比较if(cell == nil)它没有在任何地方声明。

我最终解决了这个问题! - 事实证明,由于将应用程序转移到基于故事板,我需要使用准备 Segue 将数据推送到表中。

最新更新