UI控件(scrollView、tableView)的基本使用

UIScrollView在storyboard中的使用

UIScrollView在代码中的使用

//    1.创建一个imageView并设置图片
    UIImage *image = [UIImage imageNamed:@"minion"];
    UIImageView *imageView = [[UIImageView alloc] initWithImage:image];
    
//    2.将imageView添加到scrollView中
    [self.scrollView addSubview:imageView];
    
//    3.设置scrollView的内容尺寸
    self.scrollView.contentSize = image.size;
    
//    4.设置偏移量
//    控制内容滚动的位置
//    得知scrollView中滚动的位置
//    self.scrollView.contentOffset = CGPointMake(200, 0);
    
//    5.设置内边距(增加额外的滚动区域)
//    self.scrollView.contentInset = UIEdgeInsetsMake(100, 0, 0, 0);

 

UITableView在代码中的使用

步骤:

  1. 先创建一个UITableView,确定大小和位置添加到控制器的View中
  2. 让控制器遵守UITableViewdatasource协议,并实现其中3个方法(如果组数为1的话,返回组数的方法可不写)
  3. 缓加载中加载plist文件
  4. 如果是复杂plist文件则对其一层一层解析
  5. 在模型数组的类内工厂方法中使用setValueForKeys快速设值,并且在其中节解析其它的字典数组转换为模型数组
  6. 设置数据
  7. 设置一些属性
  8. 代理的话就是用来监听一些对UITableView的操作,代理是弱引用
  9. UITableViewControl的话注意点的就是self.viewself.tableView指向的是同一个对象,使用一个UITableView的控制器。

          继承UITabelViewcontrol 默认遵守了两个协议

返回组数
/**
 *  有多少组,默认是一组(不写此方法 )
 */
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return self.carsGroup.count;
};
返回行数
/**
 *  每一组有多少行
 */
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
//    取出模型数组
    WWDCarsGroup *group = self.carsGroup[section];
    return group.cars.count;
}

返回cell(缓存池,设置cell的内容数据)
/**
 *  每一行要显示的内容
 */
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
//    定义ID
    static NSString *ID = @"A";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:ID];
    if (!cell) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:ID];
    }
//    取出模型数组
    WWDCarsGroup *group = self.carsGroup[indexPath.section];
    WWDCars *cars = group.cars[indexPath.row];
    
//    设置数据
    cell.textLabel.text = cars.name;
    cell.imageView.image = [UIImage imageNamed:cars.icon];
    return cell;
}

注册(自定义cell)
//    注册
    [self.tableView registerNib:[UINib nibWithNibName:NSStringFromClass([WWDTgCell class]) bundle:nil] forCellReuseIdentifier:tgID];

缓加载部分
• 系统
#pragma mark - 懒加载
- (NSArray *)herosArr
{
    if (!_herosArr) {
//        加载plist文件
        NSArray *arr = [NSArray arrayWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"heros.plist" ofType:nil]];
        
//        遍历字典数组获取数组模型
        NSMutableArray *temp = [NSMutableArray array];
        for (NSDictionary *herosDict in arr) {
            WWDHeros *heros = [WWDHeros herosWithDictionary:herosDict];
            [temp addObject:heros];
        }
        _herosArr = temp;
    }
    return _herosArr;
}

• MJ框架
- (NSArray *)tgs
{
    if (!_tgs) {
//        NSArray *dictArray = [NSArray arrayWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"tgs.plist" ofType:nil]];
//        _tgs = [WWDTg mj_objectArrayWithKeyValuesArray:dictArray];
        _tgs = [WWDTg mj_objectArrayWithFilename:@"tgs.plist"];
    }
    return _tgs;
}
返回表头
/**
 *  表头显示的内容
 */
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
    WWDCarsGroup *group = self.carsGroup[section];
    return group.title;
}

返回表尾
//设置尾部
- (NSString *) tableView:(UITableView *)tableView titleForFooterInSection:(NSInteger)section
{
    return @"小撸怡情,大撸伤身,强撸灰飞烟灭";
}

/**
 *  返回索引条文字
 */
- (NSArray<NSString *> *)sectionIndexTitlesForTableView:(UITableView *)tableView
{
//    NSMutableArray *temp = [NSMutableArray array];
//    for (XMGCarGroup *group in self.carGroups) {
//        [temp addObject:group.title];
//    }
//    return temp;
    // 抽取self.carGroups每一个元素(XMGCarGroup对象)的title属性的值,放在新的数组中返回
    return [self.carGroups valueForKeyPath:@"title"];
}
tableView属性设置
/********************** 属性设置 *****************************/
    // 设置每一行cell的高度
    self.tableView.rowHeight = 100;
    
    // 设置每一组头部的高度
    self.tableView.sectionHeaderHeight = 40.0
    ;

    // 设置每一组尾部的高度
    self.tableView.sectionFooterHeight = 20.0;
    
    // 设置分割线的颜色
    self.tableView.separatorColor = [UIColor redColor];
    
    // 设置分割线的样式
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    
    // 设置tableView表头view
    self.tableView.tableHeaderView = [[UISwitch alloc] init];
    
    // 设置tableView表尾view
    self.tableView.tableFooterView = [[UISwitch alloc] init];
    
    /********************** 属性设置 *****************************/

    /********************** 属性设置 *****************************/
    // 设置cell右边的指示样式
//    UITableViewCellAccessoryNone,                                                     
//    UITableViewCellAccessoryDisclosureIndicator,
//    UITableViewCellAccessoryDetailDisclosureButton,
//    UITableViewCellAccessoryCheckmark,
//    UITableViewCellAccessoryDetailButton
    cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    
    // 设置cell右边的指示控件
    // accessoryView的优先级 > accessoryType
//    cell.accessoryView = [[UISwitch alloc] init];
   cell属性 
    // 设置cell的选中样式
//    UITableViewCellSelectionStyleNone,
//    UITableViewCellSelectionStyleBlue,
//    UITableViewCellSelectionStyleGray,
//    UITableViewCellSelectionStyleDefault
//    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    
    // 设置cell的背景颜色
    cell.backgroundColor = [UIColor magentaColor];
    
    // 设置cell的背景view
    // backgroundView的优先级 > backgroundColor
    UIImageView *imageView = [[UIImageView alloc] init];
    imageView.image = [UIImage imageNamed:@"12196499,2560,1600"];
    cell.backgroundView = imageView;
    
    // 设置cell选中的背景view
    UIView *sg = [[UIView alloc] init];
    sg.backgroundColor = [UIColor purpleColor];
    cell.selectedBackgroundView = sg;

 

posted @ 2015-11-26 22:12  a滴答  阅读(280)  评论(0编辑  收藏  举报