Three20中的TTThumbViewController类的学习

做相册小程序的时候需要用到Three20库。网上的教程真心少,果然还是要看源代码……

因为本人英语水平极差,变量或者方法设置成自解释的名称也几乎看不懂,所以还是多加点注释好了。留给自己看,如果也能顺便帮到其他人就更好了。

这个文件在Thee20/src/Three20UI/Sources 文件夹下。

我是看一点往上补一点的……如果这文章真的有人看的话,不要心急哈~



涉及到的一些相关文件有:

TTImageViewController

TTPhotoView

TTThumbsTableCell

还有若干


#import "Three20UI/TTThumbsViewController.h"

// UI
#import "Three20UI/TTNavigator.h"
#import "Three20UI/TTThumbsDataSource.h"
#import "Three20UI/TTThumbsTableViewCell.h"
#import "Three20UI/TTPhoto.h"
#import "Three20UI/TTPhotoSource.h"
#import "Three20UI/TTPhotoViewController.h"
#import "Three20UI/UIViewAdditions.h"

// UINavigator
#import "Three20UINavigator/TTGlobalNavigatorMetrics.h"

// UICommon
#import "Three20UICommon/TTGlobalUICommon.h"
#import "Three20UICommon/UIViewControllerAdditions.h"

// Style
#import "Three20Style/TTGlobalStyle.h"
#import "Three20Style/TTStyleSheet.h"

// Core
#import "Three20Core/TTGlobalCoreLocale.h"
#import "Three20Core/TTGlobalCoreRects.h"
#import "Three20Core/TTCorePreprocessorMacros.h"

// 相片缩略图的格子高度。过小会导致图片显示不全,过大会使图片间留很多空白。
static CGFloat kThumbnailRowHeight = 79;


///
///
///
@implementation TTThumbsViewController

@synthesize delegate    = _delegate;
@synthesize photoSource = _photoSource;


///
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
	self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
	if (self) {
		self.statusBarStyle = UIStatusBarStyleBlackTranslucent;	// 将statusBar设为黑色透明。
		self.navigationBarStyle = UIBarStyleBlackTranslucent;	// 将导航栏设置为黑色透明
		self.navigationBarTintColor = nil;	// 将导航栏背景色设为nil
		self.wantsFullScreenLayout = YES;	// 全屏布局。略不懂,待研究。
		self.hidesBottomBarWhenPushed = YES;	// 隐藏TabBar。没用过,不了解。
	}
//	1. 如果仅仅使用TTThumbsViewController的话,那么用完了要手动将状态栏导航栏等等的颜色变为原配置。
//	2. 为了让图片缩略图滚动的时候有那种“还没到头”的感觉,所以要把XX栏都设置成透明的。
	return self;
}


///
// 起始我还不大明白delegate是什么意思⋯⋯待研究。
- (id)initWithDelegate:(id<TTThumbsViewControllerDelegate>)delegate {
	self = [self initWithNibName:nil bundle:nil];
	if (self) {
		self.delegate = delegate;
	}
	
	return self;
}


///
// 这个也不大懂。
- (id)initWithQuery:(NSDictionary*)query {
	id<TTThumbsViewControllerDelegate> delegate = [query objectForKey:@"delegate"];
	if (nil != delegate) {
		self = [self initWithDelegate:delegate];
		
	} else {
		self = [self initWithNibName:nil bundle:nil];
	}
	
	return self;
}


///
- (id)init {
	self = [self initWithNibName:nil bundle:nil];
	if (self) {
	}
	
	return self;
}


///
- (void)dealloc {
	[_photoSource.delegates removeObject:self];
	TT_RELEASE_SAFELY(_photoSource);
	
	[super dealloc];
}


///
///
#pragma mark -
#pragma mark Private


///
// 暂停加载缩略图
// 在viewDidDisapper里面用到。
// 大概涉及到TTThumbsDataSource类和TTThumbsTableViewCell类
// 如果photoSource里面有照片,那么就申请新格子
- (void)suspendLoadingThumbnails:(BOOL)suspended {
	if (_photoSource.maxPhotoIndex >= 0) {
		NSArray* cells = _tableView.visibleCells;
		for (int i = 0; i < cells.count; ++i) {
			TTThumbsTableViewCell* cell = [cells objectAtIndex:i];
			if ([cell isKindOfClass:[TTThumbsTableViewCell class]]) {
				[cell suspendLoading:suspended];
			}
		}
	}
}


///
// 在自身的loadView里面会调用到。
- (void)updateTableLayout {
	self.tableView.contentInset = UIEdgeInsetsMake(TTBarsHeight()+4, 0, 0, 0);
	self.tableView.scrollIndicatorInsets = UIEdgeInsetsMake(TTBarsHeight(), 0, 0, 0);
}


///
// 不知道在哪用到了这个,所以也不知道干嘛用
- (NSString*)URLForPhoto:(id<TTPhoto>)photo {
	if ([photo respondsToSelector:@selector(URLValueWithName:)]) {
		return [photo URLValueWithName:@"TTPhotoViewController"];
		
	} else {
		return nil;
	}
}


