iPhone - UITableView到DetailView

时间:2013-05-28 08:21:43

标签: ios objective-c

我有一个tableView列出了包含jpg和mp4文件的内容目录。

目前,当您选择任何mp4文件时,它会加载MPMovieController并相应地播放正确的文件,但如果文件的扩展名是JPG,我不确定如何加载详细视图。

例如,如果tableView中的所选文件是图片文件,则会在imageView中打开detailView以显示该图片。

对于其他文件,它会推送alertView,因为它是一个未知文件。

这是我到目前为止tableview didSelectRowAtIndex

的代码
    - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath

{static NSString *CellIdentifier = @"Cell";

UITableViewCell *cell = [tableView
                         dequeueReusableCellWithIdentifier:CellIdentifier];


NSString *urlString = [NSString stringWithFormat:@"http://10.1.4.3:8080/DCIM/DCIM/VIDEO/%@",[movieArray objectAtIndex:indexPath.row]];

//Play multiple video
NSURL *url = [NSURL URLWithString:[urlString stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];

_moviePlayer =  [[MPMoviePlayerController alloc]
                 initWithContentURL:url];

[[NSNotificationCenter defaultCenter] addObserver:self
                                         selector:@selector(moviePlayBackDidFinish:)
                                             name:MPMoviePlayerPlaybackDidFinishNotification
                                           object:_moviePlayer];

_moviePlayer.controlStyle = MPMovieControlStyleDefault;
_moviePlayer.shouldAutoplay = YES;
[self.view addSubview:_moviePlayer.view];
[_moviePlayer setFullscreen:YES animated:YES];}

任何链接或源代码都非常方便。

感谢。

2 个答案:

答案 0 :(得分:0)

因为您使用MPMoviePlayerController,所以xcode将处理您的文件(它将创建播放电影的视图并在完成后关闭视图)。

如果您想要显示图片,只需使用关闭按钮创建自定义视图(因为您需要关闭视图)。在此自定义视图中,添加UIImageView并显示图像。

答案 1 :(得分:0)

我没有测试过此代码,因为我没有时间

然而,它应该有用。

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath

{  
    static NSString *CellIdentifier = @"Cell";

    UITableViewCell *cell = [tableView
                             dequeueReusableCellWithIdentifier:CellIdentifier];

    /* ============================== THIS DO THE TRICK ============================ */
    NSString *kind = nil;
    NSString *path = [NSString stringWithFormat:@"http://10.1.4.3:8080/DCIM/DCIM/VIDEO/%@", [movieArray objectAtIndex:indexPath.row]];
    NSString *ext = [[path pathExtension] lowercaseString];

    if([ext isEqualToString:@"jpg"] || [ext isEqualToString:@"png"] || [ext isEqualToString:@"jpeg"]) {
        UIViewController *imageController = [[UIViewController alloc] init];
        UIImageView *imageView = [[UIImageView alloc] initWithImage:[UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:path]]]];

        CGRect frame = CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height);

        [imageController.view setFrame:frame];
        [imageView setFrame:frame];
        [imageController.view addSubview:imageView];
        [imageView release];

        [self presentModalViewController:imageController animated:TRUE];

        [imageController release];
    }
    /* ============================================================================= */
    else {
        NSString *urlString = path;

        //Play multiple video
        NSURL *url = [NSURL URLWithString:[urlString stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];

        _moviePlayer =  [[MPMoviePlayerController alloc]
                         initWithContentURL:url];

        [[NSNotificationCenter defaultCenter] addObserver:self
                                                 selector:@selector(moviePlayBackDidFinish:)
                                                     name:MPMoviePlayerPlaybackDidFinishNotification
                                                   object:_moviePlayer];

        _moviePlayer.controlStyle = MPMovieControlStyleDefault;
        _moviePlayer.shouldAutoplay = YES;
        [self.view addSubview:_moviePlayer.view];
        [_moviePlayer setFullscreen:YES animated:YES];
    }
}