表控件 TableView

本文详细介绍了如何在iOS应用中使用UITableView实现数据展示及交互功能,包括数据源配置、单元格重用、高度自定义、点击事件处理等关键步骤。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

表控件 TableView

协议:UITableViewDataSource,UITableViewDelegate
委托函数:

-(NSInteger) tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section//返回表有几列。

 {

 return [self.data count];

 }
 - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath//每一列分别是什么内容

 {

 NSString *SimpleTableIdentifier =@"SimpleTableIdentifier";

 UITableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:SimpleTableIdentifier];

 if(cell==nil){

 cell=[[UITableViewCell alloc ] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:SimpleTableIdentifier];//identifier标识符号

 //还可以根据tag获取控件 
 UILabel * title=(UILabel *)[cell viewWithTag:1];

 }
 UIImage *image=[UIImage imageNamed:@"123”];//图标
     cell.imageView.image=image;



 cell.detailTextLabel.text=@"你好!”;//注释

 cell.textLabel.text=self.data[indexPath.row];

 return cell; //返回类型是UITableViewCell *


 }
 -(NSInteger) tableView:(UITableView *)tableView    indentationLevelForRowAtIndexPath:(NSIndexPath *)indexPath//首行缩进函数
{
    return indexPath.row;//返回几就缩进几格
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath//按下条目后的事件
{
    detailViewController *controller=self.controller[indexPath.row];
    [self.navigationController pushViewController:controller animated:YES];

}

// 在代码中使用自己的table cell
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{
static NSString *CellIdentifier = @“cellid"; 
UISpecialTableViewCell *cell = (UISpecialTableViewCell*)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) 
{
cell= (UITwitterTableViewCell *)[[[NSBundle mainBundle] loadNibNamed:@“nib name" owner:self options:nil] lastObject];
}
// 自己的一些设置

return (UITableViewCell *)cell;
}
————————————————————————————————————————————————————————————————————
//设置cell高度
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.section==0||indexPath.section==1||indexPath.section==2)
    {
        return 64;
    }
    else return 80;

}
————————————————————————————————————————————————————————————————————
//Nav

//详细按钮

-(void )tableView:(UITableView *)tableView accessoryButtonTappedForRowWithIndexPath:(NSIndexPath *)indexPath
{
    self.detailController.title=@"Disclosure Button Pressed";
    NSString *selectedMovie =self.movies[indexPath.row];
    NSString *detailMessage=[[NSString alloc] initWithFormat:@"this is details for %@",selectedMovie];
    self.detailController.message=detailMessage;
    self.detailController.title=selectedMovie;
    [self.navigationController pushViewController:self.detailController animated:YES];
}



//设置Section的Footer 
-(NSString *)tableView:(UITableView *)tableView titleForFooterInSection:(NSInteger)section{ 
    NSString *result = nil; 
    if ([tableView isEqual:myTableView]&§ion==0) { 
        result = @"Section 0 Header"; 
    } 
    return result; 
} 

//点击后自动擦出cell痕迹
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{

    //消除cell选择痕迹
    [self performSelector:@selector(deselect) withObject:nil afterDelay:0.5f];
}
- (void)deselect
{
    [self.tableView deselectRowAtIndexPath:[self.tableView indexPathForSelectedRow] animated:YES];
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值