码迷,mamicode.com
首页 > 其他好文 > 详细

UITableView的创建及使用

时间:2015-07-04 17:00:38      阅读:105      评论:0      收藏:0      [点我收藏+]

标签:interface   import   

个别地方没有释放,自己填入即可

#import "AppDelegate.h"


@interface AppDelegate ()


@end


@implementation AppDelegate


- (void)dealloc

{

    [_window release];

    [super dealloc];

}

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {

    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];

    // Override point for customization after application launch.

    self.window.backgroundColor = [UIColor whiteColor];

    [self.window makeKeyAndVisible];

    [_window release];

//    创建根视图

    RootViewController *root =[[RootViewController alloc]init];

//    创建导航栏

    UINavigationController *nav =[[UINavigationController alloc]initWithRootViewController:root];

    self.window.rootViewController =nav;

//    不发生隐藏

    nav.navigationBarHidden =NO;

//    不透明

    nav.navigationBar.translucent = NO;

//    为导航栏添加颜色

    nav.navigationBar.barTintColor =[UIColor redColor];

    [root release];

    [nav release];

    return YES;

}


#import <UIKit/UIKit.h>

#import "FirstViewController.h"


//UItableView需要签订两个系统代理协议

@interface RootViewController : UIViewController<UITableViewDataSource,UITableViewDelegate>

{

//    定义一个全局的UItableView

    UITableView *tableViewDemo;

}


@end





#import "RootViewController.h"


@interface RootViewController ()


@end


@implementation RootViewController


- (void)viewDidLoad {

    [super viewDidLoad];

/** 在制作每个控件的时候最好形成添加控件背景颜色的习惯,以方便前期我们确定控件的位置,方便我们对控件位置的确定以及修改,在最后界面打在完成代码优化的时候可以将各个控件的背景颜色去掉或者是注释掉*/

    

//    由于模拟器原因会出现转换页面卡顿,因此为视图添加一个背景颜色(一般选择白色)

    self.view.backgroundColor =[UIColor whiteColor];

    

//    设置tableView的位置以及大小并且设置样式

//    此处UITableView设置是是全局 UITableView *tableViewDemo

    tableViewDemo = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height- 64) style:UITableViewStylePlain];

    

//    每行分割线的颜色

    [tableViewDemo setSeparatorColor:[UIColor greenColor]];

//    分割线的样式(现在设置的是没有分割线)

    tableViewDemo.separatorStyle  =UITableViewCellSeparatorStyleSingleLine;

    

//    签订代理协议(切记此处是两个代理都要签订,少一个都会出现错误)

    tableViewDemo.delegate =self;

    tableViewDemo.dataSource =self;

    

//    为tableView添加背景颜色

    tableViewDemo.backgroundColor =[UIColor whiteColor];


    

//    创建一个View视图并设置大小(位置无关紧要)

    UIView *footView =[[UIView alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 1)];

    footView.backgroundColor =[UIColor blueColor];

    //    可以用它在最后面画线(也可以用其他控件,只是大小改变设置就行)

    tableViewDemo.tableFooterView = footView;

    //    可以用它在最前面画线(也可以用其他控件,只是大小改变设置就行)

    tableViewDemo.tableHeaderView = footView;

   

    [self.view addSubview:tableViewDemo];

    [tableViewDemo release];

}

//此方法为必须写的方法(如果不写程序直接崩溃)用来创建cell(用系统自身的cell演示)

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

    static NSString *str =@"cell";

    UITableViewCell *cell =[tableViewDemo dequeueReusableCellWithIdentifier:str];

    if (cell == nil) {

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

    }

//    设置cell的主标题

    cell.textLabel.text = @"这是主标题";

//    设置cell的副标题

    cell.detailTextLabel.text =@"这是副标题";

//    设置cell自定义右侧按钮类型

    [cell setAccessoryType:UITableViewCellAccessoryCheckmark];

//    设置cell的图片

    /**当有图片设置的时候,cell的组副标题的坐标就会发生改变位于视图的右侧,而没有图片的时候在左侧*/

    cell.imageView.image =[UIImage imageNamed:@"背景.jpg"];

    return cell;

}

//设置每个cell的高度,是可选择是否用得方法,一般个人设置的cell中都会写用来适应自己所书写的控件高度

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath

{

    return 120;

}

//此方法为必须写的方法(如果不写直接崩溃)用来确定cell的个数

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

//这个数字可以根据自己的数据多少设定(一般都是设置成数据数组的长度)

    return 2;

}

//此方法为创建26个组群

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView

{

   return 26;

}

//此方法为右侧索引

-(NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView{

    

    NSArray *arra= @[@"A",@"B",@"C",@"D",@"E",@"F",@"G",@"H",@"I",@"J",@"K",@"L",@"M",@"N",@"O",@"P",@"Q",@"R",@"S",@"T",@"U",@"V",@"W",@"S",@"Y",@"Z"];

    return arra;

}

//在每组的组框中添加头部标题

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

{

    NSArray *arra= @[@"A",@"B",@"C",@"D",@"E",@"F",@"G",@"H",@"I",@"J",@"K",@"L",@"M",@"N",@"O",@"P",@"Q",@"R",@"S",@"T",@"U",@"V",@"W",@"S",@"Y",@"Z"];

    return [arra objectAtIndex:section];

}

//cell 的点击方法(一般内部操作就是页面跳转和数据传送)

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{

    FirstViewController *first =[[FirstViewController alloc]init];

    [self.navigationController pushViewController:first animated:YES];

}


本文出自 “ios” 博客,请务必保留此出处http://10136044.blog.51cto.com/10126044/1670790

UITableView的创建及使用

标签:interface   import   

原文地址:http://10136044.blog.51cto.com/10126044/1670790

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!