12-26 tableView的学习心得

一:基础部分

UITableView的两种样式:

注意是只读的

1.UITableViewStytlePlain(不分组的)

n

 

2.UITableViewStyleGrouped(分组的)

二:如何展示数据

1.

(1)UITableView需要一个数据源(dataSource)来显示数据;

 

需要注意dataSource是UITableView的一个属性,类型是id(任意形),但是需要遵守<UITableViewDataSource>协议!

 

(2)UITableView会向数据源查询一共多少行数据以及每一行显示什么数据等;

(3)没有设置数据源的UITableView只是一个空壳;

(4)凡是遵守UITableViewDataSource协议的OC对象,都可以是UITableView的数据源;

UITableViewDataSource协议代码:

有这样两个必须实现的方法

@required

 

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

//每一行有多少组(section)数据

 

// Row display. Implementers should *always* try to reuse cells by setting each cell's reuseIdentifier and querying for available reusable cells with dequeueReusableCellWithIdentifier:

// Cell gets various attributes set automatically based on table (separators) and data source (accessory views, editing controls)

 

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

//每一行显示什么内容

 还有一个常用的optional的方法

@optional

 

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView;              // Default is 1 if not implemented

//这个表示共计多少组数据

三:代码设置UITableView

//

//  ViewController.m

//  tableView

//

//  Created by Mac on 15/12/26.

//  Copyright © 2015年 Mac. All rights reserved.

//

 

#import "ViewController.h"

 

@interface ViewController ()<UITableViewDataSource>

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

 

@end

 

@implementation ViewController

 

- (void)viewDidLoad {

    [super viewDidLoad];

//    1.设置tableView的数据源,也就是ViewController自己,它自己也是个OC对象且遵守UITableViewDataSource协议!

    self.tableView.dataSource =  self;

}

- (BOOL)prefersStatusBarHidden{

    return YES;

}

#pragma mark - tableView的数据源方法

//1.设置每一组有多少行数据

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

{

    if (section == 0) {

        return 2;

    }else{

        return 3;

    }

}

//2.设置总计多少组

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView

{

    return 2;

}

 

- (void)didReceiveMemoryWarning {

    [super didReceiveMemoryWarning];

 

}

//3.设置每一行的内容

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

//其中,indexPath表示索引

//如:indexPath.row表示某一行,index.section表示某一组

{

    UITableViewCell *cell = [[UITableViewCell alloc]init];

//    将第一组第一行的cell的内容设置为红色的“征里”;

    if (indexPath.section == 0) {

        if (indexPath.row == 0) {

            cell.textLabel.text = @"征里";

            cell.textLabel.textColor = [UIColor redColor];

            return cell;

        }else{

            cell.textLabel.text = @"征里";

            return cell;

        }

    }else {

        cell.textLabel.text = @"征里";

        return cell;

    }

  

}

//4.设置每组脚与头显示

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

{

    if (section == 0) {

        return @"第一组的征里!";

    }else{

    return @"第二组的征里!";

  }

}

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

{

    if (section == 0) {

        return @"第一组的征里 @@@";

    }else{

        return @"第二组的征里 @@@";

    }

}

@end

 效果如下

 

 

转载于:https://www.cnblogs.com/BJTUzhengli/p/5077384.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值