iOS开发之tableView(自用贴)

本文详细介绍了如何使用Swift语言结合UITableView实现基础功能,包括数据源管理、代理方法调用、高度设置、cell定制、点击事件处理以及部分进阶特性如分割线去除和滚动效果关闭。此外,还涉及了如何刷新数据、处理section和cell的增删改查等关键操作。

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

//声明:本贴为自用贴,介于本人使用习惯可能不大家的使用习惯不同,不喜勿喷。

 //经常用(这些基本够用除非要加特技)

<UITableViewDataSource,UITableViewDelegate>//两个代理


  UITableView*table=[[UITableView alloc]initWithFrame:CGRectMake(0, 0, 320, 480)];

  table.delegate=self;

  table.dataSource=self;

  [self.view addSubview:table];

 

#pragma mark 代理  //常用的四个代理

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

{

    return 1;//一行section中cell的个数 不写,即默认为1

}


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

{


        return 80;//cell的高度

 

}

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

{

   

  static NSString *cellStr=@"todayCell";//本人习惯用xib

  tadayDataTableViewCell *cell = (tadayDataTableViewCell *)[tableView dequeueReusableCellWithIdentifier:cellStr];


     if(cell ==nil) {

       

         NSArray *nibArray = [[NSBundle mainBundle]loadNibNamed:@"tadayDataTableViewCell" owner:self options:nil];

         cell = (tadayDataTableViewCell *)[nibArray objectAtIndex:0];

     }


          cell.selectionStyle=UITableViewCellSelectionStyleNone;//去除点击效果

        return cell;


    

}

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

{

    NSLog(@"点击了cell");


}

//刷新table  
[table reloadData];


//可能用(小特技)

  table.separatorStyle=UITableViewCellSeparatorStyleNone;//去除cell多余的分割线

  table.scrollEnabled=NO;//table的滚动效果


-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView

{

    return 1;//section的个数  不写,即默认为1

}


//右划删除(系统的,加特技的自己写)

/*

- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {

}

- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {

    

    if (editingStyle == UITableViewCellEditingStyleDelete) {

   

 //删除处理

        

    }

    else if (editingStyle == UITableViewCellEditingStyleInsert) {

        // Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view.

    }

    

}

- (NSString *)tableView:(UITableView *)tableView titleForDeleteConfirmationButtonForRowAtIndexPath:(NSIndexPath *)indexPath{

    return @"删除";

}

*/

   //消除cell选择痕迹

/*

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{


 

   [self performSelector:@selector(deselect) withObject:nil afterDelay:0.5f];

}

- (void)deselect

{

    [self.tableView deselectRowAtIndexPath:[self.tableView indexPathForSelectedRow] animated:NO];

}

*/



//不常用

//section里的文字

- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section{

 return @"";

}

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

{

//section头的高度,可以写一些自己想要的section头(有悬浮效果,当然悬浮效果可以关)

}


-(UIView*)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section

{

//section头的view

}

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

{

//section底的高度

}

-(UIView*)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section

{

//section底的view

}





评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值