since: 2012/03/27
update: 2012/03/27
reference: I touchs: Filter4Cam 學習之 Scroll Horizontally Tables: Part 2
修正與效能調整之二
A. 選取 / 取消選取 濾鏡(Cell)
1. 開啓 HorizontalTableCell.h 檔案, 修改如下:
....
@interface HorizontalTableCell : UITableViewCell <UITableViewDelegate, UITableViewDataSource>
{
//@add
// we will rotate and add as a subview of our cell
UITableView *filterTableView;
// hold the filters for the category we are in
NSArray *filters;
//@add: lastIndexPath in tableView:didSelectRowAtIndexPath:
NSIndexPath *lastIndexPath;
}
//@add
@property (nonatomic, strong) UITableView *filterTableView;
@property (nonatomic, strong) NSArray *filters;
@property (nonatomic, strong) NSIndexPath *lastIndexPath;
@end
2. 開啓 HorizontalTableCell.m 檔案, 修改如下:
....
//@add
@synthesize filterTableView = _filterTableView;
@synthesize filters = _filters;
@synthesize lastIndexPath = _lastIndexPath;
....
//@add: 選取 / 取消選取 filter cell
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
int newRow = [indexPath row];
int oldRow = [self.lastIndexPath row];
// 選取濾鏡
if (newRow != oldRow)
{
self.lastIndexPath = indexPath;
//@TODO: 套用濾鏡功能
}
// 取消選取濾鏡
else
{
//self.lastIndexPath = nil; // -> not work at section:0 row:0
self.lastIndexPath = [NSIndexPath indexPathForRow:-1 inSection:0];
[tableView deselectRowAtIndexPath:indexPath animated:NO];
//@TODO: 恢復原始影像
}
}
....
-----------------------------------------------------------------------------------------------
B. 效能調整: GCD (Grand Central Dispatch)
1. 開啓 HorizontalTableCell.m 檔案, 修改如下:
....
//@add
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
// we used the same reuse identifier as we did in the FilterCell class.
static NSString *cellIdentifier = @"FilterCell";
// we create a standard UITableViewCell for now
//UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
//
//@update
// dequeueReusableCellWithIdentifier: method will returns a regular UITableViewCell,
// so we must cast it to our custom subclass(FilterCell).
//FilterCell *cell = (FilterCell *)[tableView dequeueReusableCellWithIdentifier:cellIdentifier];
//@update for GCD
//
// We added __block prior to our FilterCell cell variable,
// that's going to allow us to use the cell variable inside blocks.
__block FilterCell *cell = (FilterCell *)[tableView dequeueReusableCellWithIdentifier:cellIdentifier];
// If we don't get returned a reusable cell,
//
// then we allocate and initialize one with a custom frame that positions it
// at the very top left corner of its container and makes it
// the same width and height as our cell.
if (cell == nil)
{
//cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
//@update
cell = [[FilterCell alloc] initWithFrame:CGRectMake(0, 0, kCellWidth, kCellHeight)];
}
//cell.textLabel.text = @"The title of the cell in the table within the table :O";
//@update
// we just fetch the current filter dictionary from our filters array
//NSDictionary *currentFilter = [self.filters objectAtIndex:indexPath.row];
//@update for GCD
__block NSDictionary *currentFilter = [self.filters objectAtIndex:indexPath.row];
//@add for GCD
//
// We create a new dispatch queue variable and as GCD
// to give us one of the available queues.
dispatch_queue_t concurrentQueue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
//cell.sampleImage.image = [UIImage imageNamed:[currentFilter objectForKey:@"ImageName"]];
//@update for GCD
//
dispatch_async(concurrentQueue, ^{
// Run the UIImage creation in the background
UIImage *image = nil;
image = [UIImage imageNamed:[currentFilter objectForKey:@"ImageName"]];
// Get the main thread, and set the sampleImage image to the one we just loaded asynchronously.
dispatch_async(dispatch_get_main_queue(), ^{
[cell.sampleImage setImage:image];
});
});
//@title text: we don't need to load that, just read it from our array of filters.
cell.titleLabel.text = [currentFilter objectForKey:@"Title"];
return cell;
}
....
2. 編譯並執行
當圖檔較多時, 你便會注意到圖片的載入較為順暢.
2012年3月27日 星期二
2012年3月26日 星期一
Filter4Cam 實作: 13. 濾鏡表單之六
since: 2012/03/26
update: 2012/03/26
reference: I touchs: Filter4Cam 學習之 Scroll Horizontally Tables: Part 2
修正與效能調整之一
A. 修正濾鏡標題標籤之問題
1. 說明:
a. 當點選某個濾鏡的標題標籤(title label)時, 背景色不見了.(全透明)
b. 為什麼 cell 被點選時, UILabel 會變成透明的? 它是藉由內部的運作來改變
背景色的. 為了防止此種情況, 我們必須去建立 UILabel 的子類別, 並且覆寫
二個方法來讓一切正常.
2. 建立 UILabel 子類別
Xcode > File > New > File...
> iOS > Cocoa Touch > Objective-C class > Next
> Next > Create
3. 開啓 FilterTitleLabel.h 檔案, 修改如下:
#import <UIKit/UIKit.h>
//@add
#import "ConstantDefined.h"
@interface FilterTitleLabel : UILabel
{
}
//@add: set a persistent background color
- (void)setPersistentBackgroundColor:(UIColor *)color;
@end
4. 開啓 FilterTitleLabel.m 檔案, 修改如下:
#import "FilterTitleLabel.h"
@implementation FilterTitleLabel
//@add: set the UILabel's background color
- (void)setPersistentBackgroundColor:(UIColor *)color
{
super.backgroundColor = color;
}
//@add: overide
//
// when it tries calling it's own setBackgroundColor method we will not do anything,
// thus preventing the color from changing when selecting our cell.
- (void)setBackgroundColor:(UIColor *)color
{
// do nothing - background color never changes
}
//@add: overide
//
// Let the text inside the label can have a little bit of padding on the left and right side
- (void)drawTextInRect:(CGRect)rect
{
CGFloat newWidth = rect.size.width - kFilterTitleLabelPadding;
CGFloat newHeight = rect.size.height;
CGRect newRect = CGRectMake(kFilterTitleLabelPadding * 0.5, 0, newWidth, newHeight);
[super drawTextInRect:newRect];
}
//@update: comment it
/*
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
// Initialization code
}
return self;
}
*/
/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect
{
// Drawing code
}
*/
@end
5. 開啓 FilterCell.h 檔案, 修改如下:
#import <UIKit/UIKit.h>
//@add
#import "ConstantDefined.h"
#import "FilterTitleLabel.h"
//@add
@class FilterTitleLabel;
@interface FilterCell : UITableViewCell
{
//@add
// hold the sample picture for our filter
UIImageView *sampleImage; // 濾鏡效果的圖例
// the title of our filter
//UILabel *titleLabel;
//@update
FilterTitleLabel *titleLabel;
}
//@add
@property (nonatomic, strong) UIImageView *sampleImage;
//@property (nonatomic, strong) UILabel *titleLabel;
//@update
@property (nonatomic, strong) FilterTitleLabel *titleLabel;
@end
6. 開啓 FilterCell.m 檔案, 修改如下:
....
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
....
//self.titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, self.sampleImage.frame.size.height * 0.632, self.sampleImage.frame.size.width, self.sampleImage.frame.size.height * 0.37)];
//@update: use FilterTitleLabel
self.titleLabel = [[FilterTitleLabel alloc] initWithFrame:CGRectMake(0, self.sampleImage.frame.size.height * 0.632, self.sampleImage.frame.size.width, self.sampleImage.frame.size.height * 0.37)];
....
//self.titleLabel.backgroundColor = kFilterCellTitleLabelBackgroundColor;
//@update: use setPersistentBackgroundColor
[self.titleLabel setPersistentBackgroundColor:kFilterCellTitleLabelBackgroundColor];
....
}
return self;
}
....
7. 編譯並執行:
------------------------------------------------------------------------------------------------------
B. 修正濾鏡清單上下捲動時的問題
1. 說明:
a. 在此專案濾鏡只有一列, 不會遇到此問題, 但仍然先將功能調好, 以後或許會用到.
b. 當濾鏡清單有多列時, 上下捲動可能會看到重覆出現的例圖, 並且分類下的圖也
不正確.
2. 開啓 ViewController.h 檔案, 修改如下:
....
@interface ViewController : GLKViewController <AVCaptureVideoDataOutputSampleBufferDelegate, UITableViewDelegate, UITableViewDataSource>
{
....
//@add for reusable Cells
//
// When we first load our app, create an array of horizontalTableCell cells
// This will store our horizontalTableCell instances for reuse.
NSMutableArray *reusableCells;
....
}
....
//@add for reusable Cells
@property (nonatomic, strong) NSMutableArray *reusableCells;
....
//@add for initialize reusableCells
- (void)initReusableCells;
....
3. 開啓 ViewController.m 檔案, 修改如下:
....
//@add for reusable Cells
@synthesize reusableCells = _reusableCells;
....
- (void)viewDidUnload
{
[super viewDidUnload];
//@add
if ([EAGLContext currentContext] == self.glContext) {
[EAGLContext setCurrentContext:nil];
}
self.glContext = nil;
self.filterDictionary = nil;
self.reusableCells = nil;
}
....
//@add for initialize reusableCells
- (void)initReusableCells
{
if (!self.reusableCells)
{
NSSortDescriptor *sortDescriptor = [NSSortDescriptor sortDescriptorWithKey:nil ascending:YES selector:@selector(localizedCompare:)];
// sort our categories and store all of the sorted dictionary keys in an array.
NSArray *sortedCategories = [self.filterDictionary.allKeys sortedArrayUsingDescriptors:[NSArray arrayWithObject:sortDescriptor]];
NSString *categoryName;
NSArray *currentCategory;
self.reusableCells = [NSMutableArray array];
for (int i = 0; i < [self.filterDictionary.allKeys count]; i++)
{
// create a reusable cell for each
//
// we will overrode cell's initWithFrame method later, and it will pass
// in the proper frame size
HorizontalTableCell *cell = [[HorizontalTableCell alloc] initWithFrame:CGRectMake(0, 0, 320, 416)];
categoryName = [sortedCategories objectAtIndex:i];
// get the category array for this corresponding cell
currentCategory = [self.filterDictionary objectForKey:categoryName];
// store it within it's filters property
cell.filters = [NSArray arrayWithArray:currentCategory];
// add the cell we just created to our reusable cells array
//
// reusableCells array contains cells already sorted in the same order
// as our categories
//
// That way we just get the object at the necessary index
[self.reusableCells addObject:cell];
}
}
}
....
- (void)viewDidLoad
{
....
//@add for initialize reusableCells
[self initReusableCells];
}
....
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
/*
....
*/
//@update: use reusableCells
//
// we already created a cache of reusable cells, all we need to do is
// fetch the appropriate one.
HorizontalTableCell *cell = [self.reusableCells objectAtIndex:indexPath.section];
return cell;
}
....
4. 開啓 HorizontalTableCell.m 檔案, 修改如下:
....
//@update: comment it
// Since we are no longer using the provided method for reusable cells,
// we don't need an identifier for them.
/*
- (NSString *)reuseIdentifier
{
return @"HorizontalCell";
}
*/
....
5. 編譯並執行
此專案在 UI 外觀上並不會有所改變.
update: 2012/03/26
reference: I touchs: Filter4Cam 學習之 Scroll Horizontally Tables: Part 2
修正與效能調整之一
A. 修正濾鏡標題標籤之問題
1. 說明:
a. 當點選某個濾鏡的標題標籤(title label)時, 背景色不見了.(全透明)
b. 為什麼 cell 被點選時, UILabel 會變成透明的? 它是藉由內部的運作來改變
背景色的. 為了防止此種情況, 我們必須去建立 UILabel 的子類別, 並且覆寫
二個方法來讓一切正常.
Xcode > File > New > File...
> iOS > Cocoa Touch > Objective-C class > Next
Class: FilterTitleLabel
Subclass of: UILabel> Next > Create
3. 開啓 FilterTitleLabel.h 檔案, 修改如下:
#import <UIKit/UIKit.h>
//@add
#import "ConstantDefined.h"
@interface FilterTitleLabel : UILabel
{
}
//@add: set a persistent background color
- (void)setPersistentBackgroundColor:(UIColor *)color;
@end
4. 開啓 FilterTitleLabel.m 檔案, 修改如下:
#import "FilterTitleLabel.h"
@implementation FilterTitleLabel
//@add: set the UILabel's background color
- (void)setPersistentBackgroundColor:(UIColor *)color
{
super.backgroundColor = color;
}
//@add: overide
//
// when it tries calling it's own setBackgroundColor method we will not do anything,
// thus preventing the color from changing when selecting our cell.
- (void)setBackgroundColor:(UIColor *)color
{
// do nothing - background color never changes
}
//@add: overide
//
// Let the text inside the label can have a little bit of padding on the left and right side
- (void)drawTextInRect:(CGRect)rect
{
CGFloat newWidth = rect.size.width - kFilterTitleLabelPadding;
CGFloat newHeight = rect.size.height;
CGRect newRect = CGRectMake(kFilterTitleLabelPadding * 0.5, 0, newWidth, newHeight);
[super drawTextInRect:newRect];
}
//@update: comment it
/*
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
// Initialization code
}
return self;
}
*/
/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect
{
// Drawing code
}
*/
@end
5. 開啓 FilterCell.h 檔案, 修改如下:
#import <UIKit/UIKit.h>
//@add
#import "ConstantDefined.h"
#import "FilterTitleLabel.h"
//@add
@class FilterTitleLabel;
@interface FilterCell : UITableViewCell
{
//@add
// hold the sample picture for our filter
UIImageView *sampleImage; // 濾鏡效果的圖例
// the title of our filter
//UILabel *titleLabel;
//@update
FilterTitleLabel *titleLabel;
}
//@add
@property (nonatomic, strong) UIImageView *sampleImage;
//@property (nonatomic, strong) UILabel *titleLabel;
//@update
@property (nonatomic, strong) FilterTitleLabel *titleLabel;
@end
6. 開啓 FilterCell.m 檔案, 修改如下:
....
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
....
//self.titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, self.sampleImage.frame.size.height * 0.632, self.sampleImage.frame.size.width, self.sampleImage.frame.size.height * 0.37)];
//@update: use FilterTitleLabel
self.titleLabel = [[FilterTitleLabel alloc] initWithFrame:CGRectMake(0, self.sampleImage.frame.size.height * 0.632, self.sampleImage.frame.size.width, self.sampleImage.frame.size.height * 0.37)];
....
//self.titleLabel.backgroundColor = kFilterCellTitleLabelBackgroundColor;
//@update: use setPersistentBackgroundColor
[self.titleLabel setPersistentBackgroundColor:kFilterCellTitleLabelBackgroundColor];
....
}
return self;
}
....
7. 編譯並執行:
------------------------------------------------------------------------------------------------------
B. 修正濾鏡清單上下捲動時的問題
1. 說明:
a. 在此專案濾鏡只有一列, 不會遇到此問題, 但仍然先將功能調好, 以後或許會用到.
b. 當濾鏡清單有多列時, 上下捲動可能會看到重覆出現的例圖, 並且分類下的圖也
不正確.
2. 開啓 ViewController.h 檔案, 修改如下:
....
@interface ViewController : GLKViewController <AVCaptureVideoDataOutputSampleBufferDelegate, UITableViewDelegate, UITableViewDataSource>
{
....
//@add for reusable Cells
//
// When we first load our app, create an array of horizontalTableCell cells
// This will store our horizontalTableCell instances for reuse.
NSMutableArray *reusableCells;
....
}
....
//@add for reusable Cells
@property (nonatomic, strong) NSMutableArray *reusableCells;
....
//@add for initialize reusableCells
- (void)initReusableCells;
....
3. 開啓 ViewController.m 檔案, 修改如下:
....
//@add for reusable Cells
@synthesize reusableCells = _reusableCells;
....
- (void)viewDidUnload
{
[super viewDidUnload];
//@add
if ([EAGLContext currentContext] == self.glContext) {
[EAGLContext setCurrentContext:nil];
}
self.glContext = nil;
self.filterDictionary = nil;
self.reusableCells = nil;
}
....
//@add for initialize reusableCells
- (void)initReusableCells
{
if (!self.reusableCells)
{
NSSortDescriptor *sortDescriptor = [NSSortDescriptor sortDescriptorWithKey:nil ascending:YES selector:@selector(localizedCompare:)];
// sort our categories and store all of the sorted dictionary keys in an array.
NSArray *sortedCategories = [self.filterDictionary.allKeys sortedArrayUsingDescriptors:[NSArray arrayWithObject:sortDescriptor]];
NSString *categoryName;
NSArray *currentCategory;
self.reusableCells = [NSMutableArray array];
for (int i = 0; i < [self.filterDictionary.allKeys count]; i++)
{
// create a reusable cell for each
//
// we will overrode cell's initWithFrame method later, and it will pass
// in the proper frame size
HorizontalTableCell *cell = [[HorizontalTableCell alloc] initWithFrame:CGRectMake(0, 0, 320, 416)];
categoryName = [sortedCategories objectAtIndex:i];
// get the category array for this corresponding cell
currentCategory = [self.filterDictionary objectForKey:categoryName];
// store it within it's filters property
cell.filters = [NSArray arrayWithArray:currentCategory];
// add the cell we just created to our reusable cells array
//
// reusableCells array contains cells already sorted in the same order
// as our categories
//
// That way we just get the object at the necessary index
[self.reusableCells addObject:cell];
}
}
}
....
- (void)viewDidLoad
{
....
//@add for initialize reusableCells
[self initReusableCells];
}
....
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
/*
....
*/
//@update: use reusableCells
//
// we already created a cache of reusable cells, all we need to do is
// fetch the appropriate one.
HorizontalTableCell *cell = [self.reusableCells objectAtIndex:indexPath.section];
return cell;
}
....
4. 開啓 HorizontalTableCell.m 檔案, 修改如下:
....
//@update: comment it
// Since we are no longer using the provided method for reusable cells,
// we don't need an identifier for them.
/*
- (NSString *)reuseIdentifier
{
return @"HorizontalCell";
}
*/
....
5. 編譯並執行
此專案在 UI 外觀上並不會有所改變.
標籤:
開發筆記,
Filter4Cam,
iphone
2012年3月24日 星期六
Filter4Cam 實作: 12. 濾鏡表單之五
since: 2012/03/24
update: 2012/04/19
reference: I touchs: Filter4Cam 學習之 Scroll Horizontally Tables: Part 2
客製化 TableViewCell 之二
A. 說明
目前已經有垂直的 table view, 並且建立了包含有水平 table 的客製化
UITableViewCell, 但是為了讓介面看起來更酷, 所以我們需要另一個
客製化的 cell, 來將其放到水平的 table 裡.
架構一: 垂直的 table 包含客製化的 TableViewCell (HorizontalTableCell)
架構二: 客製化的 TableViewCell (HorizontalTableCell) 包含水平的 table
(filter Table View)
架構三: 水平的 table (filter Table View) 裡包含客製化的 cell (filter cell)
接下來要先建立架構三中的客製化 cell (filter cell)
------------------------------------------------------------------------
B. 建立 UITableViewCell 子類別
1. Xcode > File > New > File...
> iOS > Cocoa Touch > Objective-C class > Next
> Next > Create
2. 開啓 FilterCell.h 檔案, 修改如下:
#import <UIKit/UIKit.h>
//@add
#import "ConstantDefined.h"
@interface FilterCell : UITableViewCell
{
//@add
// hold the sample picture for our filter
UIImageView *sampleImage; // 濾鏡效果的圖例
// the title of our filter
UILabel *titleLabel;
}
//@add
@property (nonatomic, strong) UIImageView *sampleImage;
@property (nonatomic, strong) UILabel *titleLabel;
@end
3. 開啓 FilterCell.m 檔案, 修改如下:
#import "FilterCell.h"
@implementation FilterCell
//@add
@synthesize sampleImage = _sampleImage;
@synthesize titleLabel = _titleLabel;
//@add
// we are using a custom UITableViewCell, we must manually define
// the reuse identifier
- (NSString *)reuseIdentifier
{
return @"FilterCell";
}
//@add
- (void)dealloc
{
self.sampleImage = nil;
self.titleLabel = nil;
}
/*
- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
if (self) {
// Initialization code
}
return self;
}
*/
/*
- (void)setSelected:(BOOL)selected animated:(BOOL)animated
{
[super setSelected:selected animated:animated];
// Configure the view for the selected state
}
*/
//@add: override the initWithFrame method with our custom one
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
// Initialization code
//@add
// We allocate and initialize a new UIImageView and give it a custom frame.
//
// We don't position it at 0, 0 (the top left corner) but we give it a little bit
// of padding, this will allow for separation between each filter cell.
self.sampleImage = [[UIImageView alloc] initWithFrame:CGRectMake(kFilterCellHorizontalInnerPadding, kFilterCellVerticalInnerPadding, kCellWidth - kFilterCellHorizontalInnerPadding * 2, kCellHeight - kFilterCellVerticalInnerPadding * 2)];
// 設為完全不透明, 能增進效能
self.sampleImage.opaque = YES;
// we add the sampleImage as a subview of the cell's content view
[self.contentView addSubview:self.sampleImage];
// We create a UILabel and for the frame we make some adjustments
// so that it has some padding on the left side
// (thus preventing text to start from the very edge of the cell)
//
// And we make the height of the title label just 37% of the total height
// of the picture and we move it down a bit so it fits the bottom part of the cell.
//
// This will make the title not cover the entire article cell and it's image,
// but just the bottom half part.
self.titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, self.sampleImage.frame.size.height * 0.632, self.sampleImage.frame.size.width, self.sampleImage.frame.size.height * 0.37)];
// then set the label as opaque
self.titleLabel.opaque = YES;
// change the background color to a semitransparent gray (半透明灰)
self.titleLabel.backgroundColor = [UIColor colorWithRed:0 green:0.4745098 blue:0.29019808 alpha:0.9];
// give it a white color for the text
self.titleLabel.textColor = [UIColor whiteColor];
// set its font to the default bold font with a size of 11 points
self.titleLabel.font = [UIFont boldSystemFontOfSize:11];
// allow the label to show in two lines
self.titleLabel.numberOfLines = 2;
// add it as a subview of the sampleImage.
[self.sampleImage addSubview:self.titleLabel];
// set the background color of the cell to something along the lines of our interface
self.backgroundColor = [UIColor colorWithRed:0 green:0.40784314 blue:0.21568627 alpha:1.0];
// change the size of the cell's background view to the same size as
// our sampleImage
self.selectedBackgroundView = [[UIView alloc] initWithFrame:self.sampleImage.frame];
// give it the custom background color we added in our ConstantDefined.h
// file earlier.
self.selectedBackgroundView.backgroundColor = kHorizontalTableSelectedBackgroundColor;
// rotate the cell 90 degrees clockwise (順時針旋轉 90 度)
//
// 因為這些 cells 會在已旋轉的 table view 裡, 如果在此不作旋轉的話,
// 將會看到被逆時針旋轉 90 度的結果
self.transform = CGAffineTransformMakeRotation(M_PI * 0.5);
}
return self;
}
@end
------------------------------------------------------------------------
C. 最後一個步驟
1. 開啓 HorizontalTableCell.h 檔案, 修改如下:
#import <UIKit/UIKit.h>
//@add
#import "ConstantDefined.h"
#import "FilterCell.h"
....
2. 開啓 HorizontalTableCell.m 檔案, 修改如下:
....
//@add
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
// we used the same reuse identifier as we did in the FilterCell class.
static NSString *cellIdentifier = @"FilterCell";
// we create a standard UITableViewCell for now
//UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
//
//@update
// dequeueReusableCellWithIdentifier: method will returns a regular
// UITableViewCell, so we must cast it to our custom subclass(FilterCell).
FilterCell *cell = (FilterCell *)[tableView dequeueReusableCellWithIdentifier:cellIdentifier];
// If we don't get returned a reusable cell,
//
// then we allocate and initialize one with a custom frame that positions it
// at the very top left corner of its container and makes it
// the same width and height as our cell.
if (cell == nil)
{
//cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
//@update
cell = [[FilterCell alloc] initWithFrame:CGRectMake(0, 0, kCellWidth, kCellHeight)];
}
//cell.textLabel.text = @"The title of the cell in the table within the table :O";
//@update
// we just fetch the current filter dictionary from our filters array
NSDictionary *currentFilter = [self.filters objectAtIndex:indexPath.row];
cell.sampleImage.image = [UIImage imageNamed:[currentFilter objectForKey:@"ImageName"]];
cell.titleLabel.text = [currentFilter objectForKey:@"Title"];
return cell;
}
....
3. 開啓 ViewController.h 檔案, 修改如下:
....
//@add
#import "Filter4CamHelper.h"
#import "ConstantDefined.h"
#import "HorizontalTableCell.h"
....
4. 開啓 ViewController.m 檔案, 修改如下:
....
//@add
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
// Return the number of rows in the section.
//return 2;
//@update: creating a sort descriptor which will sort the objects in ascending order(升冪)
/*
NSSortDescriptor *sortDescriptor = [NSSortDescriptor sortDescriptorWithKey:nil ascending:YES selector:@selector(localizedCompare:)];
NSArray *sortedCategories = [self.filterDictionary.allKeys sortedArrayUsingDescriptors:[NSArray arrayWithObject:sortDescriptor]];
NSString *categoryName = [sortedCategories objectAtIndex:section];
NSArray *currentCategory = [self.filterDictionary objectForKey:categoryName];
return [currentCategory count];
*/
//@update
return 1;
}
....
//@add
- (void)awakeFromNib
{
// changes the background color of our vertical table
[self.filterListTableView setBackgroundColor:kVerticalTableBackgroundColor];
// changes the height of each row so that it can now fit our custom Filter Cell.
self.filterListTableView.rowHeight = kCellHeight + (kRowVerticalPadding * 0.5) + ((kRowVerticalPadding * 0.5) * 0.5);
}
....
// Customize the appearance of table view cells.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
//static NSString *CellIdentifier = @"Cell";
//@update: create a reuse identifier for our cell
static NSString *CellIdentifier = @"HorizontalCell";
//UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
//@update: try to dequeue one
HorizontalTableCell *cell = (HorizontalTableCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
// if that's not possible we create one.
if (cell == nil)
{
//cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
//@update
cell = [[HorizontalTableCell alloc] initWithFrame:CGRectMake(0, 0, tableView.frame.size.width, tableView.frame.size.height)];
}
//@add: sort our categories
NSSortDescriptor *sortDescriptor = [NSSortDescriptor sortDescriptorWithKey:nil ascending:YES selector:@selector(localizedCompare:)];
// get the current category's array of filters
NSArray *sortedCategories = [self.filterDictionary.allKeys sortedArrayUsingDescriptors:[NSArray arrayWithObject:sortDescriptor]];
// fetching a single filter category name
NSString *categoryName = [sortedCategories objectAtIndex:indexPath.section];
// fetching filters for a single category
NSArray *currentCategory = [self.filterDictionary objectForKey:categoryName];
//NSDictionary *currentFilter = [currentCategory objectAtIndex:indexPath.row];
//cell.textLabel.text = [currentFilter objectForKey:@"Title"];
//cell.imageView.image = [UIImage imageNamed:[currentFilter objectForKey:@"ImageName"]];
//@update:pass the entire array to our HorizontalTableCell
cell.filters = currentCategory;
return cell;
}
//@add(此處應該不會用到)
/*
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
NSLog(@"Filter List Row => section:%d row:%d", [indexPath section], [indexPath row]);
}
*/
....
5. 編譯並執行
------------------------------------------------------------------------
D. UI 調整 (update: 2012/03/25)
移除 section header, 並調整 cell 位置
1. 開啓 FilterCell.m 檔案, 修改如下:
....
//@add: override the initWithFrame method with our custom one
- (id)initWithFrame:(CGRect)frame
{
....
// change the background color to a semitransparent gray (半透明灰)
// (Filter Cell 標題標籤的背景顏色)
//self.titleLabel.backgroundColor = [UIColor colorWithRed:0 green:0.4745098 blue:0.29019808 alpha:0.9];
//@update
self.titleLabel.backgroundColor = kFilterCellTitleLabelBackgroundColor;
....
}
....
2. 開啓 ViewController.m 檔案, 修改如下:
....
//@add
- (UITableView *)filterListTableView
{
if (_filterListTableView == nil) {
//_filterListTableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 380, self.view.frame.size.width, 100) style:UITableViewStyleGrouped];
//@update: kOverlayTableViewRowHeight = 106
_filterListTableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 374, self.view.frame.size.width, kOverlayTableViewRowHeight) style:UITableViewStyleGrouped];
}
return _filterListTableView;
}....
//@add for setting camera Overlay
- (void)cameraOverlay
{
[self.filterLensImageView setFrame:CGRectMake(20, 80, 280, 280)];
[self.view addSubview:self.filterLensImageView];
//@add
self.filterListTableView.delegate = self;
self.filterListTableView.dataSource = self;
//@add: kOverlayTableViewRowHeight = 106
self.filterListTableView.rowHeight = kOverlayTableViewRowHeight;
//@add: 將 section Header Height 設為 0
self.filterListTableView.sectionHeaderHeight = 0;
// 不透明度, 會影響到所有的 subview 及 table 內的元件
//self.filterListTableView.alpha = 0.2;
//@add: 取消捲軸功能
self.filterListTableView.scrollEnabled = NO;
//@add: 將 Table (最下方的)分隔線設為: 不透明灰 (update: 2012/04/19)
self.filterListTableView.separatorColor = kVerticalTableSeparatorColor;
//@add: changes the background color of our vertical table
// 垂直方向 table view 的背景顏色, 目前設為: 不透明灰
// 由於已設定 scrollEnabled = NO, 因此也看不到設定的顏色
[self.filterListTableView setBackgroundColor:kVerticalTableBackgroundColor];
[self.view addSubview:self.filterListTableView];
}
....
//@add: provides from UITableViewDataSource
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
//return section == 0 ? kHeadlineSectionHeight : kRegularSectionHeight;
//@update
return 0;
}
//@add: provides from UITableViewDataSource
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
/*
....
*/
//@update
return nil;
}
....
//@update: comment it
/*
- (void)awakeFromNib
{
....
}
*/
....
3. 編譯並執行(有再調整過顏色)
update: 2012/04/19
reference: I touchs: Filter4Cam 學習之 Scroll Horizontally Tables: Part 2
客製化 TableViewCell 之二
A. 說明
目前已經有垂直的 table view, 並且建立了包含有水平 table 的客製化
UITableViewCell, 但是為了讓介面看起來更酷, 所以我們需要另一個
客製化的 cell, 來將其放到水平的 table 裡.
架構一: 垂直的 table 包含客製化的 TableViewCell (HorizontalTableCell)
架構二: 客製化的 TableViewCell (HorizontalTableCell) 包含水平的 table
(filter Table View)
架構三: 水平的 table (filter Table View) 裡包含客製化的 cell (filter cell)
接下來要先建立架構三中的客製化 cell (filter cell)
------------------------------------------------------------------------
B. 建立 UITableViewCell 子類別
1. Xcode > File > New > File...
> iOS > Cocoa Touch > Objective-C class > Next
Class: FilterCell
Subclass of: UITableViewCell> Next > Create
2. 開啓 FilterCell.h 檔案, 修改如下:
#import <UIKit/UIKit.h>
//@add
#import "ConstantDefined.h"
@interface FilterCell : UITableViewCell
{
//@add
// hold the sample picture for our filter
UIImageView *sampleImage; // 濾鏡效果的圖例
// the title of our filter
UILabel *titleLabel;
}
//@add
@property (nonatomic, strong) UIImageView *sampleImage;
@property (nonatomic, strong) UILabel *titleLabel;
@end
3. 開啓 FilterCell.m 檔案, 修改如下:
#import "FilterCell.h"
@implementation FilterCell
//@add
@synthesize sampleImage = _sampleImage;
@synthesize titleLabel = _titleLabel;
//@add
// we are using a custom UITableViewCell, we must manually define
// the reuse identifier
- (NSString *)reuseIdentifier
{
return @"FilterCell";
}
//@add
- (void)dealloc
{
self.sampleImage = nil;
self.titleLabel = nil;
}
/*
- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
if (self) {
// Initialization code
}
return self;
}
*/
/*
- (void)setSelected:(BOOL)selected animated:(BOOL)animated
{
[super setSelected:selected animated:animated];
// Configure the view for the selected state
}
*/
//@add: override the initWithFrame method with our custom one
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
// Initialization code
//@add
// We allocate and initialize a new UIImageView and give it a custom frame.
//
// We don't position it at 0, 0 (the top left corner) but we give it a little bit
// of padding, this will allow for separation between each filter cell.
self.sampleImage = [[UIImageView alloc] initWithFrame:CGRectMake(kFilterCellHorizontalInnerPadding, kFilterCellVerticalInnerPadding, kCellWidth - kFilterCellHorizontalInnerPadding * 2, kCellHeight - kFilterCellVerticalInnerPadding * 2)];
// 設為完全不透明, 能增進效能
self.sampleImage.opaque = YES;
// we add the sampleImage as a subview of the cell's content view
[self.contentView addSubview:self.sampleImage];
// We create a UILabel and for the frame we make some adjustments
// so that it has some padding on the left side
// (thus preventing text to start from the very edge of the cell)
//
// And we make the height of the title label just 37% of the total height
// of the picture and we move it down a bit so it fits the bottom part of the cell.
//
// This will make the title not cover the entire article cell and it's image,
// but just the bottom half part.
self.titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, self.sampleImage.frame.size.height * 0.632, self.sampleImage.frame.size.width, self.sampleImage.frame.size.height * 0.37)];
// then set the label as opaque
self.titleLabel.opaque = YES;
// change the background color to a semitransparent gray (半透明灰)
self.titleLabel.backgroundColor = [UIColor colorWithRed:0 green:0.4745098 blue:0.29019808 alpha:0.9];
// give it a white color for the text
self.titleLabel.textColor = [UIColor whiteColor];
// set its font to the default bold font with a size of 11 points
self.titleLabel.font = [UIFont boldSystemFontOfSize:11];
// allow the label to show in two lines
self.titleLabel.numberOfLines = 2;
// add it as a subview of the sampleImage.
[self.sampleImage addSubview:self.titleLabel];
// set the background color of the cell to something along the lines of our interface
self.backgroundColor = [UIColor colorWithRed:0 green:0.40784314 blue:0.21568627 alpha:1.0];
// change the size of the cell's background view to the same size as
// our sampleImage
self.selectedBackgroundView = [[UIView alloc] initWithFrame:self.sampleImage.frame];
// give it the custom background color we added in our ConstantDefined.h
// file earlier.
self.selectedBackgroundView.backgroundColor = kHorizontalTableSelectedBackgroundColor;
// rotate the cell 90 degrees clockwise (順時針旋轉 90 度)
//
// 因為這些 cells 會在已旋轉的 table view 裡, 如果在此不作旋轉的話,
// 將會看到被逆時針旋轉 90 度的結果
self.transform = CGAffineTransformMakeRotation(M_PI * 0.5);
}
return self;
}
@end
------------------------------------------------------------------------
C. 最後一個步驟
1. 開啓 HorizontalTableCell.h 檔案, 修改如下:
#import <UIKit/UIKit.h>
//@add
#import "ConstantDefined.h"
#import "FilterCell.h"
....
2. 開啓 HorizontalTableCell.m 檔案, 修改如下:
....
//@add
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
// we used the same reuse identifier as we did in the FilterCell class.
static NSString *cellIdentifier = @"FilterCell";
// we create a standard UITableViewCell for now
//UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
//
//@update
// dequeueReusableCellWithIdentifier: method will returns a regular
// UITableViewCell, so we must cast it to our custom subclass(FilterCell).
FilterCell *cell = (FilterCell *)[tableView dequeueReusableCellWithIdentifier:cellIdentifier];
// If we don't get returned a reusable cell,
//
// then we allocate and initialize one with a custom frame that positions it
// at the very top left corner of its container and makes it
// the same width and height as our cell.
if (cell == nil)
{
//cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
//@update
cell = [[FilterCell alloc] initWithFrame:CGRectMake(0, 0, kCellWidth, kCellHeight)];
}
//cell.textLabel.text = @"The title of the cell in the table within the table :O";
//@update
// we just fetch the current filter dictionary from our filters array
NSDictionary *currentFilter = [self.filters objectAtIndex:indexPath.row];
cell.sampleImage.image = [UIImage imageNamed:[currentFilter objectForKey:@"ImageName"]];
cell.titleLabel.text = [currentFilter objectForKey:@"Title"];
return cell;
}
....
3. 開啓 ViewController.h 檔案, 修改如下:
....
//@add
#import "Filter4CamHelper.h"
#import "ConstantDefined.h"
#import "HorizontalTableCell.h"
....
4. 開啓 ViewController.m 檔案, 修改如下:
....
//@add
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
// Return the number of rows in the section.
//return 2;
//@update: creating a sort descriptor which will sort the objects in ascending order(升冪)
/*
NSSortDescriptor *sortDescriptor = [NSSortDescriptor sortDescriptorWithKey:nil ascending:YES selector:@selector(localizedCompare:)];
NSArray *sortedCategories = [self.filterDictionary.allKeys sortedArrayUsingDescriptors:[NSArray arrayWithObject:sortDescriptor]];
NSString *categoryName = [sortedCategories objectAtIndex:section];
NSArray *currentCategory = [self.filterDictionary objectForKey:categoryName];
return [currentCategory count];
*/
//@update
return 1;
}
....
//@add
- (void)awakeFromNib
{
// changes the background color of our vertical table
[self.filterListTableView setBackgroundColor:kVerticalTableBackgroundColor];
// changes the height of each row so that it can now fit our custom Filter Cell.
self.filterListTableView.rowHeight = kCellHeight + (kRowVerticalPadding * 0.5) + ((kRowVerticalPadding * 0.5) * 0.5);
}
....
// Customize the appearance of table view cells.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
//static NSString *CellIdentifier = @"Cell";
//@update: create a reuse identifier for our cell
static NSString *CellIdentifier = @"HorizontalCell";
//UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
//@update: try to dequeue one
HorizontalTableCell *cell = (HorizontalTableCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
// if that's not possible we create one.
if (cell == nil)
{
//cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
//@update
cell = [[HorizontalTableCell alloc] initWithFrame:CGRectMake(0, 0, tableView.frame.size.width, tableView.frame.size.height)];
}
//@add: sort our categories
NSSortDescriptor *sortDescriptor = [NSSortDescriptor sortDescriptorWithKey:nil ascending:YES selector:@selector(localizedCompare:)];
// get the current category's array of filters
NSArray *sortedCategories = [self.filterDictionary.allKeys sortedArrayUsingDescriptors:[NSArray arrayWithObject:sortDescriptor]];
// fetching a single filter category name
NSString *categoryName = [sortedCategories objectAtIndex:indexPath.section];
// fetching filters for a single category
NSArray *currentCategory = [self.filterDictionary objectForKey:categoryName];
//NSDictionary *currentFilter = [currentCategory objectAtIndex:indexPath.row];
//cell.textLabel.text = [currentFilter objectForKey:@"Title"];
//cell.imageView.image = [UIImage imageNamed:[currentFilter objectForKey:@"ImageName"]];
//@update:pass the entire array to our HorizontalTableCell
cell.filters = currentCategory;
return cell;
}
//@add(此處應該不會用到)
/*
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
NSLog(@"Filter List Row => section:%d row:%d", [indexPath section], [indexPath row]);
}
*/
....
5. 編譯並執行
D. UI 調整 (update: 2012/03/25)
移除 section header, 並調整 cell 位置
1. 開啓 FilterCell.m 檔案, 修改如下:
....
//@add: override the initWithFrame method with our custom one
- (id)initWithFrame:(CGRect)frame
{
....
// change the background color to a semitransparent gray (半透明灰)
// (Filter Cell 標題標籤的背景顏色)
//self.titleLabel.backgroundColor = [UIColor colorWithRed:0 green:0.4745098 blue:0.29019808 alpha:0.9];
//@update
self.titleLabel.backgroundColor = kFilterCellTitleLabelBackgroundColor;
....
}
....
2. 開啓 ViewController.m 檔案, 修改如下:
....
//@add
- (UITableView *)filterListTableView
{
if (_filterListTableView == nil) {
//_filterListTableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 380, self.view.frame.size.width, 100) style:UITableViewStyleGrouped];
//@update: kOverlayTableViewRowHeight = 106
_filterListTableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 374, self.view.frame.size.width, kOverlayTableViewRowHeight) style:UITableViewStyleGrouped];
}
return _filterListTableView;
}....
//@add for setting camera Overlay
- (void)cameraOverlay
{
[self.filterLensImageView setFrame:CGRectMake(20, 80, 280, 280)];
[self.view addSubview:self.filterLensImageView];
//@add
self.filterListTableView.delegate = self;
self.filterListTableView.dataSource = self;
//@add: kOverlayTableViewRowHeight = 106
self.filterListTableView.rowHeight = kOverlayTableViewRowHeight;
//@add: 將 section Header Height 設為 0
self.filterListTableView.sectionHeaderHeight = 0;
// 不透明度, 會影響到所有的 subview 及 table 內的元件
//self.filterListTableView.alpha = 0.2;
//@add: 取消捲軸功能
self.filterListTableView.scrollEnabled = NO;
//@add: 將 Table (最下方的)分隔線設為: 不透明灰 (update: 2012/04/19)
self.filterListTableView.separatorColor = kVerticalTableSeparatorColor;
//@add: changes the background color of our vertical table
// 垂直方向 table view 的背景顏色, 目前設為: 不透明灰
// 由於已設定 scrollEnabled = NO, 因此也看不到設定的顏色
[self.filterListTableView setBackgroundColor:kVerticalTableBackgroundColor];
[self.view addSubview:self.filterListTableView];
}
....
//@add: provides from UITableViewDataSource
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
//return section == 0 ? kHeadlineSectionHeight : kRegularSectionHeight;
//@update
return 0;
}
//@add: provides from UITableViewDataSource
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
/*
....
*/
//@update
return nil;
}
....
//@update: comment it
/*
- (void)awakeFromNib
{
....
}
*/
....
3. 編譯並執行(有再調整過顏色)
標籤:
開發筆記,
Filter4Cam,
iphone
Filter4Cam 實作: 11. 濾鏡表單之四
since: 2012/03/23
update: 2012/04/19
reference: I touchs: Filter4Cam 學習之 Scroll Horizontally Tables: Part 2
客製化 TableViewCell 之一
A. 說明
1. 使用 Apple 預設的 cell 樣式, 可以有子標題或圖片, 但是無法在其中嵌入一個
Table View.
2. 在此將簡化架構, 僅供 iPhone 使用, 也讓將來的維護較為容易.(不考慮 iPad)
3. 之後將會建立一個 UITableView, 旋轉後加入成為 cell 的 subview.
(需要覆寫 initWithFrame 方法)
------------------------------------------------------------------------------------
B. 新增 TableViewCell 子類別
(可在其中嵌入水平的 filter Table View)
1. Xcode > File > New > File...
> iOS > Cocoa Touch > Objective-C class > Next
> Next > Create
2. 開啓 HorizontalTableCell.h 檔案, 修改如下:
#import <UIKit/UIKit.h>
//@add
#import "ConstantDefined.h"
//@interface HorizontalTableCell : UITableViewCell
//@update
@interface HorizontalTableCell : UITableViewCell <UITableViewDelegate, UITableViewDataSource>
{
//@add
// we will rotate and add as a subview of our cell
UITableView *filterTableView;
// hold the filters for the category we are in
NSArray *filters;
}
//@add
@property (nonatomic, strong) UITableView *filterTableView;
@property (nonatomic, strong) NSArray *filters;
@end
3. 開啓 HorizontalTableCell.m 檔案, 修改如下:
#import "HorizontalTableCell.h"
@implementation HorizontalTableCell
//@add
@synthesize filterTableView = _filterTableView;
@synthesize filters = _filters;
#pragma mark - Table View Data Source
//@add
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return [self.filters count];
}
//@add
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *cellIdentifier = @"FilterCell";
// we create a standard UITableViewCell for now
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
if (cell == nil)
{
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
}
cell.textLabel.text = @"The title of the cell in the table within the table :O";
return cell;
}
//@update: comment it
/*
- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
if (self) {
// Initialization code
}
return self;
}
*/
//@update: comment it
/*
- (void)setSelected:(BOOL)selected animated:(BOOL)animated
{
[super setSelected:selected animated:animated];
// Configure the view for the selected state
}
*/
#pragma mark - Memory Management
//@add
- (void)dealloc
{
self.filterTableView = nil;
self.filters = nil;
}
@end
------------------------------------------------------------------------------------
C. 定義用來客製化介面(大小, 顏色)的常數
開啓 ConstantDefined.h 檔案, 修改如下:
#ifndef Filter4Cam_ConstantDefined_h
#define Filter4Cam_ConstantDefined_h
//@add
#define kHeadlineSectionHeight 26
#define kRegularSectionHeight 18
////////////////////////////////////////////////////////////////////
//@add: 將 Table View 旋轉成水平向之相關設定值
//
// 嵌在一個 table view row 裡的 table view 之寬度(或旋轉前的 table 長度)
// 在此設為 iPhone 預設的寬度 320 來填滿整個螢幕.
#define kTableLength 320
////////////////////////////////////////////////////////////////////
// 在水平向的 Table 裡, 我們不想要預設的 UITableView cells 大小.
// 原因有二: 1. 因為我們要作成正方形的.
// 2. 即使你的 table 可能是 100 pixels 寬, 但是 cells 將會大於這個值,
// 浪費了記憶體與資源來畫看不到的東西.
//
// 嵌入的 table view 之 cell 的寬度(旋轉之後, 會是 row 的高度)
#define kCellWidth 106
// 嵌入的 table view 之 cell 的高度(旋轉之後, 會是 row 的寬度)
#define kCellHeight 106
//@add: 2012/03/25
////////////////////////////////////////////////////////////////////
// overlay 在最底層 camera 上的原始 table
//
// 設定其 Row 的高度與嵌入的 table 之 cell 的 高度/寬度(正方形) 相同
// 如此 cell 才會完整的位於可捲動的區域內.
#define kOverlayTableViewRowHeight 106
////////////////////////////////////////////////////////////////////
// 為了在水平向的 Table 裡建立稍微小一點的 cell, 我們讓每個 filter 的圖片和標題
// 之間有一些間隔.
//
// 為 cell(filter) 的圖片和標題填上空白
#define kFilterCellVerticalInnerPadding 3
#define kFilterCellHorizontalInnerPadding 3
////////////////////////////////////////////////////////////////////
// 讓標題的可讀性提高
// 藉由設定 cell 左右二邊的空白 pixels 數量, 來明確區別 UILabel 中不同 filter 的 title
//
// 為 filter cell 的標題 Label 填上空白
#define kFilterTitleLabelPadding 4
////////////////////////////////////////////////////////////////////
// 水平 Table View 裡的空白 pixels 數量
//
// 在 row 裡嵌入的 table view 之垂直方向空白 pixels 數量
#define kRowVerticalPadding 0
// 在 row 裡嵌入的 table view 之水平方向空白 pixels 數量
#define kRowHorizontalPadding 0
////////////////////////////////////////////////////////////////////
// 分別為垂直方向與水平方向的 table view 設定背景顏色
// 當使用者在 scroll 可用的 row, 或當 app 載入時; 才可以看到類似的背景顏色
// (而非全白)
//
// 垂直方向 table view 的背景顏色
#define kVerticalTableBackgroundColor [UIColor colorWithRed:0.58823529 green:0.58823529 blue:0.58823529 alpha:1.0]
// 水平方向 table view 的背景顏色(即嵌在垂直 table rows 裡的)
#define kHorizontalTableBackgroundColor [UIColor colorWithRed:0.6745098 green:0.6745098 blue:0.6745098 alpha:1.0]
// 將 Table (最下方)的分隔線設為: 不透明灰 (update: 2012/04/19)
#define kVerticalTableSeparatorColor [UIColor colorWithRed:0.48235294 green:0.48235294 blue:0.48235294 alpha:1.0]
////////////////////////////////////////////////////////////////////
// 當使用者選擇特定的 filter 時, cell 會出現的背景顏色.
// 一般的預設是藍色的, 這樣會破壞整體的設計
//
// 當使用者選擇特定的 filter 時, 水平方向 table view 的背景顏色
// (點選 Filter 時, 圖例的外框顏色)
#define kHorizontalTableSelectedBackgroundColor [UIColor colorWithRed:0.0 green:0.59607843 blue:0.37254902 alpha:1.0]
//@add: 2012/03/25
////////////////////////////////////////////////////////////////////
// Filter Cell 標題標籤的背景顏色
//
#define kFilterCellTitleLabelBackgroundColor [UIColor colorWithRed:0.0 green:0.476 blue:0.0 alpha:0.7]
#endif
------------------------------------------------------------------------------------
//@add
- (NSString *)reuseIdentifier
{
return @"HorizontalCell";
}
//@add
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
// Initialization code
//@add
// we are creating our table horizontally
self.filterTableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, kCellHeight, kTableLength)];
// turn off the vertical and horizontal scroll indicators
self.filterTableView.showsVerticalScrollIndicator = NO;
self.filterTableView.showsHorizontalScrollIndicator = NO;
// making a call to Core Graphic’s API in order to
// rotate our view 90 degrees counter clockwise.(逆時針90度)
// iOS 乘法運算效能優於除法
self.filterTableView.transform = CGAffineTransformMakeRotation(-M_PI * 0.5);
// set the table view's frame again
// since we now rotated it we must reset the values of the table
[self.filterTableView setFrame:CGRectMake(kRowHorizontalPadding * 0.5, kRowVerticalPadding * 0.5, kTableLength - kRowHorizontalPadding, kCellHeight)];
self.filterTableView.rowHeight = kCellWidth;
self.filterTableView.backgroundColor = kHorizontalTableBackgroundColor;
self.filterTableView.separatorStyle = UITableViewCellSeparatorStyleSingleLine;
self.filterTableView.separatorColor = [UIColor clearColor];
// set ourselves as the table's data source and delegate
self.filterTableView.dataSource = self;
self.filterTableView.delegate = self;
[self addSubview:self.filterTableView];
}
return self;
}
....
update: 2012/04/19
reference: I touchs: Filter4Cam 學習之 Scroll Horizontally Tables: Part 2
客製化 TableViewCell 之一
A. 說明
1. 使用 Apple 預設的 cell 樣式, 可以有子標題或圖片, 但是無法在其中嵌入一個
Table View.
2. 在此將簡化架構, 僅供 iPhone 使用, 也讓將來的維護較為容易.(不考慮 iPad)
3. 之後將會建立一個 UITableView, 旋轉後加入成為 cell 的 subview.
(需要覆寫 initWithFrame 方法)
------------------------------------------------------------------------------------
B. 新增 TableViewCell 子類別
(可在其中嵌入水平的 filter Table View)
1. Xcode > File > New > File...
> iOS > Cocoa Touch > Objective-C class > Next
Class: HorizontalTableCell
Subclass of: UITableViewCell // 自行輸入> Next > Create
2. 開啓 HorizontalTableCell.h 檔案, 修改如下:
#import <UIKit/UIKit.h>
//@add
#import "ConstantDefined.h"
//@interface HorizontalTableCell : UITableViewCell
//@update
@interface HorizontalTableCell : UITableViewCell <UITableViewDelegate, UITableViewDataSource>
{
//@add
// we will rotate and add as a subview of our cell
UITableView *filterTableView;
// hold the filters for the category we are in
NSArray *filters;
}
//@add
@property (nonatomic, strong) UITableView *filterTableView;
@property (nonatomic, strong) NSArray *filters;
@end
3. 開啓 HorizontalTableCell.m 檔案, 修改如下:
#import "HorizontalTableCell.h"
@implementation HorizontalTableCell
//@add
@synthesize filterTableView = _filterTableView;
@synthesize filters = _filters;
#pragma mark - Table View Data Source
//@add
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return [self.filters count];
}
//@add
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *cellIdentifier = @"FilterCell";
// we create a standard UITableViewCell for now
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
if (cell == nil)
{
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
}
cell.textLabel.text = @"The title of the cell in the table within the table :O";
return cell;
}
//@update: comment it
/*
- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
if (self) {
// Initialization code
}
return self;
}
*/
//@update: comment it
/*
- (void)setSelected:(BOOL)selected animated:(BOOL)animated
{
[super setSelected:selected animated:animated];
// Configure the view for the selected state
}
*/
#pragma mark - Memory Management
//@add
- (void)dealloc
{
self.filterTableView = nil;
self.filters = nil;
}
@end
------------------------------------------------------------------------------------
C. 定義用來客製化介面(大小, 顏色)的常數
開啓 ConstantDefined.h 檔案, 修改如下:
#ifndef Filter4Cam_ConstantDefined_h
#define Filter4Cam_ConstantDefined_h
//@add
#define kHeadlineSectionHeight 26
#define kRegularSectionHeight 18
////////////////////////////////////////////////////////////////////
//@add: 將 Table View 旋轉成水平向之相關設定值
//
// 嵌在一個 table view row 裡的 table view 之寬度(或旋轉前的 table 長度)
// 在此設為 iPhone 預設的寬度 320 來填滿整個螢幕.
#define kTableLength 320
////////////////////////////////////////////////////////////////////
// 在水平向的 Table 裡, 我們不想要預設的 UITableView cells 大小.
// 原因有二: 1. 因為我們要作成正方形的.
// 2. 即使你的 table 可能是 100 pixels 寬, 但是 cells 將會大於這個值,
// 浪費了記憶體與資源來畫看不到的東西.
//
// 嵌入的 table view 之 cell 的寬度(旋轉之後, 會是 row 的高度)
#define kCellWidth 106
// 嵌入的 table view 之 cell 的高度(旋轉之後, 會是 row 的寬度)
#define kCellHeight 106
//@add: 2012/03/25
////////////////////////////////////////////////////////////////////
// overlay 在最底層 camera 上的原始 table
//
// 設定其 Row 的高度與嵌入的 table 之 cell 的 高度/寬度(正方形) 相同
// 如此 cell 才會完整的位於可捲動的區域內.
#define kOverlayTableViewRowHeight 106
////////////////////////////////////////////////////////////////////
// 為了在水平向的 Table 裡建立稍微小一點的 cell, 我們讓每個 filter 的圖片和標題
// 之間有一些間隔.
//
// 為 cell(filter) 的圖片和標題填上空白
#define kFilterCellVerticalInnerPadding 3
#define kFilterCellHorizontalInnerPadding 3
////////////////////////////////////////////////////////////////////
// 讓標題的可讀性提高
// 藉由設定 cell 左右二邊的空白 pixels 數量, 來明確區別 UILabel 中不同 filter 的 title
//
// 為 filter cell 的標題 Label 填上空白
#define kFilterTitleLabelPadding 4
////////////////////////////////////////////////////////////////////
// 水平 Table View 裡的空白 pixels 數量
//
// 在 row 裡嵌入的 table view 之垂直方向空白 pixels 數量
#define kRowVerticalPadding 0
// 在 row 裡嵌入的 table view 之水平方向空白 pixels 數量
#define kRowHorizontalPadding 0
////////////////////////////////////////////////////////////////////
// 分別為垂直方向與水平方向的 table view 設定背景顏色
// 當使用者在 scroll 可用的 row, 或當 app 載入時; 才可以看到類似的背景顏色
// (而非全白)
//
// 垂直方向 table view 的背景顏色
#define kVerticalTableBackgroundColor [UIColor colorWithRed:0.58823529 green:0.58823529 blue:0.58823529 alpha:1.0]
// 水平方向 table view 的背景顏色(即嵌在垂直 table rows 裡的)
#define kHorizontalTableBackgroundColor [UIColor colorWithRed:0.6745098 green:0.6745098 blue:0.6745098 alpha:1.0]
// 將 Table (最下方)的分隔線設為: 不透明灰 (update: 2012/04/19)
#define kVerticalTableSeparatorColor [UIColor colorWithRed:0.48235294 green:0.48235294 blue:0.48235294 alpha:1.0]
////////////////////////////////////////////////////////////////////
// 當使用者選擇特定的 filter 時, cell 會出現的背景顏色.
// 一般的預設是藍色的, 這樣會破壞整體的設計
//
// 當使用者選擇特定的 filter 時, 水平方向 table view 的背景顏色
// (點選 Filter 時, 圖例的外框顏色)
#define kHorizontalTableSelectedBackgroundColor [UIColor colorWithRed:0.0 green:0.59607843 blue:0.37254902 alpha:1.0]
//@add: 2012/03/25
////////////////////////////////////////////////////////////////////
// Filter Cell 標題標籤的背景顏色
//
#define kFilterCellTitleLabelBackgroundColor [UIColor colorWithRed:0.0 green:0.476 blue:0.0 alpha:0.7]
#endif
------------------------------------------------------------------------------------
D. 開啓 HorizontalTableCell.m 檔案, 修改如下:
....//@add
- (NSString *)reuseIdentifier
{
return @"HorizontalCell";
}
//@add
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
// Initialization code
//@add
// we are creating our table horizontally
self.filterTableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, kCellHeight, kTableLength)];
// turn off the vertical and horizontal scroll indicators
self.filterTableView.showsVerticalScrollIndicator = NO;
self.filterTableView.showsHorizontalScrollIndicator = NO;
// making a call to Core Graphic’s API in order to
// rotate our view 90 degrees counter clockwise.(逆時針90度)
// iOS 乘法運算效能優於除法
self.filterTableView.transform = CGAffineTransformMakeRotation(-M_PI * 0.5);
// set the table view's frame again
// since we now rotated it we must reset the values of the table
[self.filterTableView setFrame:CGRectMake(kRowHorizontalPadding * 0.5, kRowVerticalPadding * 0.5, kTableLength - kRowHorizontalPadding, kCellHeight)];
self.filterTableView.rowHeight = kCellWidth;
self.filterTableView.backgroundColor = kHorizontalTableBackgroundColor;
self.filterTableView.separatorStyle = UITableViewCellSeparatorStyleSingleLine;
self.filterTableView.separatorColor = [UIColor clearColor];
// set ourselves as the table's data source and delegate
self.filterTableView.dataSource = self;
self.filterTableView.delegate = self;
[self addSubview:self.filterTableView];
}
return self;
}
....
標籤:
開發筆記,
Filter4Cam,
iphone
訂閱:
文章 (Atom)






