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];
}
}

外部获取IndexPath的几种方式(关联对象等)的更多相关文章

  1. django一对一数据库建立和进行数据传输的3种方式all()(对象) values()(字典) values_list()(元组)

    class Business(models.Model): caption = models.CharField(max_length=32) code = models.CharField(max_ ...

  2. 获取Type的三种方式

    using System;using UnityEngine; public class Type_Test : MonoBehaviour{    private void Awake()    { ...

  3. java动态获取WebService的两种方式(复杂参数类型)

    java动态获取WebService的两种方式(复杂参数类型) 第一种: @Override public OrderSearchListRes searchOrderList(Order_Fligh ...

  4. AngularJS中获取数据源的几种方式

    在AngularJS中,可以从$rootScope中获取数据源,也可以把获取数据的逻辑封装在service中,然后注入到app.run函数中,或者注入到controller中.本篇就来整理获取数据的几 ...

  5. maven 引入外部jar包的几种方式(转)

    原文链接: maven 引入外部jar包的几种方式 方式1:dependency 本地jar包 <dependency> <groupId>com.hope.cloud< ...

  6. java 获取时间戳的三种方式

      java 获取时间戳的三种方式 CreationTime--2018年7月13日16点29分 Author:Marydon 1.实现方式 方式一:推荐使用 System.currentTimeMi ...

  7. 【Struts2】Struts2获取session的三种方式

    1.Map<String,Object> map =  ActionContext.getContext().getSession(); 2.HttpSession session = S ...

  8. js获取时间戳的三种方式

      js获取时间戳的三种方式 CreateTime--2018年5月23日08:44:10 Author:Marydon // 方式一:推荐使用 var timestamp=new Date().ge ...

  9. Struts2(四.注册时检查用户名是否存在及Action获取数据的三种方式)

    一.功能 1.用户注册页面 <%@ page language="java" contentType="text/html; charset=UTF-8" ...

随机推荐

  1. 获取客户端真实ip地址(无视代理)

    /// <summary> /// 获取客户端IP地址(无视代理) /// </summary> /// <returns>若失败则返回回送地址</retur ...

  2. python获取文件扩展名的方法

    主要介绍了python获取文件扩展名的方法,涉及Python针对文件路径的相关操作技巧 import os.path def file_extension(path): ] print file_ex ...

  3. Goroutines和Channels(一)

    Go语言中的并发程序可以用两种手段来实现.本章讲解goroutine和channel,其支持“顺序通信进程”(communicating sequential processes)或被简称为CSP.C ...

  4. 运行gulp提示:Task function must be specified

    问题出在gulp版本上,以下是gulp3  VS  gulp4的区别: gulp4最大的变化是不能像以前那样传递一个依赖的任务列表. gulp3中,如果有一个任务A.B和C的列表,你想在一个序列中运行 ...

  5. 棋盘覆盖问题(算法分析)(Java版)

    1.问题描述: 在一个2k×2k个方格组成的棋盘中,若有一个方格与其他方格不同,则称该方格为一特殊方格,且称该棋盘为一个特殊棋盘.显然特殊方格在棋盘上出现的位置有种情形.因而对任何 k≥0,有4k种不 ...

  6. Spring AMQP 源码分析 06 - 手动消息确认

    ### 准备 ## 目标 了解 Spring AMQP 如何手动确认消息已成功消费 ## 前置知识 <Spring AMQP 源码分析 04 - MessageListener> ## 相 ...

  7. Java基础十二--多态是成员的特点

    Java基础十二--多态是成员的特点 一.特点 1,成员变量. 编译和运行都参考等号的左边. 覆盖只发生在函数上,和变量没关系. Fu f = new Zi();System.out.println( ...

  8. Yii 语言设置 中文提示信息

    1.  在main.php配置文件中加入 'language'=>'zh_cn', 注: 在URL中追加参数lang=zh_cn即可实现中文 2.  在Controller方法中添加 publi ...

  9. 解决jenkins运行selenium测试出错的问题

    If you run Jenkins as a service in the background it won't open apps in the foreground. You may eith ...

  10. Dubbo 环境搭建

    CentOS7 x64 JDK1.8 1. wget https://mirrors.tuna.tsinghua.edu.cn/apache/zookeeper/zookeeper-3.4.8/zoo ...