您的位置:首页 > 移动开发 > IOS开发

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

2013-08-28 14:19 483 查看
我们上一章已经创建了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];

 
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  ios tableview