第二十二篇:UITableView練習代碼

車的品牌展示:

//  Car.h
//  07-UITableView練習-Car
//
//  Created by 瞿傑 on 15/9/28.
//

#import <Foundation/Foundation.h>

@interface Car : NSObject

@property (nonatomic,copy)NSString * icon;
@property (nonatomic,copy)NSString * name;

+ (instancetype)carWithDictionary:(NSDictionary *)dic;
- (instancetype)initWithDictionary:(NSDictionary *)dic;

@end
//
//  Car.m
//  07-UITableView練習-Car
//
//  Created by 瞿傑 on 15/9/28.
//

#import "Car.h"

@implementation Car

+ (instancetype)carWithDictionary:(NSDictionary *)dic{
    return [[self alloc]initWithDictionary:dic];
}
- (instancetype)initWithDictionary:(NSDictionary *)dic{
    if (self = [super init]) {
        self.icon = dic[@"icon"];
        self.name = dic[@"name"];
    }
    return self;
}

@end
//
//  CarGroup.h
//  07-UITableView練習-Car
//
//  Created by 瞿傑 on 15/9/28.
//

#import <Foundation/Foundation.h>

@interface CarGroup : NSObject

@property (nonatomic,copy)NSString * title;
@property(nonatomic, strong)NSArray * cars;

+ (instancetype)carGroupWithDictionary:(NSDictionary * ) dic;
- (instancetype)initWithDictionary:(NSDictionary * )dic;

@end
//
//  CarGroup.m
//  07-UITableView練習-Car
//
//  Created by 瞿傑 on 15/9/28.
//  Copyright © 2015年 itcast. All rights reserved.
//

#import "CarGroup.h"
#import "Car.h"

@implementation CarGroup

+ (instancetype)carGroupWithDictionary:(NSDictionary * ) dic{
    return [[self alloc]initWithDictionary:dic];
}
- (instancetype)initWithDictionary:(NSDictionary * )dic{
    if (self = [super init]) {
        self.title = dic[@"title"];
        NSArray * carsDic = dic[@"cars"];
        NSMutableArray * cars = [NSMutableArray array];
        for(NSDictionary * dict in carsDic){
            Car * car = [Car carWithDictionary:dict];
            [cars addObject:car];
        }
        self.cars = cars ;
    }
    return self;
}

@end

//
//  ViewController.m
//  07-UITableView練習-Car
//
//  Created by 瞿傑 on 15/9/28.
//  Copyright © 2015年 itcast. All rights reserved.
//

#import "ViewController.h"
#import "CarGroup.h"
#import "Car.h"

@interface ViewController ()<UITableViewDataSource , UITableViewDelegate ,UIAlertViewDelegate>

@property (nonatomic , strong)NSArray * carGroup;
@property (weak, nonatomic) IBOutlet UITableView *tableView;


@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
}

// 隱藏狀態欄
- (BOOL)prefersStatusBarHidden{
    return YES;
}

// 獲得cars_total.plist中的數據
- (NSArray *)carGroup{
    if (nil == _carGroup) {
        NSString * path = [[NSBundle mainBundle] pathForResource:@"cars_total.plist" ofType:nil];
        NSArray * group = [NSArray arrayWithContentsOfFile:path];
        NSMutableArray * carGroup = [NSMutableArray array];
        for(NSDictionary * dic in group){
            CarGroup * tmp = [CarGroup carGroupWithDictionary:dic];
            [carGroup addObject:tmp];
        }
        self.carGroup = carGroup ;
    }
    return _carGroup;
}

#pragma mark - 數據源協議UIDataSource中的方法
/** 1.返回組數 */
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return self.carGroup.count ;
}

/** 2.返回第section組的行數 */
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    CarGroup * group = self.carGroup[section];
    return group.cars.count;
}

/** 3.返回顯示第indexPath.section行第 indexPath.row行的內容 */
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    //(1)可重用的標識符
    NSString * ID = @"carGroup";
    
    // (2)先從數據池中看有沒有同一類型的Cell
    UITableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:ID];
    //(3)如果沒有可重用的cell,就創建一個
    if (cell == nil) {
        NSLog(@"-----(%ld , %ld)",indexPath.section,indexPath.row);
        cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:ID];
    }
    //(4)設置cell中的內容
    CarGroup * group = self.carGroup[indexPath.section];
    Car * car = group.cars[indexPath.row];
    cell.textLabel.text = car.name;
    cell.imageView.image = [UIImage imageNamed:car.icon];
    cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    
    return cell;
}

/** 返回第section組的頭標題*/
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section{
    CarGroup * group = self.carGroup[section];
    return group.title;
}

//- (NSString *)tableView:(UITableView *)tableView titleForFooterInSection:(NSInteger)section{
//    
//}

/** 返回每組的頭標題,用於顯示在屏幕的最右邊 */
- (NSArray<NSString *> *)sectionIndexTitlesForTableView:(UITableView *)tableView{
//    NSMutableArray * titleArray = [NSMutableArray array];
//    for(CarGroup * group in self.carGroup){
//        [titleArray addObject:group.title];
//    }
//    return titleArray;
    return [self.carGroup valueForKey:@"title"];
}

#pragma mark - UITableViewDelegate代理協議中的方法

