本文转载至 http://blog.sina.com.cn/s/blog_bf2d33bd01017q6l.html
@interface ThirdViewController : UIViewController
@property(nonatomic,retain)UITableView *tv;
@property(nonatomic,retain)NSMutableArray *tarray,*filteredArray;
@property(nonatomic,retain)UISearchDisplayController *sdc;//搜索栏控制器
@property(nonatomic,retain)UISearchBar *serchbar;
@end
//
#import "ThirdViewController.h"
@interface ThirdViewController ()
@end
@implementation ThirdViewController
@synthesize tarray,tv,sdc,serchbar,filteredArray;
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}
//crayons.txt
- (void)viewDidLoad
{
[super viewDidLoad];
self.title=@"搜索";
// self.view.backgroundColor=[UIColor blueColor];横屏控制条高33竖屏控制条搞44
self.tv=[[UITableView alloc]initWithFrame:CGRectMake(0, 0, 320, 480-44-44) style:UITableViewStylePlain];
[self.view addSubview:tv];
self.tv.dataSource=self;//设置代理为自身
self.tv.delegate=self;
NSString *path=[[NSBundle mainBundle]pathForResource:@"crayons" ofType:@"txt"];//取到路径
NSString *readFile=[[NSString alloc]initWithContentsOfFile:path encoding:NSUTF8StringEncoding error:nil];//读取文件
self.tarray= (NSMutableArray*)[readFile componentsSeparatedByString:@" "];//分割数据
//设置搜索条的大小
self.serchbar=[[UISearchBar alloc]initWithFrame:CGRectMake(0, 0, 320, 44)];
[self.view addSubview:serchbar];
//创建搜索栏控制器
self.sdc=[[UISearchDisplayController alloc]initWithSearchBar:serchbar contentsController:self];
//UISearchDisplayController组件 和 UITableView共用用相同的代理
sdc.searchResultsDataSource=self;//设置搜索结果数据源
sdc.searchResultsDelegate=self;//设置代理
[self.tv reloadData];//刷新数据
// Do any additional setup after loading the view.
}
- (void)viewDidUnload
{
[super viewDidUnload];
// Release any retained subviews of the main view.
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
#pragma mark - Table view data source
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
//#warning Potentially incomplete method implementation.
// Return the number of sections.
return 1;
}
//返回条数
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
//#警告不完整的方法实现。
//返回一节中的行数。
// return [self.tarray count];
if (tableView==self.tv)
{
return [self.tarray count];
}
else
{
//Cocoa提供了一个类NSPredicate类,该类主要用于指定过滤器的条件,该对象可以准确的描述所需条件,对每个对象通过谓词进行筛选,判断是否与条件相匹配。谓词表示计算真值或假值的函数。
//用predicateWithFormat创建一个谓词,条件是搜索的信息
//self关键字 字符串运算符CONTAINS[c][d][cd] CONTAINS:检查某个字符串是否以另一个字符串内部。[c]不区分大小写[d]不区分发音符号即没有重音符号[cd]既不区分大小写,又不区分发音符号。
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"SELF contains[cd] %@", self.serchbar.text];
//filteredArrayUsingPredicate方法它将循环过滤数组内容,根据谓词计算每个对象的值,并将值为YES的对象累计到将被返回的新数组filteredArray中。
self.filteredArray = (NSMutableArray*)[self.tarray filteredArrayUsingPredicate:predicate];
//符合搜索条件的新数组的长度
return [self.filteredArray count];
}
}
//返回可用单元格
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"Cell";//复用cell的字符索引
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];//重用机制创建cell
if (cell == nil)
{
cell = [[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:CellIdentifier]autorelease];
}
int index = indexPath.row;//取出行标记
NSString *txt = @"";
if (tableView==self.tv)
{
txt=[self.tarray objectAtIndex:index];
}else
{
txt=[self.filteredArray objectAtIndex:index];
}
NSArray *array = [txt componentsSeparatedByString:@"#"];//设置#分割
cell.textLabel.text = [array objectAtIndex:0];
UIColor *tc=[self setColorWithString:[array objectAtIndex:1]];
cell.textLabel.textColor=tc;
// Configure the cell...`
return cell;
}
//设置行高
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
return 50;
}
-(UIColor*)setColorWithString:(NSString*)colorString
{
NSString *f1=[colorString substringWithRange:NSMakeRange(0, 2)];
NSString *f2=[colorString substringWithRange:NSMakeRange(2, 2)];
NSString *f3=[colorString substringWithRange:NSMakeRange(4, 2)];
unsigned int red,green,blue;
[[NSScanner scannerWithString:f1] scanHexInt:&red];
[[NSScanner scannerWithString:f2] scanHexInt:&green];
[[NSScanner scannerWithString:f3] scanHexInt:&blue];
// NSLog(@"f1===%d,f2===%d,f3===%d",red,green,blue);
return [[UIColor alloc]initWithRed:red/255.0 green:green/255.0 blue:blue/255.0 alpha:1];
}
//重写的表视图,支持有条件的编辑。
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath
{
// 返回NO,如果你不想指定的项目为可编辑的。
return YES;
}
#pragma mark - Table view delegate
//响应单击事件
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
// Navigation logic may go here. Create and push another view controller.
[self.tv resignFirstResponder];
UITableViewCell *cell=[tableView cellForRowAtIndexPath:indexPath];
self.serchbar.tintColor=cell.textLabel.textColor;
self.navigationController.navigationBar.tintColor=cell.textLabel.textColor;
}
//
Almond #EED9C4
Antique Brass #C88A65
Apricot #FDD5B1
Aquamarine #71D9E2
Asparagus #7BA05B
Atomic Tangerine #FF9966
Banana Mania #FBE7B2
Beaver #926F5B
Bittersweet #FE6F5E
Black #000000
Blizzard Blue #A3E3ED
Blue #0066FF
Blue Bell #9999CC
Blue Green #0095B6
Blue Violet #6456B7
Brick Red #C62D42
Brink Pink #FB607F
Brown #AF593E
Burnt Orange #FF7034
Burnt Sienna #E97451
Cadet Blue #A9B2C3
Canary #FFFF99
Caribbean Green #00CC99
Carnation Pink #FFA6C9
Cerise #DA3287
Cerulean #02A4D3
Chartreuse #FF9966
Chestnut #B94E48
Copper #DA8A67
Cornflower #93CCEA
Cotton Candy #FFB7D5
Cranberry #DB5079
Dandelion #FED85D
Denim #1560BD
Desert Sand #EDC9AF
Eggplant #614051
Electric Lime #CCFF00
Fern #63B76C
Flesh #FFCBA4
Forest Green #5FA777
Fuchsia #C154C1
Fuzzy Wuzzy Brown #C45655
Gold #E6BE8A
Goldenrod #FCD667
Granny Smith Apple #9DE093
Gray #8B8680
Green #01A368
Green Yellow #F1E788
Happy Ever After #6CDA37
Hot Magenta #FF00CC
Inch Worm #B0E313
Indian Red #B94E48
Indigo #4F69C6
Jazzberry Jam #A50B5E
Jungle Green #29AB87
Laser Lemon #FFFF66
Lavender #FBAED2
Macaroni And Cheese #FFB97B
Magenta #F653A6
Magic Mint #AAF0D1
Mahogany #CA3435
Manatee #8D90A1
Mango Tango #E77200
Maroon #C32148
Mauvelous #F091A9
Melon #FEBAAD
Midnight Blue #003366
Mountain Meadow #1AB385
Mulberry #C54B8C
Navy Blue #0066CC
Neon Carrot #FF9933
Olive Green #B5B35C
Orange #FF681F
Orchid #E29CD2
Outer Space #2D383A
Outrageous Orange #FF6037
Pacific Blue #009DC4
Peach #FFCBA4
Periwinkle #C3CDE6
Pig Pink #FDD7E4
Pine Green #01796F
Pink Flamingo #FF66FF
Plum #843179
Prussian Blue #003366
Purple Heart #652DC1
Purple Mountain's Majesty #9678B6
Purple Pizzazz #FF00CC
Radical Red #FF355E
Raw Sienna #D27D46
Razzle Dazzle Rose #FF33CC
Razzmatazz #E30B5C
Red #ED0A3F
Red Orange #FF3F34
Red Violet #BB3385
Robin's Egg Blue #00CCCC
Royal Purple #6B3FA0
Salmon #FF91A4
Scarlet #FD0E35
Screamin' Green #66FF66
Sea Green #93DFB8
Sepia #9E5B40
Shadow #837050
Shamrock #33CC99
Shocking Pink #FF6FFF
Silver #C9C0BB
Sky Blue #76D7EA
Spring Green #ECEBBD
Sunglow #FFCC33
Sunset Orange #FE4C40
Tan #FA9D5A
Tickle Me Pink #FC80A5
Timberwolf #D9D6CF
Torch Red #FD0E35
Tropical Rain Forest #00755E
Tumbleweed #DEA681
Turquoise Blue #6CDAE7
Ultra Green #66FF66
Ultra Orange #FF6037
Ultra Pink #FF6FFF
Ultra Red #FD5B78
Ultra Yellow #FFFF66
Unmellow Yellow #FFFF66
Violet (purple) #8359A3
Violet Red #F7468A
Vivid Tangerine #FF9980
Vivid Violet #803790
White #FFFFFF
Wild Blue Yonder #7A89B8
Wild Strawberry #FF3399
Wild Watermelon #FD5B78
Wisteria #C9A0DC
Yellow #FBE870
Yellow Green #C5E17A
Yellow Orange #FFAE42