UITableView常用来展示数据,类似于Android中的ListView,相对于Android中的ListView而言,UITableView的实现是非常简单,继承UITableViewDataSource,UITableViewDelegate然后根据需要是实现对应的方法即可。UITableView有两个默认的内置风格,Plain和Grouped,Plain表明表格视图自身没有真正地在你自己实际地提供任何外观之前提供很多的外观,大部分情况下,它会做的唯一的事情是它会给你这些header和footer。Grouped表格视图是UIKit提供的分组风格。风格的话如果有特别的需求,还可以自定义分组的风格。

页面布局

页面比较简单,一个简单UITableView:

头文件中的不需要声明,需要实现一下协议:

@interface ViewController : UIViewController <UITableViewDataSource,UITableViewDelegate>@end

Demo实现

声明三个数据用来展示数据:

@interface ViewController (){    NSArray *channelArr;    NSMutableArray *filmArr;    NSMutableArray *tvArr;}@end

初始化数据:

- (void)viewDidLoad {    [super viewDidLoad];    // Do any additional setup after loading the view, typically from a nib.    channelArr=[[NSArray alloc] initWithObjects:@"电影",@"电视剧",nil];    filmArr=[[NSMutableArray alloc] initWithObjects:@"智取威虎山",@"一步之遥",@"匆匆那年",@"北京爱情故事",nil];    tvArr=[[NSMutableArray alloc] initWithObjects:@"何以笙箫默",@"锋刃",@"陆小凤与花满楼",@"武媚娘传奇",nil];}

 设置分组的组数:

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{    NSLog(@"%lu",(unsigned long)channelArr.count);    return [channelArr count];}

设置分组的标题:

- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section{    return [channelArr objectAtIndex:section];}

设置每个分组下内容的个数:

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{    NSInteger count=0;    switch (section) {        case 0:            count=[filmArr count];            break;        case 1:            count=[tvArr count];            break;    }    return count;}

设置每个分组下的具体内容:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{    UITableViewCell *cell =[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:nil];    switch (indexPath.section) {        case 0:            [cell.textLabel setText:[filmArr objectAtIndex:indexPath.row]];            break;        case 1:            [cell.textLabel setText:[tvArr objectAtIndex:indexPath.row]];            break;    }    return cell;}

 设置分组标题和底部的高度:

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{    return 40;}- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{    return 0;}

 设置点击事件:

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{    NSString *content;    switch (indexPath.section) {        case 0:            content=[NSString stringWithFormat:@"%@-%@",channelArr[0],[filmArr objectAtIndex:indexPath.row]];            break;        case 1:               content=[NSString stringWithFormat:@"%@-%@",channelArr[1],[tvArr objectAtIndex:indexPath.row]];            break;    }    UIAlertView *alterView=[[UIAlertView alloc] initWithTitle:@"当前位置:" message:content delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil];    [alterView show];}

 最终效果:

源代码:

////  ViewController.m//  TableView//http://www.cnblogs.com/xiaofeixiang///  Created by keso on 15/1/24.//  Copyright (c) 2015年 keso. All rights reserved.//#import "ViewController.h"@interface ViewController (){    NSArray *channelArr;    NSMutableArray *filmArr;    NSMutableArray *tvArr;}@end@implementation ViewController- (void)viewDidLoad {    [super viewDidLoad];    // Do any additional setup after loading the view, typically from a nib.    channelArr=[[NSArray alloc] initWithObjects:@"电影",@"电视剧",nil];    filmArr=[[NSMutableArray alloc] initWithObjects:@"智取威虎山",@"一步之遥",@"匆匆那年",@"北京爱情故事",nil];    tvArr=[[NSMutableArray alloc] initWithObjects:@"何以笙箫默",@"锋刃",@"陆小凤与花满楼",@"武媚娘传奇",nil];}//设置分组的组数- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{    NSLog(@"%lu",(unsigned long)channelArr.count);    return [channelArr count];}//设置分组的标题- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section{    return [channelArr objectAtIndex:section];}//- (NSString *)tableView:(UITableView *)tableView titleForFooterInSection:(NSInteger)section{//    return @"我是底部";//}//设置每个分组的个数- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{    NSInteger count=0;    switch (section) {        case 0:            count=[filmArr count];            break;        case 1:            count=[tvArr count];            break;    }    return count;}//设置分组中具体的内容- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{    UITableViewCell *cell =[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:nil];    switch (indexPath.section) {        case 0:            [cell.textLabel setText:[filmArr objectAtIndex:indexPath.row]];            break;        case 1:            [cell.textLabel setText:[tvArr objectAtIndex:indexPath.row]];            break;    }    return cell;}//分组标题的行高- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{    return 40;}- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{    return 0;}//选中点击事件- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{    NSString *content;    switch (indexPath.section) {        case 0:            content=[NSString stringWithFormat:@"%@-%@",channelArr[0],[filmArr objectAtIndex:indexPath.row]];            break;        case 1:               content=[NSString stringWithFormat:@"%@-%@",channelArr[1],[tvArr objectAtIndex:indexPath.row]];            break;    }    UIAlertView *alterView=[[UIAlertView alloc] initWithTitle:@"当前位置:" message:content delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil];    [alterView show];}- (void)didReceiveMemoryWarning {    [super didReceiveMemoryWarning];    // Dispose of any resources that can be recreated.}@end

更多相关文章

  1. Android应用开发中使用GridView网格布局的代码示例
  2. EditText实现横向光标
  3. CardView卡片效果
  4. android Permission 访问权限许可 大全
  5. Android隐藏标题栏及解决启动闪过标题的实例详解
  6. TabLayout的基本使用
  7. Golang MD5算法与 java(android)的互通
  8. Android设置中的Preferencescreen用法介绍与分析
  9. android--菜单

随机推荐

  1. 横竖屏转换
  2. 在网址中跳转APP(进入XXXAPP内查看)
  3. Android application context/activity c
  4. Android之TextView属性详解
  5. android键盘输入读取
  6. android基础入门(一)——搭建android开发
  7. Android 多线程----AsyncTask异步任务详
  8. Android常见面试题
  9. Android开源项目解析:PullToRefresh
  10. Failed to fetch URL http://dl-ssl.goog