(首先,我是开发新手)我昨晚在互联网上找到了一个 Treehouse Learning 演示代码和一个 scrollingTableView 代码,并且能够将这两个代码集成在一起并让一切正常工作。然后我错误地用以前的版本保存了我的项目,并且无法让它再次工作。我已经把它恢复到它从 Parse 接收消息但没有在 scrollingTableView 上以缩略图形式显示它们的位置,而且当我点击消息时,我没有像我设置我的 segue 那样被带到 detailViewController。如果有人知道这次我哪里出错了,请给我一个提示!干杯!
@interface AlbumsViewController ()<PPImageScrollingTableViewCellDelegate>
@property (strong, nonatomic) NSArray *images;
@end
@implementation AlbumsViewController
- (void)viewDidLoad
{
[super viewDidLoad];
self.title = @"Albums";
[self.tableView setBackgroundColor:[UIColor grayColor]];
self.moviePlayer = [[MPMoviePlayerController alloc] init];
}
- (void)viewWillAppear:(BOOL)animated {
[super viewWillAppear:animated];
PFQuery *query = [PFQuery queryWithClassName:@"Messages"];
[query whereKey:@"recipientIds" equalTo:[[PFUser currentUser] objectId]];
[query orderByDescending:@"createdAt"];
[query findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error) {
if (error) {
NSLog(@"Error: %@ %@", error, [error userInfo]);
}
else {
// We found messages!
self.messages = objects;
[self.tableView reloadData];
NSLog(@"Retrieved %d messages", [self.messages count]);
}
}];
}
pragma mark - UITableViewDataSource
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return [self.messages count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"Cell";
PPImageScrollingTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
PFObject *message = [self.messages objectAtIndex:indexPath.row];
cell.textLabel.text = [message objectForKey:@"senderName"];
NSString *fileType = [message objectForKey:@"fileType"];
if ([fileType isEqualToString:@"image"]) {
cell.imageView.image = [UIImage imageNamed:@"icon_image"];
NSLog(@"it's an image");
}
else {
cell.imageView.image = [UIImage imageNamed:@"icon_video"];
NSLog(@"it's a video");
}
return cell;
}
pragma mark - UITableViewDelegate
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
return 150;
}
pragma mark - PPImageScrollingTableViewCellDelegate
- (void)scrollingTableViewCell:(PPImageScrollingTableViewCell *)scrollingTableViewCell didSelectImageAtIndexPath:(NSIndexPath*)indexPathOfImage atCategoryRowIndex:(NSInteger)categoryRowIndex
{
self.selectedMessage = [self.messages objectAtIndex:categoryRowIndex];
NSString *fileType = [self.selectedMessage objectForKey:@"fileType"];
if ([fileType isEqualToString:@"image"]) {
[self performSegueWithIdentifier:@"showDetail" sender:self];
}
else {
// File type is video
PFFile *videoFile = [self.selectedMessage objectForKey:@"file"];
NSURL *fileUrl = [NSURL URLWithString:videoFile.url];
self.moviePlayer.contentURL = fileUrl;
[self.moviePlayer prepareToPlay];
[self.moviePlayer thumbnailImageAtTime:0 timeOption:MPMovieTimeOptionNearestKeyFrame];
// Add it to the view controller so we can see it
[self.view addSubview:self.moviePlayer.view];
[self.moviePlayer setFullscreen:YES animated:YES];
}
[self.selectedMessage saveInBackground];
}
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
if ([segue.identifier isEqualToString:@"showDetail"]) {
[segue.destinationViewController setHidesBottomBarWhenPushed:YES];
DetailViewController *imageViewController = (DetailViewController *)segue.destinationViewController;
imageViewController.message = self.selectedMessage;
}
}
@end