我可能遗漏了一些非常明显的东西,但是:我正在尝试将一个项目迁移到 Storyboards 中,但是我正在努力让我的表格视图控制器显示用户定义的数据。
基本上,该项目使用媒体选择器来创建歌曲队列,应用程序可以完美地播放这些歌曲。当我尝试在表格视图中显示此用户定义列表时,我的麻烦就开始了,它根本不显示任何信息!(我的旧 .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
我完全想念它为什么不返回任何东西!