Chinaunix首页 | 论坛 | 博客
  • 博客访问: 1049190
  • 博文数量: 403
  • 博客积分: 10272
  • 博客等级: 上将
  • 技术积分: 4407
  • 用 户 组: 普通用户
  • 注册时间: 2012-02-24 14:22
文章分类

全部博文(403)

文章存档

2012年(403)

分类: 嵌入式

2012-05-08 16:28:27

UITableView是app开发中常用到的控件,功能很强大,多用于数据的显示。下面以一个简单的实例来介绍tableview的基本用法。(适合新手,高手飘过)

@interface TableViewTestViewController : UIViewController<UITableViewDataSource,UITableViewDelegate>{

    UITableView *DataTable;

    NSMutableArray *dataArray1; //定义数据数组1

    NSMutableArray *dataArray2;//定义数据数组2

    NSMutableArray *titleArray;//定义标题数组

}

 

- (void)viewDidLoad

{

    [superviewDidLoad];

//初始化tableview

    DataTable = [[UITableViewalloc] initWithFrame:CGRectMake(0, 0, 320, 420)];//指定位置大小

    [DataTablesetDelegate:self];//指定委托

    [DataTablesetDataSource:self];//指定数据委托

    [self.viewaddSubview:DataTable];//加载tableview

    

    dataArray1 = [[NSMutableArrayalloc] initWithObjects:@"中国", @"美国", @"英国", nil];//初始化数据数组1

    dataArray2 = [[NSMutableArrayalloc] initWithObjects:@"黄种人", @"黑种人", @"白种人", nil];//初始化数据数组2

    titleArray = [[NSMutableArrayalloc] initWithObjects:@"国家", @"种族", nil];//初始化标题数组

    

}

 

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation

{

    // Return YES for supported orientations

    return (interfaceOrientation == UIInterfaceOrientationPortrait);

}

 

 

//每个section显示的标题

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

    switch (section) {

        case 0:

            return [titleArray objectAtIndex:section];//提取标题数组的元素用来显示标题

        case 1:

            return [titleArray objectAtIndex:section];//提取标题数组的元素用来显示标题

        default:

            return @"Unknown";

    }

 

}

 

//指定有多少个分区(Section),默认为1

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {

    return [titleArray count];//返回标题数组中元素的个数来确定分区的个数

}

 

//指定每个分区中有多少行,默认为1

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

    switch (section) {

        case 0:

           return  [dataArray1 count];//每个分区通常对应不同的数组,返回其元素个数来确定分区的行数

            break;

        case 1:

            return  [dataArray2 count];

            break;

        default:

            return 0;

            break;

    }

}

 

//绘制Cell

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

 

    static NSString *CellIdentifier = @"Cell";

 //初始化cell并指定其类型,也可自定义cell

UITableViewCell *cell = (UITableViewCell*)[tableView  dequeueReusableCellWithIdentifier:CellIdentifier];

  if(cell == nil

  {

  cell = [[[UITableViewCellalloc

  initWithFrame:CGRectZero 

  reuseIdentifier:CellIdentifier] autorelease];

}

   switch (indexPath.section) {

  case 0://对应各自的分区

    [[cell textLabel]  setText:[dataArray1 objectAtIndex:indexPath.row]];//给cell添加数据

    break;

  case 1:

    [[cell textLabel]  setText:[dataArray2 objectAtIndex:indexPath.row]];

    break;

  default:

    [[cell textLabel]  setText:@"Unknown"];

}

  return cell;//返回cell

}

 

tableview还有很多高难度的属性和接口,在以后我会慢慢补齐。

阅读(1666) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~