UITableView

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

1、创建

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

2、常见属性设置

  self.tableView.separatorStyle = UITableViewCellSeparatorStyleSingleLine;

    self.tableView.separatorColor = [UIColor redColor];

    self.tableView.backgroundColor = [UIColor orangeColor];

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 = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];

    

    

    // Configure the cell...

    

    cell.textLabel.text = [self.dataArray objectAtIndex: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 = [[UIAlertView alloc]initWithTitle:[NSString stringWithFormat:@"您已选中了%d",indexPath.row] message:@"您已选中了" delegate:nil cancelButtonTitle:@"取消选中" otherButtonTitles:@"确认选中", nil];

    [alertViewSelect show];

}

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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值