#UITabelView#的基本使用

  • UITableViwe的基本使用

//
//  ViewController.m
//  01-展示多组数据
//
//  Created by xiaomage on 16/1/7.
//  Copyright © 2016年 小码哥. All rights reserved.
//

#import "ViewController.h"

@interface ViewController ()<UITableViewDataSource>
@property (weak, nonatomic) IBOutlet UITableView *tableView;

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // 设置数据源
    self.tableView.dataSource = self;
}

#pragma mark - UITableViewDataSource
/**
 *  告诉tableView一共有多少组数据
 */
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 4;
}

/**
 *  告诉tableView第section组有多少行
 */
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if (section == 0) { // 第0组
        return 2;
    } else if (section == 1){ // 第1组
        return 6;
    } else if (section == 2){ // 第2组
        return 6;
    } else { // 最后 一组
        return 1;
    }
}

/**
 *  告诉tableView每一行显示的内容(tableView每一行都是UITableViewCell或者它的子类)
 */
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell = [[UITableViewCell alloc] init];
    if (indexPath.section == 0) { // 第0组
        if (indexPath.row == 0) { // 第0组第0行
            cell.textLabel.text = @"通用";
        } else if (indexPath.row == 1){ // 第0组第1行
            cell.textLabel.text = @"隐私";
        }
    } else {
        cell.textLabel.text = [NSString stringWithFormat:@"%ld组%zd行-其他数据",indexPath.section,indexPath.row];
    }
    
    return cell;
}

@end

  • 展示汽车数据(简单展示)

//
//  ViewController.m
//  02-展示汽车数据
//
//  Created by xiaomage on 16/1/7.
//  Copyright © 2016年 小码哥. All rights reserved.
//

#import "ViewController.h"

@interface ViewController () <UITableViewDataSource>
@property (weak, nonatomic) IBOutlet UITableView *tableView;

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
}

#pragma mark- UITableViewDataSource
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 2;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if (section == 0) {
        return 2;
    } else {
        return 3;
    }
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell = [[UITableViewCell alloc] init];
    
    // 设置cell右边显示的控件
    // accessoryView优先级 > accessoryType
//    cell.accessoryView = [[UISwitch alloc] init];
    // 设置cell右边的指示样式
    cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    
    if (indexPath.section == 0) {
        if (indexPath.row == 0) {
            cell.textLabel.text = @"奔驰";
            cell.imageView.image = [UIImage imageNamed:@"m_2_100"];
        } else if (indexPath.row == 1) {
            cell.textLabel.text = @"宝马";
            cell.imageView.image = [UIImage imageNamed:@"m_3_100"];
        }
    } else if (indexPath.section == 1) {
        if (indexPath.row == 0) {
            cell.textLabel.text = @"丰田";
            cell.imageView.image = [UIImage imageNamed:@"m_7_100"];
        } else if (indexPath.row == 1) {
            cell.textLabel.text = @"马自达";
            cell.imageView.image = [UIImage imageNamed:@"m_18_100"];
        } else if (indexPath.row == 2) {
            cell.textLabel.text = @"本田";
            cell.imageView.image = [UIImage imageNamed:@"m_26_100"];
        }
    }
    return cell;
}

/**
 *  告诉tableView每一组的头部标题
 */
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
    if (section == 0) {
        return @"德系品牌";
    } else {
        return @"日系品牌";
    }
}

/**
 *  告诉tableView每一组的尾部标题
 */
- (NSString *)tableView:(UITableView *)tableView titleForFooterInSection:(NSInteger)section
{
    if (section == 0) {
        return @"德系品牌GHGHGHGHGHG";
    } else {
        return @"日系品牌HJHJHJKUUUUUU";
    }
}

@end

  • 展示汽车数据(使用模型优化)


最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容