iOS UI 知识点

来源:互联网 发布:蛇蛇争霸网络不稳定 编辑:程序博客网 时间:2024/06/05 15:40

懒加载数据模型

  1. if语句判断是否为空
  2. 然后取路径
  3. 从路径中取出数组(数组内容为字典)
  4. 把字典转换成模型
  5. 返回模型
    例子:

    - (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;}

自定义单元格

  1. contentView
  2. 创建xib中设置
  3. storyboard中设置
  4. layoutSubView(子视图化)

重写set方法的时候一定要先把set方法本身的任务写上

区分 section 和 tableView 的 头视图

  1. section 的头视图只需要设置行高
  2. tableView 的头视图要设置 frame

创建模型

  1. 模型属于一个类
  2. .h 文件中声明数模型中需要的属性初始化方法(两个初始化方法—类初始化方法和对象初始化方法)
  3. 先写对象初始化初始化方法

    -(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;}
  4. 再写类初始化方法

    + (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];}
1 0