update: 2012/03/21
reference: I touchs: Filter4Cam 學習之 Scroll Horizontally Tables: Part 1
載入濾鏡清單屬性檔之二
A. 載入濾鏡圖檔與標題
開啓 ViewController.m 檔案, 修改如下:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil)
{
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
//@add
NSSortDescriptor *sortDescriptor = [NSSortDescriptor sortDescriptorWithKey:nil ascending:YES selector:@selector(localizedCompare:)];
NSArray *sortedCategories = [self.filterDictionary.allKeys sortedArrayUsingDescriptors:[NSArray arrayWithObject:sortDescriptor]];
NSString *categoryName = [sortedCategories objectAtIndex:indexPath.section];
NSArray *currentCategory = [self.filterDictionary objectForKey:categoryName];
NSDictionary *currentFilter = [currentCategory objectAtIndex:indexPath.row];
//cell.textLabel.text = @"Filter List Row";
cell.textLabel.text = [currentFilter objectForKey:@"Title"];
cell.imageView.image = [UIImage imageNamed:[currentFilter objectForKey:@"ImageName"]];
return cell;
}
編譯並執行:
B. 客製 Table View 的 Setion Header
1. 新增常數定義檔
Xcode > File > New > File...
> iOS > C and C++ > Header File > Next
Save As: ConstantDefined.h
> Create 2. 開啓 ConstantDefined.h 檔案, 修改如下:
#ifndef Filter4Cam_ConstantDefined_h
#define Filter4Cam_ConstantDefined_h
//@add
#define kHeadlineSectionHeight 26
#define kRegularSectionHeight 18
#endif
3. 開啓 ViewController.h 檔案, 修改如下:
....
//@add
#import "Filter4CamHelper.h"
#import "ConstantDefined.h"
....
//@add: provides from UITableViewDataSource
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
return section == 0 ? kHeadlineSectionHeight : kRegularSectionHeight;
}
//@add: provides from UITableViewDataSource
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
// create a UIView, UILabel and UIFont for our custom header
UIView *customSectionHeaderView;
UILabel *titleLabel;
UIFont *labelFont;
// creates a header with a different height and font size
if (section == 0)
{
customSectionHeaderView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, tableView.frame.size.width, kHeadlineSectionHeight)];
titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(10, 0, tableView.frame.size.width, kHeadlineSectionHeight)];
labelFont = [UIFont boldSystemFontOfSize:20];
}
else
{
customSectionHeaderView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, tableView.frame.size.width, kRegularSectionHeight)];
titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(10, 0, tableView.frame.size.width, kRegularSectionHeight)];
labelFont = [UIFont boldSystemFontOfSize:13];
}
// set tableView backgroundColor
tableView.backgroundColor = [UIColor colorWithRed:0.11568627 green:0.11568627 blue:0.11568627 alpha:0.28];
// change the background color of our custom view
// and set some properties on the UILabel
customSectionHeaderView.backgroundColor = [UIColor colorWithRed:0.11568627 green:0.11568627 blue:0.11568627 alpha:0.28];
titleLabel.textAlignment = UITextAlignmentLeft;
[titleLabel setTextColor:[UIColor whiteColor]];
[titleLabel setBackgroundColor:[UIColor clearColor]];
titleLabel.font = labelFont;
// get the current category title for the section
// just like we did earlier on the tableView:titleForHeaderInSection: Method
// we don’t need that method anymore so delete that method which we wrote earlier.
NSSortDescriptor *sortDescriptor = [NSSortDescriptor sortDescriptorWithKey:nil ascending:YES selector:@selector(localizedCompare:)];
NSArray *sortedCategories = [self.filterDictionary.allKeys sortedArrayUsingDescriptors:[NSArray arrayWithObject:sortDescriptor]];
NSString *categoryName = [sortedCategories objectAtIndex:section];
titleLabel.text = [categoryName substringFromIndex:1]; // 移除 1MyFilter 開頭的 1
[customSectionHeaderView addSubview:titleLabel];
return customSectionHeaderView;
}
//@add: get the titles for our sections
/*
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
NSSortDescriptor *sortDescriptor = [NSSortDescriptor sortDescriptorWithKey:nil ascending:YES selector:@selector(localizedCompare:)];
NSArray *sortedCategories = [self.filterDictionary.allKeys sortedArrayUsingDescriptors:[NSArray arrayWithObject:sortDescriptor]];
NSString *categoryName = [sortedCategories objectAtIndex:section];
return [categoryName substringFromIndex:1]; // 移除 1MyFilter 開頭的 1
}
*/
....
4. 編譯並執行
沒有留言:
張貼留言
注意:只有此網誌的成員可以留言。