TableView基础篇

第一,初始化
       可以使用懒加载进行初始化;(懒加载就是利用get方法进行初始化)

//界面布局

    UITableView *tableView_list = [[UITableView allocinitWithFrame:CGRectMake(00XSCREENWIDTHXSCREENHEIGHT)];

    tableView_list.backgroundColor = [UIColor clearColor];

    tableView_list.dataSource = self;

    tableView_list.delegate = self;

    tableView_list.separatorStyle = UITableViewCellSeparatorStyleNone;

    [self.view addSubview:tableView_list];


#pragma mark - UITableViewDataSource
- ( NSInteger )numberOfSectionsInTableView:( UITableView  *)tableView {
     return  1 ;
}

- ( NSInteger )tableView:( UITableView  *)tableView numberOfRowsInSection:( NSInteger )section {
     return   10 ;
}

- ( UITableViewCell  *)tableView:( UITableView  *)tableView cellForRowAtIndexPath:( NSIndexPath  *)indexPath {
   static   NSString  *identify =  @"Identify" ;
   UITableViewCell *cell =  [tableView  dequeueReusableCellWithIdentifier :identify];
   if  (cell ==  nil ) {
       cell = [[ UITableViewCell  alloc initWithStyle : UITableViewCellStyleDefault   reuseIdentifier :identify];
    }
     return  cell;
}

#pragma mark - UITableViewDelegate
- ( CGFloat )tableView:( UITableView  *)tableView heightForRowAtIndexPath:( NSIndexPath  *)indexPath {
   return   50 ;
}

//列表头部
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
     return 30;
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    UIView *backView = [[UIView allocinitWithFrame:CGRectMake(00tableView_list.width30)];
     backView. backgroundColor  =  ColorForViewBg ;
    return  backView;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
     [tableView  deselectRowAtIndexPath :indexPath  animated : YES ];
}


第二,tableView索引的设置

索引显示设置
tableView_list. sectionIndexColor = [ UIColor blueColor]; //字体颜色
tableView_list . sectionIndexBackgroundColor  = [ UIColor   clearColor ];//索引背景颜色

以下两句是索引的关键
//返回索引栏数据
- (NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView {
     return   array_section ;
}

// 建立索引栏和 section 的关联
- ( NSInteger )tableView:( UITableView  *)tableView sectionForSectionIndexTitle:( NSString  *)title atIndex:( NSInteger )index {
     return  [ array_section   indexOfObject :title];
}


第三,滑动删除

首先要设置cell可编辑
- ( BOOL )tableView:( UITableView  *)tableView canEditRowAtIndexPath:( NSIndexPath  *)indexPath {
     return   YES ;
}

显示的字
- ( NSString  *)tableView:( UITableView  *)tableView titleForDeleteConfirmationButtonForRowAtIndexPath:( NSIndexPath  *)indexPath {
     return   @" 删除 " ;
}

删除对应的操作
- ( void )tableView:( UITableView  *)tableView commitEditingStyle:( UITableViewCellEditingStyle )editingStyle forRowAtIndexPath:( NSIndexPath  *)indexPath {
   if  (editingStyle ==  UITableViewCellEditingStyleDelete ) {
       //[self deleteAction_myCar:indexPath.row];
   }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值