UITableView的基本用法

来源:互联网 发布:淘宝北海家自制护肤品 编辑:程序博客网 时间:2024/05/17 20:28

今天给同学们来讲一下UITableView的基本用法,那么我们今天就讲一个汽车的展示列表我们从最最基本最直观的角度来讲解,适合与新手的学习和进步!接下来的博客分享我会把UITableView我所学的东西我所会掌握的东西全部为同学们讲解~那么废话不多说直接上代码!




//

//  ZZViewController.m

//  02- 展示汽车品牌

//

//  Created by 周昭 on 16/10/27.

//  Copyright © 2016 HT_Technology. All rights reserved.

//


#import "ZZViewController.h"


@interface ZZViewController()

/**

 *  此处我们通过storyboard加载tableView并且设置代理和数据源为该控制器

 */

@property (weak,nonatomic)IBOutletUITableView *tableView;


@end


@implementation ZZViewController


- (void)viewDidLoad

{

    [superviewDidLoad];

}


#pragma mark - 这里隐藏状态栏

- (BOOL)prefersStatusBarHidden

{

    returnYES;

}


#pragma mark - 数据源方法

/**

 *  一共有多少组数据

 */

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView

{

    //    NSLog(@"numberOfSectionsInTableView-一共有多少组数据");

    return3;

}


/**

 *  section组有多少行

 */

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

{

    //    NSLog(@"numberOfRowsInSection-%d", section);

    

#pragma mark - 那么写到这里只是给那些初级入门的初学者看怎么样最直观最直接去实践一个列表的功能可是如果后期的你数据改变那你这个控制器不是死的很惨吗?你的改动不就很大吗?

    if (section ==0) {//德系品牌

        return3;

    } elseif (section ==1){ //日韩品牌

        return4;

    } else {//欧系品牌

        return2;

    }

}

/**

 *  每一行显示怎样的内容(cell)

 */

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

{

    //    NSLog(@"cellForRowAtIndexPath-%d%d", indexPath.section, indexPath.row);

    UITableViewCell *cell = [[UITableViewCellalloc]initWithStyle:UITableViewCellStyleDefaultreuseIdentifier:nil];

    

    if (indexPath.section ==0) {//德系品牌(0)

        

        if (indexPath.row ==0) {//0组的第0

            cell.textLabel.text =@"奥迪";

        } elseif (indexPath.row ==1) { //0组的第1

            cell.textLabel.text =@"宝马";

        } elseif (indexPath.row ==2) {

            cell.textLabel.text =@"奔驰";

        }

#pragma mark - 同上面的解释如果改动了那么你的控制器不会死的很惨而且你改动的代码将会是无数那么现在开始重构并且拓展功能

    } elseif (indexPath.section ==1) { //日韩品牌(1)

        

        if (indexPath.row ==0) {//1组的第0

            cell.textLabel.text =@"本田";

        } elseif (indexPath.row ==1) { //1组的第1

            cell.textLabel.text =@"丰田";

        } elseif (indexPath.row ==2) {

            cell.textLabel.text =@"铃木";

        } elseif (indexPath.row ==3) {

            cell.textLabel.text =@"三菱";

        }

        

    } elseif (indexPath.section ==2) { //欧系品牌(2)

        

        if (indexPath.row ==0) {//2组的第0

            cell.textLabel.text =@"兰博基尼";

        } elseif (indexPath.row ==1) { //2组的第1

            cell.textLabel.text =@"劳斯莱斯";

        }

    }

    return cell;

}


/**

 *  section组显示怎样的头部标题

 */

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

{

    if (section ==0) {

        return@"德系品牌";

    } elseif (section ==1) {

        return@"日韩品牌";

    } else {

        return@"欧系品牌";

    }

}


/**

 *  section组显示怎样的尾部标题

 */

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

{

    if (section ==0) {

        return@"世界一流品牌";

    } elseif(section ==1) {

        return@"牛逼哄哄";

    } else {

        return@"价格昂贵";

    }

}


@end







0 0