IOS学习笔记之八:tableView的创建

1.先附上代码

#import "CardGameViewController.h"


@interface CardGameViewController ()


@end


@implementation CardGameViewController


NSArray *arrayForTable;

- (void)viewDidLoad

{

    [super viewDidLoad];

// Do any additional setup after loading the view, typically from a nib.

    _firstTable.dataSource=self;

    _firstTable.delegate=self;

    arrayForTable=@[@"1",

                    @"2",

                    @"3",

                    @"4",

                    @"5",

                    @"6",

                    @"7",

                    @"8",

                    @"9",

                    @"10"];

}


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

{

    return [arrayForTable count];

}


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

{

    static NSString *tableIdentifier=@"firstTableCell";

    UITableViewCell *thisCell =[tableView dequeueReusableCellWithIdentifier:tableIdentifier];

    if(thisCell==nil)

    {

        thisCell=[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:tableIdentifier];

    }

    thisCell.textLabel.text=[arrayForTable objectAtIndex:[indexPath row]];

    return thisCell;

    

}

- (void)didReceiveMemoryWarning

{

    [super didReceiveMemoryWarning];

    // Dispose of any resources that can be recreated.

}


@end

2.关于delegate的理解,view中含有一个table view控件,而对这个控件进行初始化,需要委托给viewController来做,于是viewController需要加上protocol
叫做 < UITableViewDelegate , UITableViewDataSource >,之后在头文件中加入table的声明
流程化整个过程


给viewController加上tableview的delegate和datasource的protocol
在viewController的头文件中加入对这个table的声明
创建一个数组用来存放tableview每一个row中的内容
在m文件viewDidLoad方法中实现viewController对tableview的delegate和datasource的调用
同时在viewDidLoad的方法中初始化数组用来tableview的每一栏要显示的内容
接下来实现tableview delegate的方法 -( NSInteger ) tableView:( UITableView *)tableView numberOfRowsInSection:( NSInteger )section

    上面的方法中要返回想要的table的数量,在这里是所创建的数组的大小
然后再实现另一个必须要实现的方法-(UITableViewCell *) tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath

在这个方法里,需要返回tableviewcell,而且需要对这个tableview加一个标签,也就是identifier,并对这个cell的text进行设置,将数组的位置和table的row的位置对应起来

如果要将一个表格分割成两个sections,则需要加入下列代码

-(NSInteger) numberOfSectionsInTableView:(UITableView *)tableView

{

    return 2;

}

然后,在进行两个必须实现的方法的时候,分别提取indexPath的section的值进行比较分别赋值就行了,注意从0开始,而且section的分割符也可以改变。
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值