ViewController UITableView,viewcontroller

來源:互聯網
上載者:User

ViewController UITableView,viewcontroller

//

//  ViewController.m

//  CarDemo

//

//  Created by GuoYule on 15/1/20.

//  Copyright (c) 2015年 GuoYule. All rights reserved.

//


#import "ViewController.h"

#import "CarGroup.h"

#import "GYLcar.h"

@interface ViewController ()<UITableViewDataSource>

/**

 *  車品牌資料

 */

@property(nonatomic,strong)NSArray * carGroups;

@end


@implementation ViewController


- (void)viewDidLoad {

    [superviewDidLoad];

    // Do any additional setup after loading the view, typically from a nib.

}


-(BOOL)prefersStatusBarHidden

{

    return YES;

}

-(NSArray *)carGroups


{

   if (_carGroups ==nil) {

       // 初始化

        

        // 1.獲得plist的全路徑

        NSString *path = [[NSBundlemainBundle] pathForResource:@"cars_total.plist"ofType:nil];

        

        // 2.載入數組

       NSArray *dictArray = [NSArrayarrayWithContentsOfFile:path];

        

        // 3.將dictArray裡面的所有字典轉成模型對象,放到新的數組中

       NSMutableArray *carGroupsArray = [NSMutableArrayarray];

       for (NSDictionary *dictin dictArray) {

            // 3.1.建立模型對象

           CarGroup *group = [CarGroupgroupWithDict:dict];

            

            // 3.2.添加模型對象到數組中

            [carGroupsArrayaddObject:group];

        }

        

       // 4.賦值

       _carGroups = carGroupsArray;

    }

    return_carGroups;

    

    

}

#pragma mark -資料來源模型

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView

{

    return self.carGroups.count;

    

}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section

{

   CarGroup * group = self.carGroups[section];

   return group.cars.count;

}



- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath

{

    //定義一個迴圈標識

   static NSString * ID =@"car";

    //2.從緩衝池中取出可迴圈利用的cell

    UITableViewCell * cell = [tableViewdequeueReusableCellWithIdentifier:ID];

    //3.如果緩衝池中沒有那麼就要建立一個 並且要賦予標識

   if (cell == nil)

    {

        cell = [[UITableViewCellalloc] initWithStyle:UITableViewCellStyleDefaultreuseIdentifier:ID];

    }

    //4.設定資料

   CarGroup * group = self.carGroups[indexPath.section];

   GYLcar * car = group.cars[indexPath.row];

    cell.imageView.image = [UIImageimageNamed:car.icon];

    cell.textLabel.text = car.name;

   return  cell;

}

/**

 *  第section組顯示的頭部標題


 */

-(NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section

{

   CarGroup * group = self.carGroups[section];

   return group.title;

}

/**

 *  返回回右邊索引跳顯示的字串資料

 */

-(NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView{

    return [self.carGroupsvalueForKeyPath:@"title"];//這是KVC可以間接取值

    //為什麼不用[self.carGroups valueForKey:@"title"]因為這個不會間接取值

    

}


@end



聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.