美文网首页洋洋洒洒又一年iOS开发之技能点实用轮子
外部获取IndexPath的几种方式(关联对象等)

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

作者: 仁伯 | 来源:发表于2016-05-22 09:57 被阅读5380次

最近做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];

相关文章

网友评论

  • 齐刘海姑娘:哈喽~您的文章已收录专题【我不是程序猿,请叫我攻城狮】http://www.jianshu.com/collection/db91065b98c6,欢迎关注投稿哦~ :kissing_heart: :heart: :heart: :smile:
  • vernepung:首先,并不确定你的需求,但感觉reloadData吃内存可能是写法问题,还有个问题可能存在每一个Cell一个计时器上(不确定你的说法和写法是否是我理解的方式!纯属猜测哈
    仁伯:@vernepung 恩,对的,如果不使用cell重用,cell中子视图过多的话,肯定会出问题的。
    vernepung:@仁伯安 感觉这样的话,应该多切几次页面也会崩溃,或者多点数据也一样会爆...
    仁伯:@vernepung 没有,就用一个计时器,网络请求返回数据后开启的计时器,当然也可能我重用用的不到位。
  • 欧阳铨:自定义一个cell,然后写一个set方法可以吗😊
    仁伯:@欧阳铨 可以的,不过这样也需要获取indexPath
  • Colin_狂奔的蚂蚁:第二种不建议使用
    仁伯:@狂奔蚂蚁 愿闻其详
    Colin_狂奔的蚂蚁:@狂奔蚂蚁 cellforrow 处理的事情太多,不要主动调cellforrow方法,想获取indexpath 方法有的是,而且不止两种
    仁伯:@狂奔蚂蚁 修改所有的cell的特定子视图,必须要用的,比如,每个cell中都有一个倒计时,使用计时器更新视图,第二种方式是不二之选,reloadData方式吃内存较多。

本文标题:外部获取IndexPath的几种方式(关联对象等)

本文链接:https://www.haomeiwen.com/subject/frmorttx.html