ios tableView那些事(二)给tableView添加数据

我们上一章已经创建了tableview现在我们给它添加点数据吧


#import <UIKit/UIKit.h>

 

/*tableview 一定要用到这两个delegate  UITableViewDataSource,UITableViewDelegate */

@interface ViewController :UIViewController <UITableViewDataSource,UITableViewDelegate>

{

    UITableView *tableview;

    NSArray *array; //创建个数组来放我们的数据

    

}

@property (strong,nonatomic)UITableView *tableview;

@property (strong,nonatomic)NSArray *array;

@end


#import "ViewController.h"


@interfaceViewController ()


@end


@implementation ViewController

@synthesize tableview;

@synthesize array;

- (void)viewDidLoad

{

    [superviewDidLoad];

tableview = [[UITableViewalloc]initWithFrame:CGRectMake(0, 0,self.view.bounds.size.width,self.view.bounds.size.height)style:UITableViewStylePlain];

    

//    UITableViewStylePlain,                

//    UITableViewStyleGrouped

   

    tableview.delegate =self;//不要忘写了这两句话哟调用delegate*/

    tableview.dataSource=self;

    [self.viewaddSubview:tableview];

    

    NSMutableArray *arrayValue = [[NSMutableArrayalloc]init];

    

    for (int i = 0; i< 10; i++)

    {

        NSString *value = [NSStringstringWithFormat:@"%d",i];

        [arrayValue addObject:value];

    }

    

     array = arrayValue;

    

}


/* 这个函数是显示tableview的章节数*/

-(NSInteger)numberOfSectionsInTableView:(UITableView*)tableView

{

    return 1;

}

/* 这个函数是指定显示多少cells*/

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

{

    return [arraycount];//这个是指定加载数据的多少即显示多少个cell,如果这个地方弄错了会崩溃的哟

}



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

{

    

    

    //定义个静态字符串为了防止与其他类的tableivew重复

    static NSString *CellIdentifier = @"Cell";  

    //定义cell的复用性当处理大量数据时减少内存开销

    UITableViewCell *cell = [tableviewdequeueReusableCellWithIdentifier:CellIdentifier];

    

    if (cell ==nil)

    {

        

        cell = [[UITableViewCellalloc]initWithStyle:UITableViewCellStyle: UITableViewCellStyleDefault:CellIdentifier];

    }

    

    cell.textLabel.text = [arrayobjectAtIndex:[indexPathrow]];  //通过 [indexPath row] 遍历数组

    

    return cell;

}

- (void)didReceiveMemoryWarning

{

    [superdidReceiveMemoryWarning];

    // Dispose of any resources that can be recreated.

}


@end

是不是很简单



现在有些过于简单那么我们在加些东西吧

我们的cell styl 也有四种样式


 cell = [[UITableViewCellalloc]initWithStyle:UITableViewCellStyleDefaultreuseIdentifier:CellIdentifier];

上图是默认格式

    

//    UITableViewCellStyleDefault,// Simple cell with text label and optional image view (behavior of UITableViewCell in iPhoneOS 2.x)

//    UITableViewCellStyleValue1,// Left aligned label on left and right aligned label on right with blue text (Used in Settings)

//    UITableViewCellStyleValue2,// Right aligned label on left with blue text and left aligned label on right (Used in Phone/Contacts)

//    UITableViewCellStyleSubtitle// Left aligned label on top and left aligned label on bottom with gray text (Used in iPod).


我们在下面的函数里加上这行代码

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

{

   

    

    //定义个静态字符串 为了防止与其他类的tableivew重复

    static NSString *CellIdentifier = @"Cell";  

    //定义cell 的复用性 当处理大量数据时减少内存开销

    UITableViewCell *cell = [tableview dequeueReusableCellWithIdentifier:CellIdentifier];

   

    if (cell == nil)

    {

        

        cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];

    }

    

    cell.textLabel.text = [array objectAtIndex:[indexPath row]];  //通过 [indexPath row] 遍历数组

    cell.detailTextLabel.text = [arrayobjectAtIndex:[indexPathrow]];

    return cell;

}



是不是有种熟悉的感觉呢

接下来看看剩下的两种风格


 cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleValue1reuseIdentifier:CellIdentifier];




 cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleValue2reuseIdentifier:CellIdentifier];

 



  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值