表视图的常用属性和方法
来源:互联网 发布:java多线程有什么用 编辑:程序博客网 时间:2024/05/21 01:53
AppDelegate.m设置根控制器
RootViewController.h
@interface RootViewController : UIViewController<UITableViewDataSource,UITableViewDelegate>{ NSMutableArray *_data; UITableView *tableView;}
RootViewController.m
- (void)dealloc{ [tableView release]; [_data release]; [super dealloc];}- (void)viewDidLoad{ [super viewDidLoad]; //创建表视图 tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 20, 320, 460) style:UITableViewStylePlain]; tableView.dataSource = self; tableView.delegate = self; [self.view addSubview:tableView]; NSArray *arrary = [[UIFont familyNames] retain]; _data = [[NSMutableArray arrayWithArray: arrary] retain]; /*____________________________UITableView的常用属性_________________________________*/// 1.设置<span style="color:#ff0000;">单元格的分割线</span> /* UITableViewCellSeparatorStyleNone, //没有分割线 UITableViewCellSeparatorStyleSingleLine, //单线分割 UITableViewCellSeparatorStyleSingleLineEtched */// tableView.separatorStyle = UITableViewCellSeparatorStyleSingleLine; //设置分割线的颜色 tableView.separatorColor = [UIColor redColor]; //2.tableView的头视图 <span style="color:#ff0000;">//头视图的frame值只有高有用</span> UIView *headerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 0, 150)]; headerView.backgroundColor = [UIColor greenColor]; tableView.tableHeaderView = headerView; [headerView release]; //在头视图上添加子视图(注意:frame得设置) UIButton *button = [UIButton buttonWithType:UIButtonTypeInfoDark]; button.frame = CGRectMake(90, 90, 20, 20); [button addTarget:self action:@selector(buttonAction) forControlEvents:UIControlEventTouchUpInside]; [headerView addSubview:button]; //3.tableView的尾视图 UIView *footerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 0, 200)]; footerView.backgroundColor = [UIColor grayColor]; tableView.tableFooterView = footerView; [footerView release]; //4.<span style="color:#ff0000;">设置行高(默认是44)</span> //如果同时设定代理方法,以代理为准 tableView.rowHeight = 90; //5.<span style="color:#ff0000;">设置特定单元格的高度[代理方法]</span> //6.设置<span style="color:#ff0000;">表视图的背景颜色</span> /* 如果在iOS7中要设置表视图的背景颜色,需要在cell中设置cell的背景为透明 */ tableView.backgroundColor = [UIColor orangeColor];}- (void)buttonAction {//________________________UITableView的常用方法__________________________// [_data removeObjectAtIndex:0]; //<span style="color:#ff0000;">1.刷新表视图的数据,让视图和数据同步</span>// [tableView reloadData]; //注意:如果cell的个数变化的时候,不能用这个方法 //2.<span style="color:#ff0000;">刷新指定的几个单元格</span>// [_data exchangeObjectAtIndex:0 withObjectAtIndex:1];// NSIndexPath *indexPath1 = [NSIndexPath indexPathForRow:0 inSection:0];// NSIndexPath *indexPath2 = [NSIndexPath indexPathForRow:1 inSection:0];// [tableView reloadRowsAtIndexPaths:@[indexPath1,indexPath2] withRowAnimation:UITableViewRowAnimationRight]; //3.<span style="color:#ff0000;">刷新指定的组</span>// [tableView reloadSections: withRowAnimation:<#(UITableViewRowAnimation)#>] //4.<span style="color:#ff0000;">查找指定单元格</span>// NSIndexPath *indexPath = [NSIndexPath indexPathForRow:4 inSection:0];// UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];// NSLog(@"cell:%@",cell); //5.<span style="color:#ff0000;">返回视图中正在显示的cell</span>// NSArray *cells = [tableView visibleCells];// NSLog(@"cells:%@",cells); //6.<span style="color:#ff0000;">滑动到指定的单元格</span> NSIndexPath *indexPath = [NSIndexPath indexPathForRow:20 inSection:0]; //第二个参数用于指定对应的单元格滑动到视图的什么位置 [tableView scrollToRowAtIndexPath:indexPath atScrollPosition:UITableViewScrollPositionBottom animated:YES];}#pragma mark - UITableView dataSource- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { return _data.count; }- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{ UITableViewCell *cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:nil]; //将数据交给cell显示 cell.textLabel.text = [_data objectAtIndex:indexPath.row]; cell.backgroundColor = [UIColor clearColor]; return [cell autorelease]; }#pragma mark - UITableView delegate//设置特定行的高度- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{ if (indexPath.row == 0) { return 90; }else if (indexPath.row == 1) { return 200; } return 44;}
0 0
- 表视图的常用属性和方法
- 表视图常用属性和方法
- UIScrollView滚动视图(常用属性以及方法)
- [JavaScript] window的常用属性和方法
- [JavaScript] window的常用属性和方法
- RecordSet的常用属性和方法
- 常用控件的属性、事件和方法
- 常用的DOM属性、方法和集合
- 正则表达式的常用方法和属性
- DOM的常用属性和方法
- WindowsMediaPlayer的常用属性和方法
- DOM常用对象的属性和方法
- MSHFLEXGRID常用的属性,事件和方法
- jgrid常用的属性和方法
- C# SerialPort常用的属性和方法
- form表单常用的属性和方法
- JS的Document属性和常用方法
- HttpWebResponse类的常用属性和方法
- 最好的我们3
- UVa 202 - Repeating Decimals
- ImageButton和ImageView
- Memcached主从同步备份负载配置
- Activity和Service通信
- 表视图的常用属性和方法
- PHP预定义变量(一)
- HLS协议解析1
- Unix时间戳转换为标准时间
- 使用java.lang.Integer需要注意的一个问题
- 【边做项目边学Android】知识点:Android控件系列之ProgressDialog与ProgressBar
- Java代码块
- 怎样分析crash dump(内存错误)
- mac下ios 7.1 版本openssl编译