表视图(一)学习总结

表视图结构:
表视图:头视图、单元格、尾视图三部分组成

    一 基础部分
       1.表视图的样式:
 @property (nonatomic, readonly) UITableViewStyle style;
    UITableViewStylePlain(不分组)
        
UITableViewStyleGrouped(分组的):
    
2.初始化UITableView:
        UITableView    *tableView1 = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, kScreenHeight) style:UITableViewStylePlain];
3.实现数据源和代理:只有实现数据源才能展现数据
        
4.NSIndex:
5.常用数据源的方法:
    @required
//每一行有多少组(section)数据
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section;

//每一行显示什么内容
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath;

6.可选方法:
    @optional
//这个表示共计多少组数据
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView; // Default is 1 if not implemented
单元格的方法
//组中头部视图的标题
- (nullable NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section; 

//组中尾部视图的标题
- (nullable NSString *)tableView:(UITableView *)tableView titleForFooterInSection:(NSInteger)section;

// Editing:可编辑
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath;

// Moving/reordering:可移动
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath;

// Index:返回组中的所有标题
- (nullable NSArray<NSString *> *)sectionIndexTitlesForTableView:(UITableView *)tableView __TVOS_PROHIBITED; 

//返回组中标题的索引
- (NSInteger)tableView:(UITableView *)tableView sectionForSectionIndexTitle:(NSString *)title atIndex:(NSInteger)index __TVOS_PROHIBITED; 

//提交编辑的样式
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath;

//移动到目标单元格
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)sourceIndexPath toIndexPath:(NSIndexPath *)destinationIndexPath;

7.常用代理方法:

//当单元格被选中时调⽤用
- (void)tableView:(UITableView *)tableView

        didSelectRowAtIndexPath:(NSIndexPath *)indexPath;

// 配置⾏行⾼高
- (CGFloat)tableView:(UITableView *)tableView

       heightForRowAtIndexPath:(NSIndexPath *)indexPath;

// 设置section 头部、尾部视图的⾼高度
- (CGFloat)tableView:(UITableView *)tableView

              heightForHeaderInSection:(NSInteger)section;
  - (CGFloat)tableView:(UITableView *)tableView
            heightForFooterInSection:(NSInteger)section;

// ⾃自定义section头部、尾部视图,注意:需要指定⾼高度
- (UIView *)tableView:(UITableView *)tableView

                viewForHeaderInSection:(NSInteger)section;
  - (UIView *)tableView:(UITableView *)tableView
             viewForFooterInSection:(NSInteger)section;
8.表视图常用属性:
     //消除表格的分割线
    tableView1.separatorStyle = UITableViewCellSeparatorStyleNone;
     //行高
    tableView1.rowHeight = 150;    
    //背景图片
    tableView1.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"view-background.jpg"]];
    //添加头部视图
    tableView1.tableHeaderView =reuseLoopView;
    
9.代码的实现:
//
// 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;
}
//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
运行效果:
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值