表视图的常用属性和方法

来源:互联网 发布: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