UITableView的创建及使用

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

#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 [email protected]"cell";

UITableViewCell *cell =[tableViewDemo dequeueReusableCellWithIdentifier:str];

if (cell == nil) {

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

}

//    设置cell的主标题

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

//    设置cell的副标题

cell.detailTextLabel.text [email protected]"这是副标题";

//    设置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];

}

时间: 2024-08-10 02:03:17

UITableView的创建及使用的相关文章

UITableView的创建及其一些常用方法

UITableView,它的数据源继承于UITableViewDataSource,它的委托UITableViewDelegate. 一.UITableView的创建 1.代码方式: 1 UITableView *tableView=[[UITableView alloc]initWithFrame:[[UIScreen mainScreen]bounds]]; 2 tableView.backgroundColor=[UIColor grayColor]; 3 [self.view addSu

iOS UITableView表视图(1)

//在.h文件中声明一下 //例如:@property(nonatomic,strong)UITableView *table; //创建一个UITableView self.table = [[UITableView alloc] initWithFrame:self.bounds style:(UITableViewStylePlain)]; //设置行的高度 self.table.rowHeight = 260.0; //设置分割线的颜色 self.table.separatorColor

UITableView, 表视图

UITableView, 表视图     样式     1.UITableViewStylePlain, 正常样式     2.UITableViewStyleGrouped,  分组样式 行高, 默认44 tableView.rowHeight = 80; 分隔线的颜色 tableView.separatorColor = [UIColor orangeColor]; tableView.separatorStyle = UITableViewCellSeparatorStyleSingleL

关于直接创建视图UITableViewController显示(初学)

今天渣渣想直接创建一个UITableView视图作为根视图来用结果发现有警告,才明白TableView和view是不能直接作为根视图的,需要放在ViewController上.做个笔记详细了解下. 参考博文:http://blog.csdn.net/ryantang03/article/details/7749103(IOS学习笔记8—UITableViewController)里面列举了xib创建方法. //--------------------------------------分割线--

4、iOS 开发之 UITableView

一.UITableView的创建 表格控件在创建时必须指定样式,只能使用以下实例化方法 [[UITableView alloc] initWithFrame:self.view.bounds style:UITableViewStyleGrouped]; UITableView的两种样式 1> UITableViewStylePlain 2> UITableViewStyleGrouped 2.UITableView的常见属性 // 头部视图(广告) @property (nonatomic,

iOS学习之UITableView

一.UITableView的概念 UITabelView继承于UIScrollView,可以滚动. @interface UITableView : UIScrollView <NSCoding> UITableView的每一条数据对应的单元格叫做Cell,是UITableViewCell的一个对象,继承于UIView. @interface UITableViewCell : UIView <NSCoding, UIGestureRecognizerDelegate> UITab

iOS:UICollectionView的子类化创建

UICollectionView的创建基本与UITableView的创建方式相同 首先,创建继承于UICollectionView的子类 然后在初始化方法中设置一些属性 - (id)initWithFrame:(CGRect)frame { UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc] init]; flowLayout.minimumInteritemSpacing = 0; //列间距

##DAY10 UITableView基础

UITableView继承于UIScrollView,可以滚动. UITableView的每?条数据对应的单元格叫做Cell,是UITableViewCell的?个对象,继承于UIView. UITableView可以分区显?,每?个分区称为section, 每??称为row, 编号都从0开始. 系统提供了?个专门的类来整合section和row,叫做NSIndexPath. #pragma mark ———————UITableView基本属性—————————— 创建: initWithFr

iOS 仪表式数字跳动动画-b

前几天搞了 双曲线波浪动画(http://www.jianshu.com/p/7db295fd38eb)和环形倒计时动画(http://www.jianshu.com/p/d1d16dff33c9)而且感觉效果还不错,喜欢的人还很多,于是今天打算 在搞一个"仪表式数字跳动动画". 那么什么是仪表式数字跳动动画. 直接上效果 6月-27-2016 11-28-17.gif 一, 看了效果也许就明白仪表式数字跳动动画是什么鬼了(名字是我自己取得)先梳理一下思路: 首先可以看到文字在上下滑动