UITableView的使用

1、UITableView继承自UIScrollView

2、UITableView是显示表格数据的UI控件:只能显示单列多行数据

3、UITableView需要设置数据源代理,才能知道显示什么数据。
设置数据源代理的方法:遵守代理协议UITableViewDataSource,设置代理:tableView.dataSource = self;

实现三个代理方法:

//组数
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView;  
//每一组行数
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section;
//每一个单元格
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath;

4、注意点:
在iOS6.0之前,设置每一个单元格采用以下方式:

// 从缓存池中 “出队” 一个单元格
HMTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellID];
        
// 判断是否成功 “出队”(取出) 单元格
if (cell == nil) {
            
    // 如果缓存池中没有可用单元格,则自己创建单元格
    cell = [[HMTableViewCell alloc] initWithStyle:UITableViewCellStyleValue2 reuseIdentifier:cellID];
}

同时要重写cell 的initWithStyle方法:

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
    self = [super initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:reuseIdentifier];
    if (self) {
    }
    return self;
}

在iOS6.0之后:

// 1、为tableView 注册单元格,使tableView自动创建单元格的时候,知道要创建什么类型的单元格
[tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:cellID];

// 2、从缓存池中去cell
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellID forIndexPath:indexPath];

此方法中可以以下方法,设置单元格样式:

cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;



常用方法举例:

// -------------------------------------  单元格 编辑功能 实现:edit  ---------------------------------------------

/**
 * 询问单元格是否可以编辑【必须要实现】
 *
 * @param tableView
 * @param indexPath 要编辑的单元格indexPath
 *
 * @return 是否可以编辑
 */
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath{
    return YES;
}


// -----------------  当只需要显示“delete”按钮时 ----------------------
/**
 * 当只需要显示默认的删除按钮时,实现这个代理方法,按钮为”delete“,点击”delete“按钮,会调用这个代理方法【必须要实现】
 *
 * @param tableView
 * @param editingStyle 编辑样式
 * @param indexPath    被编辑单元格的indexPath
 */
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath{
    NSLog(@"%zd",indexPath.row);
}


/**
 * 当要修改默认“delete”按钮的字符时,实现这个代理方法,返回要显示的文字
 *
 * @param tableView
 * @param indexPath 被编辑单元格的indexPath
 *
 * @return 返回按钮文字
 */
- (NSString *)tableView:(UITableView *)tableView titleForDeleteConfirmationButtonForRowAtIndexPath:(NSIndexPath *)indexPath{
    return @"删除";
}

// -----------------  当需要显示多个编辑按钮时 ----------------------

/**
 * 当需要显示多个编辑按钮时,实现这个方法,返回操作的集合。重写这个方法实现多个操作,就不需要实现以上两个方法了,以上两个方法是默认“delete”按钮的实现方法
 *
 * @param tableView
 * @param indexPath 被编辑单元格的indexPath
 *
 * @return 操作集合
 */
- (NSArray<UITableViewRowAction *> *)tableView:(UITableView *)tableView editActionsForRowAtIndexPath:(NSIndexPath *)indexPath{
    UITableViewRowAction *action1 = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleDefault title:@"删掉你" handler:^(UITableViewRowAction * _Nonnull action, NSIndexPath * _Nonnull indexPath) {
        
        [self.datas removeObjectAtIndex:indexPath.row];
        [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
        
        
        NSLog(@"删掉你:%zd",indexPath.row);
        [tableView setEditing:NO animated:YES];
    }];
    
    UITableViewRowAction *action2 = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleNormal title:@"取消关注" handler:^(UITableViewRowAction * _Nonnull action, NSIndexPath * _Nonnull indexPath) {
        NSLog(@"忘掉你:%zd",indexPath.row);
//        [tableView setEditing:NO animated:YES];
    }];
    
    return @[action1,action2];
}

// ------------------------------------  单元格 重新排序 : move  ---------------------------------

/**
 * 当移动单元格动作完成的时候调用这个代理方法,通常在这个代理方法中要调整数据顺序【必须实现这个方法才能有排序功能】
 *
 * @param tableView
 * @param sourceIndexPath      被移动的单元格的indexPath
 * @param destinationIndexPath 目标单元格的indexPath
 */
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)sourceIndexPath toIndexPath:(NSIndexPath *)destinationIndexPath{
    NSLog(@"source:%zd  -  destination:%zd",sourceIndexPath.row,destinationIndexPath.row);
    [self.datas exchangeObjectAtIndex:sourceIndexPath.row withObjectAtIndex:destinationIndexPath.row];
    for (NSString *str in self.datas) {
        NSLog(@"%@",str);
    }
}

/**
 * 询问某一行是否能够移动【必须要实现】
 *
 * @param tableView
 * @param indexPath 被移动的单元格的indexPath
 *
 * @return 是否可以移动
 */
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath{
    return YES;
}

/**
 * 在开始移动单元格的时候,会调用这个代理方法,会询问移动的目标indexPath。如果不需要设置目标单元格,则不需要实现这个代理方法
 *
 * @param tableView
 * @param sourceIndexPath              被移动的单元格的indexPath
 * @param proposedDestinationIndexPath 推测目标单元格的indexPath
 *
 * @return 返回目标单元格的indexPath
 */
- (NSIndexPath *)tableView:(UITableView *)tableView targetIndexPathForMoveFromRowAtIndexPath:(NSIndexPath *)sourceIndexPath toProposedIndexPath:(NSIndexPath *)proposedDestinationIndexPath{
    return [NSIndexPath indexPathForRow:3 inSection:0];
}


- (IBAction)clickReorder:(id)sender {
    [self.tableView setEditing:!self.tableView.isEditing animated:YES];
}



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值