懒加载数据模型
- if语句判断是否为空
- 然后取路径
- 从路径中取出数组(数组内容为字典)
- 把字典转换成模型
返回模型
例子:- (NSMutableArray *)tgModels { if (_tgModels == nil) { NSString *path = [[NSBundle mainBundle] pathForResource:@"tgs" ofType:@"plist"]; NSArray *models = [NSArray arrayWithContentsOfFile:path]; _tgModels = [NSMutableArray array]; for (NSDictionary *dict in models) { QYTGModel *model = [QYTGModel tgModelWithDcitionary:dict]; [_tgModels addObject:model]; } } return _tgModels; }
自定义单元格
- contentView
- 创建xib中设置
- storyboard中设置
- layoutSubView(子视图化)
重写set方法的时候一定要先把set方法本身的任务写上
区分 section 和 tableView 的 头视图
- section 的头视图只需要设置行高
- tableView 的头视图要设置 frame
创建模型
- 模型属于一个类
- .h 文件中声明数模型中需要的属性和初始化方法(两个初始化方法—类初始化方法和对象初始化方法)
先写对象初始化初始化方法
-(instancetype)initWithDictionary:(NSDictionary *)dict { if (self = [super init]) { //第一种方法: _buycount = dict[@"buycount"]; _icon = dict[@"icon"]; _price = dict[@"price"]; _title = dict[@"title"]; // 第二种方法:灌入字典 //[self setValuesForKeysWithDictionary:dict]; } return self; }
再写类初始化方法
+ (instancetype)tgModelWithDcitionary:(NSDictionary *)dict{ return [[self alloc] initWithDictionary:dict]; }
设置导航栏的编辑按钮并实现编辑
移动的实现和编辑相似,先返回是否允许移动,然后移动方法:先从原处取出要移动的,删除原处内容,再把内容插入到目的处(移动的界面实现系统已经做好,我们只需要做数据源的处理)
在viewDidLoad中调用下两个方法:
//设置导航栏上右边的编辑
-(void)setRightBarButtonItem{
UIBarButtonItem *rightBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"编辑" style:UIBarButtonItemStylePlain target:self action:@selector(edit:)];
self.navigationItem.rightBarButtonItem = rightBarButtonItem;
}
//编辑 完成 切换
- (void)edit:(UIBarButtonItem *)sender {
if ([sender.title isEqualToString:@"编辑"]) {
sender.title = @"完成";
[self.tableView setEditing:YES animated:YES];
} else {
sender.title = @"编辑";
[self.tableView setEditing:NO animated:YES];
}
}
实现代理方法:
//是否可以编辑
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
return YES;
}
//编辑风格(插入和删除)
- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath{
if (indexPath.row % 2) {//奇数行
return UITableViewCellEditingStyleInsert;
}else{//偶数
return UITableViewCellEditingStyleDelete;
}
}
//编辑的实现方法(注意必须把数据源删除)
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
if (editingStyle == UITableViewCellEditingStyleInsert) {
//更改数据源
[array insertObject:@"青云iOS" atIndex:indexPath.row + 1];
//更改界面
NSIndexPath *addIndexPath = [NSIndexPath indexPathForRow:indexPath.row + 1 inSection:indexPath.section];
[tableView insertRowsAtIndexPaths:[NSArray arrayWithObject:addIndexPath] withRowAnimation:UITableViewRowAnimationLeft];
}else if (editingStyle == UITableViewCellEditingStyleDelete){
//更改数据源
[array removeObjectAtIndex:indexPath.row];
//更改界面
[tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationMiddle];
}
}
代码设置tabbar
-(void)awakeFromNib{
self.tabBarController.tabBar.tintColor = [UIColor brownColor];
UIImage *image = [[UIImage imageNamed:@"tabbar_contacts"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
UIImage *selectedImage = [[UIImage imageNamed:@"tabbar_contactsHL"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
self.tabBarItem = [[UITabBarItem alloc] initWithTitle:@"通讯录" image:image selectedImage:selectedImage];
}
下拉刷新
UIRefreshControl *refresh = [[UIRefreshControl alloc] init];
[self.tableView addSubview:refresh];
[refresh addTarget:self action:@selector(refresh:) forControlEvents:UIControlEventValueChanged];
--------------------------------
-(void)refresh:(UIRefreshControl *)refresh{
[refresh performSelector:@selector(endRefreshing) withObject:nil afterDelay:2];
}
根据TextField输入的网址来访问对应的网页
注意要遵循UIWebViewDelegate,UITextFieldDelegate协议,并设置代理
NSURL *url = [NSURL URLWithString:_textField.text];
NSURLRequest *request = [NSURLRequest requestWithURL:url];
[_webView loadRequest:request];
//设置状态栏的风火轮
-(void)webViewDidStartLoad:(UIWebView *)webView{
[[UIApplication sharedApplication] setNetworkActivityIndicatorVisible:YES];
}
-(void)webViewDidFinishLoad:(UIWebView *)webView{
[[UIApplication sharedApplication] setNetworkActivityIndicatorVisible:NO];
}
索引条
NSString *path = [[NSBundle mainBundle] pathForResource:@"sortednames" ofType:@"plist"];
_dict = [NSDictionary dictionaryWithContentsOfFile:path];
NSArray *keys = _dict.allKeys;
_keys = [keys sortedArrayUsingSelector:@selector(compare:)];
//设置索引
-(NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView{
return _keys;
}
//点击索引
-(NSInteger)tableView:(UITableView *)tableView sectionForSectionIndexTitle:(NSString *)title atIndex:(NSInteger)index{
return index;
}
搜索栏
必须遵循协议 UISearchResultsUpdating
- (void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText{
if ([searchText isEqualToString:@""] || searchText == nil) {//clear
_isSearching = NO;
[_tableView reloadData];
return;
}
_isSearching = YES;
//搜索关键字(谓词过滤,谓词是针对于数组的)
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"SELF CONTAINS[CD] %@",searchText];
NSMutableArray *resultArr = [NSMutableArray array];
for (NSString *key in _keys) {
NSArray *array = _dict[key];
NSArray * resultArray = [array filteredArrayUsingPredicate:predicate];
[resultArr addObjectsFromArray:resultArray];
}
_results = resultArr;
[_tableView reloadData];
}