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在代碼中的使用框架

步驟:spa

  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;
相關文章
相關標籤/搜索