/** 選擇點擊了indexPath的cell,可做一些事*/
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    CarGroup * group = self.carGroup[indexPath.section];
    Car * car = group.cars[indexPath.row];
    
    // 彈出一個UIAlertView窗口
    UIAlertView * alert = [[UIAlertView alloc]initWithTitle:car.name message:nil delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"確定", nil];
    [alert show];
    //可實現alert的UIAlertViewDelegate代理協議中的方法,當對alert操作時,可做相應的反應
}

- (void)tableView:(UITableView *)tableView didDeselectRowAtIndexPath:(NSIndexPath *)indexPath{
    
}

/**
 *  每一行的高度不一致的時候使用這個方法來設置行高
 */
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.row == 0) return 100;
    return 60;
}

//#pragma mark - alertView的代理方法
///**
// *  點擊了alertView上面的按鈕就會調用這個方法
// *
// *  @param buttonIndex 按鈕的索引,從0開始
// */
//- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
//{
//    if (buttonIndex == 0) return;
//    
//    // 按鈕的索引肯定不是0
//    
//    // 1.取得文本框最後的文字
//    NSString *name = [alertView textFieldAtIndex:0].text;
//    //    int row = alertView.tag;
//    //    NSIndexPath *path = [NSIndexPath indexPathForRow:row inSection:0];
//    //    UITableViewCell *cell = [self.tableView cellForRowAtIndexPath:path];
//    //    cell.textLabel.text = name;
//    
//    // 2.修改模型數據
//    long int row = alertView.tag;
//   CarGroup *carGroup = self.carGroup[row];
//    carGroup.title = name;
//    
//    // 3.告訴tableView重新加載模型數據
//    // reloadData : tableView會向數據源重新請求數據
//    // 重新調用數據源的相應方法取得數據
//    // 重新調用數據源的tableView:numberOfRowsInSection:獲得行數
//    // 重新調用數據源的tableView:cellForRowAtIndexPath:得知每一行顯示怎樣的cell
//    // 全部刷新
//    //    [self.tableView reloadData];
//    
//    // 局部刷新
//    NSIndexPath *path = [NSIndexPath indexPathForRow:row inSection:0];
//    [self.tableView reloadRowsAtIndexPaths:@[path] withRowAnimation:UITableViewRowAnimationBottom];
//}
@end

LOL英雄頭像展示:

#import <Foundation/Foundation.h>

@interface QJHero : NSObject

@property (nonatomic , copy)NSString * icon;
@property (nonatomic , copy)NSString * name;
@property (nonatomic , copy)NSString * intro;

+ (instancetype)heroWithDictionary:(NSDictionary *)dic;
- (instancetype)initWithDictionary:(NSDictionary *)dic;

@end

#import "QJHero.h"

@implementation QJHero

+ (instancetype)heroWithDictionary:(NSDictionary *)dic{
    return [[self alloc]initWithDictionary:dic];
}
- (instancetype)initWithDictionary:(NSDictionary *)dic{
    if (self = [super init]) {
        self.icon = dic[@"icon"];
        self.name = dic[@"name"];
        self.intro = dic[@"intro"];
    }
    return self ;
}

@end


//
//  ViewController.m
//  06-UITableView練習-LOL
//
//  Created by 瞿傑 on 15/9/27.
//

#import "ViewController.h"
#import "QJHero.h"

@interface ViewController ()<UITableViewDataSource>

@property (weak, nonatomic) IBOutlet UITableView *tableView;
@property (nonatomic , strong) NSArray * heros;

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    
    // 設置每行的行高
    self.tableView.rowHeight = 50;
    
}


- (BOOL)prefersStatusBarHidden{
    
    return YES ;
}
- (NSArray *)heros{
    if (_heros == nil) {
        NSString * path = [[NSBundle mainBundle]  pathForResource:@"heros.plist" ofType:nil];
        NSArray * heros = [NSArray arrayWithContentsOfFile:path];
        NSMutableArray * heroArray = [[NSMutableArray alloc]init];
        for(NSDictionary * dic in heros){
            QJHero * hero = [QJHero heroWithDictionary:dic];
            [heroArray addObject:hero];
        }
        _heros = heroArray ;
    }
    return _heros ;
}
// 1.返回組數
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return  1;
}
// 2.返回第section的行數
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return  self.heros.count;
}
// 3.返回顯示第indexPath.section行第 indexPath.row行的內容
- (UITableViewCell * )tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    NSString * ID = @"A";
    // 先從數據池中看有沒有同一類型的Cell
    UITableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:ID];
    // 如果沒有就創建一個
    if (cell == nil) {
        NSLog(@"創建一個 --- %ld",(long)indexPath.row);
        cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:ID];
    }
    // 設置cell的數據內容
    QJHero * hero = self.heros[indexPath.row];
    cell.textLabel.text = hero.name; //cell.textLabel.backgroundColor = [UIColor redColor];
    cell.detailTextLabel.text = hero.intro;
    cell.imageView.image = [UIImage imageNamed:hero.icon];
    
    // 設置每行後面的配件類型爲 >
    cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    
    // 設置背景顏色
    cell.backgroundColor = [UIColor redColor];
    
    return cell ;
}
@end



發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章