matsuda
10/31/2011 - 4:15 AM

TableViewCellにYouTube動画を表示

TableViewCellにYouTube動画を表示

<html>
<head>
<meta name="viewport" content="initial-scale=1.0, user-scalable=no, width=212"/>
<style type="text/css">
    body {
        background-color: transparent;
        color: white;
    }
</style>
</head>
<body style="margin:0">
<div>
<object width="$width$" height="$height$">
<param name="movie" value="http://www.youtube.com/v/$movieId$"></param>
<param name="wmode" value="transparent"></param>
<embed src="http://www.youtube.com/v/$movieId$" type="application/x-shockwave-flash" wmode="transparent" width="$width$" height="$height$"></embed>
</object>
</div>
</body>
</html>
#import "YouTubeCell.h"

#define zYouTubeFrameSize     CGSizeMake(212, 172)

@interface YouTubeCell (PrivateMethods)
- (void)loadYouTubeHTML;
- (void)embedYouTube:(NSString*)movieId size:(CGSize)size;
@end


@implementation PQActivityListItemCell

- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        youTubeView_ = [[UIWebView alloc] initWithFrame:CGRectZero];
        [self.contentView addSubview:youTubeView_];
        [self loadYouTubeHTML];
    }
    return self;
}

- (void)dealloc
{
    // YouTube
    [youTubeView_ release], youTubeView_ = nil;
    [youTubeHTML_ release], youTubeHTML_ = nil;
    [super dealloc];
}

- (void)layoutSubviews
{
    [super layoutSubviews];
    [self layoutYouTubeView];
}

- (void)layoutYouTubeView
{
    youTubeView_.origin = CGPointMake((self.view.width - youTubeView_.width) / 2, 0);
    movieId = @"ApnZTL-AspQ";
    [self embedYouTube:movieId size:zYouTubeFrameSize];
}

#pragma mark - Private methods


- (void)embedYouTube:(NSString*)movieId size:(CGSize)size
{
    if ( movieId && ( [movieId length] > 0 ) ) {
        NSString* html = [youTubeHTML_ stringByReplacingOccurrencesOfString:@"$movieId$" withString:movieId];
        html = [html stringByReplacingOccurrencesOfString:@"$width$" withString:[NSString stringWithFormat:@"%d", (int)size.width]];
        html = [html stringByReplacingOccurrencesOfString:@"$height$" withString:[NSString stringWithFormat:@"%d", (int)size.height]];
        [youTubeView_ loadHTMLString:html baseURL:nil];
    } else {
        [youTubeView_ loadHTMLString:nil baseURL:nil];
    }
    youTubeView_.size = size;
}


- (void)loadYouTubeHTML
{
    NSString *youtubePath = [[NSBundle mainBundle] pathForResource:@"youtube" ofType:@"html"];
    youTubeHTML_ = [[NSString stringWithContentsOfFile:youtubePath encoding:NSUTF8StringEncoding error:nil] retain];
}

@end
#import <Foundation/Foundation.h>

@interface YouTubeCell : UITableViewCell {
    // YouTube
    UIWebView* youTubeView_;
    NSString* youTubeHTML_;
}

@end