注意:
1.block的属性用 copy
2.pch文件 中定义一次,整个工程都能用 注意:Build Setting —> 搜索lang —> prefix header —>
$(PROJECT_DIR)/PrefixHeader.pch (相对路径)
3. //设置button上title的颜色
[button setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
4.cell的函数中用到if最好加else
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
if (table == tableView )
{
NSArray *titleArray = @[@"D",@"F",@"H",@"M",@"W"];
return titleArray[section];
}
else
{
return nil;
}
}
一些概念:
1.反向传值之代理:
委托者中
(1).@protocol ViewController2Delegate <NSObject>
- (void)receiveDataDictionary:(NSMutableDictionary *)dataDic;
@end
(2).@property (assign,nonatomic) id<ViewController2Delegate>delegate;
(3).if ([self.delegate respondsToSelector:@selector(receiveDataDictionary:)])
{
[self.delegate receiveDataDictionary:dataDic];
}
代理者中:
(4).遵循代理协议:
<ViewController2Delegate>
(5).建立代理关系:
vc2.delegate = self;
(6).实现协议方法:
- (void)receiveDataDictionary:(NSMutableDictionary *)dataDic
{
// NSLog(@"%@",dataDic);
self.navigationController.navigationBar.barTintColor = [UIColor colorWithRed:[[dataDic valueForKey:@"red"] floatValue]/255.0 green:[[dataDic valueForKey:@"green"] floatValue]/255.0 blue:[[dataDic valueForKey:@"blue"] floatValue]/255.0 alpha:1.0];
[dataDic1 setObject:[dataDic valueForKey:@"name"] forKey:@"name"];
[dataDic1 setObject:[dataDic valueForKey:@"pass"] forKey:@"pass"];
[dataDic1 setObject:[dataDic valueForKey:@"sex"] forKey:@"sex"];
[dataArray addObject:dataDic1];
// NSLog(@"%@",dataDic1);
// NSLog(@"%@",dataArray);
}
2.反向传值之block:
委托者中:
(1).typedef void(^block)(NSMutableDictionary *);
(2).@property (copy,nonatomic) block myBlock;
(3). if (self.myBlock)
{
self.myBlock(dataDic);
}
代理者中:
(4).vc2.myBlock = ^(NSMutableDictionary *dataDic){
self.navigationController.navigationBar.barTintColor = [UIColor colorWithRed:[[dataDic valueForKey:@"red"] floatValue]/255.0 green:[[dataDic valueForKey:@"green"] floatValue]/255.0 blue:[[dataDic valueForKey:@"blue"] floatValue]/255.0 alpha:1.0];
[dataDic1 setObject:[dataDic valueForKey:@"name"] forKey:@"name"];
[dataDic1 setObject:[dataDic valueForKey:@"pass"] forKey:@"pass"];
[dataDic1 setObject:[dataDic valueForKey:@"sex"] forKey:@"sex"];
[dataArray addObject:dataDic1];
[table reloadData];
};
3.搜索框:
(1).全局 UISearchDisplayController *displayerController;
NSMutableArray *resultArray;//搜索结果数据
(2). UISearchBar *searchBar = [[UISearchBar alloc] initWithFrame:CGRectMake(0, 0, 0, 40)];
/*
UISearchBarStyleDefault,
UISearchBarStyleProminent,
UISearchBarStyleMinimal
*/
searchBar.searchBarStyle = UISearchBarStyleMinimal;
searchBar.showsBookmarkButton = YES;
searchBar.placeholder = @"请输入英雄名字:";
searchBar.delegate = self;
//创建搜索控制器
displayerController = [[UISearchDisplayController alloc] initWithSearchBar:searchBar contentsController:self];
displayerController.searchResultsDataSource = self;
displayerController.searchResultsDelegate = self;
// [displayerController.searchResultsTableView reloadData];
resultArray = [NSMutableArray array];
table.tableHeaderView = searchBar;
(3).- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
if (table == tableView)
{
return dataArray.count;
}
else
{
return 1;//结果数组
}
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
if (tableView == table)
{
return [dataArray[section] count];
}
else
{
return resultArray.count;
}
}
(4).- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *cellID = @"cellID";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellID];
if (!cell)
{
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellID];
}
if (tableView == table)
{
cell.textLabel.text = dataArray[indexPath.section][indexPath.row];
}
else
{
cell.textLabel.text = resultArray[indexPath.row];
}
return cell;
}
(5).
- (void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText
{
if (resultArray.count)
{
[resultArray removeAllObjects];
}
NSString *str = searchBar.text;
//清除字符串中的空格
str = [str stringByReplacingOccurrencesOfString:@" " withString:@""];
for (NSArray *tempArray in dataArray)
{
for (NSString *tempStr in tempArray)
{
NSRange range = [tempStr rangeOfString:str];
//判断数据源字符串中是否含有搜索字符串
if (range.location != NSNotFound)
{
//如果含有就加入到结果数组中
[resultArray addObject:tempStr];
}
}
}
//刷新table
[displayerController.searchResultsTableView reloadData];
}