///
///
#pragma mark -
#pragma mark UIViewController


///
- (void)loadView {//最上面的RowHeigt就是在这里用的。对tableView进行一些初始化操作。
	[super loadView];
	self.tableView.rowHeight = kThumbnailRowHeight;//设置行高
	// 设置自动调整大小。方法是在UIView里面。
	self.tableView.autoresizingMask =
	UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
	// TTSTULEVAR看起来像是一个全局的设定。易于统一风格吧。
	self.tableView.backgroundColor = TTSTYLEVAR(backgroundColor);
	// 貌似是cell的分割风格。没发现none和singleLine的区别
	self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
	[self updateTableLayout];
}


///
- (void)viewDidAppear:(BOOL)animated {
	[super viewDidAppear:animated];
	[self suspendLoadingThumbnails:NO];
}


///
- (void)viewDidDisappear:(BOOL)animated {
	[self suspendLoadingThumbnails:YES];
	[super viewDidDisappear:animated];
}


///
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
	return TTIsSupportedOrientation(interfaceOrientation);
}


///
- (void)didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation {
	[self updateTableLayout];
	[self.tableView reloadData];
}


///
//
#pragma mark -
#pragma mark UIViewController (TTCategory)


///
- (BOOL)persistView:(NSMutableDictionary*)state {
	NSString* delegate = [[TTNavigator navigator] pathForObject:_delegate];
	if (delegate) {
		[state setObject:delegate forKey:@"delegate"];
	}
	return [super persistView:state];
}


///
- (void)restoreView:(NSDictionary*)state {
	[super restoreView:state];
	NSString* delegate = [state objectForKey:@"delegate"];
	if (delegate) {
		self.delegate = [[TTNavigator navigator] objectForPath:delegate];
	}
}


///
// 这里似乎是设置导航栏上的两个按钮
// 但我还是不明白为什么右边的按钮可以在被Photo调用的时候出现,而左边的只有直接打开Thumbs的时候才有。
- (void)setDelegate:(id<TTThumbsViewControllerDelegate>)delegate {
	_delegate = delegate;
	
	if (_delegate) {
		self.navigationItem.leftBarButtonItem =
		[[[UIBarButtonItem alloc] initWithCustomView:[[[UIView alloc] init]
													  autorelease]]autorelease];
		self.navigationItem.rightBarButtonItem =
		[[[UIBarButtonItem alloc] initWithTitle:TTLocalizedString(@"Done", @"")
										  style:UIBarButtonItemStyleBordered
										 target:self
										 action:@selector(removeFromSupercontroller)] autorelease];
	}
}


///
///
#pragma mark -
#pragma mark TTModelViewController


///
- (void)didRefreshModel {
	[super didRefreshModel];
	self.title = _photoSource.title;
}


///
///
#pragma mark -
#pragma mark TTTableViewController


///
// 不懂
- (CGRect)rectForOverlayView {
	return TTRectContract(CGRectOffset([super rectForOverlayView], 0, TTBarsHeight()-_tableView.top),
						  0, TTBarsHeight());
}


///
///
#pragma mark -
#pragma mark TTThumbsTableViewCellDelegate


///
//没看完。大概是这样:如果URL里有图片就显示,没有的话,就显示一张photo。目测是“找不到图片”的那张
- (void)thumbsTableViewCell:(TTThumbsTableViewCell*)cell didSelectPhoto:(id<TTPhoto>)photo {
	[_delegate thumbsViewController:self didSelectPhoto:photo];
	
	BOOL shouldNavigate = YES;
	if ([_delegate respondsToSelector:@selector(thumbsViewController:shouldNavigateToPhoto:)]) {
		shouldNavigate = [_delegate thumbsViewController:self shouldNavigateToPhoto:photo];
	}
	
	if (shouldNavigate) {
		NSString* URL = [self URLForPhoto:photo];
		if (URL) {
			TTOpenURLFromView(URL, self.view);
			
		} else {
			TTPhotoViewController* controller = [self createPhotoViewController];
			controller.centerPhoto = photo;
			[self.navigationController pushViewController:controller animated:YES];
		}
	}
}


///
///
#pragma mark -
#pragma mark Public


///
// 设置图像源。
- (void)setPhotoSource:(id<TTPhotoSource>)photoSource {
	if (photoSource != _photoSource) {
		[_photoSource release];
		_photoSource = [photoSource retain];
		
		self.title = _photoSource.title;
		self.dataSource = [self createDataSource];
	}
}


///
- (TTPhotoViewController*)createPhotoViewController {
	return [[[TTPhotoViewController alloc] init] autorelease];
}


///
- (id<TTTableViewDataSource>)createDataSource {
	return [[[TTThumbsDataSource alloc] initWithPhotoSource:_photoSource delegate:self] autorelease];
}


@end


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值