UITableView

来源:互联网 发布:玩具战争防御塔数据 编辑:程序博客网 时间:2024/06/08 02:25

UITableView,如其义,多用于以表格的形式展示数据。基本用法如下:

1、创建

self.tableView = [[UITableViewalloc]initWithFrame:CGRectMake(0,0, 320,460) style:UITableViewStyleGrouped];

2、常见属性设置

  self.tableView.separatorStyle =UITableViewCellSeparatorStyleSingleLine;

    self.tableView.separatorColor = [UIColorredColor];

    self.tableView.backgroundColor = [UIColororangeColor];

3、设置delegate及datasource

self.tableView.delegate =self;

    self.tableView.dataSource =self;

4、实现dataSource和delegate的方法

//必须实现的3dataSource方法

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView

{

    return 1;

}


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

{

    return self.dataArray.count;

}



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

{

    static NSString *CellIdentifier =@"txTableCell";

    

    

    UITableViewCell *cell = [tableViewdequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];

    

    

    // Configure the cell...

    

    cell.textLabel.text = [self.dataArrayobjectAtIndex:indexPath.row];

    

    

    return cell;

}

//设置tableViewtableViewCell的高度

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

{

    return 40.0f;

}


//设置每个sectionheader的高度

- (CGFloat) tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section

{

    return 50.0f;

}


- (CGFloat) tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section

{

    return 50.0f;

}


- (void) tableView:(UITableView *)tableView didDeselectRowAtIndexPath:(NSIndexPath *)indexPath

{

    UIAlertView *alertViewSelect = [[UIAlertViewalloc]initWithTitle:[NSStringstringWithFormat:@"您已选中了%d",indexPath.row] message:@"您已选中了" delegate:nil cancelButtonTitle:@"取消选中" otherButtonTitles:@"确认选中",nil];

    [alertViewSelect show];

}

详细代码可以参照:https://github.com/tingxuan/txUITableViewDemo