您的位置:首页 > 产品设计 > UI/UE

UITableView的基本用法

2016-10-27 18:00 288 查看
今天给同学们来讲一下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



内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: