外部获取IndexPath的几种方式(关联对象等)

最近做tableview的cell倒计时,原来的方案是每次计时触发重新reloadData,导致内存暴涨。故而找了几种直接修改cell中的具体label控件内容的方法。现总结如下:
所谓NSIndexPath,主要用来标识cell在列表中的坐标位置,其有两个属性:section、row,section是用来标识cell处于第几个section中,row是用来说明cell在该section中的第几行。

一、单击某个cell中的button获取indexPath

1、 一般方式
     - (void)buttonAction:(UIButton *)sender
     {
        UITableViewCell *cell = (UITableViewCell *)[[sender superview] superview]; 
        NSIndexPath *indexPath = [_tableView indexPathForCell:cell];  
        NSLog(@"indexPath is = %i",indexPath.row); 
     }
2、runtime添加属性方式,即关联对象的方式
    //runtime 关联对象
     这种方式首先引入#import <objc/runtime.h>

     - (UITableViewCell *)tableView:(UITableView *)tableVie cellForRowAtIndexPath:(NSIndexPath *)indexPath
     {
         static NSString *identiStr = @"cellID";
         UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identiStr];
         if (cell == nil)
         {
            cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identiStr];
            UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
            button.frame = CGRectMake(0, 0, 100, 33);
    
            [button addTarget:self action:@selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside];
            [button setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
            button.tag = 110 + indexPath.row;
            [cell.contentView addSubview:button];
          }
          UIButton *button = (UIButton *)[cell.contentView viewWithTag:110];
         //runtime 关联对象
         objc_setAssociatedObject(button, @"button", indexPath, OBJC_ASSOCIATION_ASSIGN);
         [button setTitle:dataSource[indexPath.row] forState:UIControlStateNormal];

         return cell;     
     }
    //事件触发 runtime 获取关联的对象
     - (void)buttonAction:(UIButton *)sender
     {
        //runtime 获取关联的对象
        UITableViewCell *cell = objc_getAssociatedObject(sender, @"button");
        NSIndexPath *indexPath = [_tableView indexPathForCell:cell];
        NSLog(@"indexPath is = %ld",indexPath.row);
     }

二、已知具体row,获取indexPath

- (void) refreshLessTime
{
    for (int row = 0; row < leftTimeArr.count; row ++)
    {
          NSIndexPath *indexPath = [NSIndexPath indexPathForItem: row inSection:0];
          UITableViewCell *cell = (UITableViewCell *)[_tableView cellForRowAtIndexPath:indexPath];
          UILabel *remainingTimeLabel = (UILabel *)[[cell.contentView viewWithTag:500] viewWithTag:501];
          remainingTimeLabel.text = [leftTimeArr objectAtIndex:indexPath.row];
     }
}

附送两种tableview刷新方式

1. 刷新特定行row:
NSIndexPath *indexPath = [NSIndexPath indexPathForRow:0 inSection:0];
[self.tableView reloadRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
2. 刷新特定分区section:
NSIndexSet *indexSet = [[NSIndexSet alloc] initWithIndex:0];
[self.tableView reloadSections:indexSet withRowAnimation:UITableViewRowAnimationFade];
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 概述在iOS开发中UITableView可以说是使用最广泛的控件,我们平时使用的软件中到处都可以看到它的影子,类似...
    liudhkk阅读 9,093评论 3 38
  • { 24、Sqlite数据库 1、存储大数据量,增删改查,常见管理系统:Oracle、MSSQLServer、DB...
    CYC666阅读 964评论 0 1
  • 1.badgeVaule气泡提示 2.git终端命令方法> pwd查看全部 >cd>ls >之后桌面找到文件夹内容...
    i得深刻方得S阅读 4,770评论 1 9
  • 古典专栏13-2《理解拖延,掌握和自己谈判的3种艺术 》13-3《拖延症的自我修养,6个停止拖延的好策略》读后反思...
    丹丹自语阅读 402评论 0 0
  • “这个吻,奖励你的,明天继续!”黄先生笑咪咪地对我说。 “可是,没有剩下的油饼了。”我做着鬼脸回应着。 我并不经常...
    夜光罩着你阅读 692评论 7 3