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. python 集合取最大最小值

    #Create a set seta = , , , , , ]) #Find maximum value print(max(seta)) #Find minimum value print(min ...

  2. Java-Java面向对象程序设计

    2017-10-09 17:23:52 在面向对象技术中,将客观世界中的一个事物作为一个对象来考虑,比如有个张先生,他就是一个对象.每个对象都有自己的属性和行为.张先生的属性根据需要有姓名.性别.身高 ...

  3. android之视频播放系统VideoView和自定义VideoView控件的应用

    Android播放视频,包含系统自带VideoView控件,和自定义VideoView控件,可全屏播放,案例包含了本地视频和网络视频. 1:自定义VideoView控件 2:布局代码 3:Activi ...

  4. JVM自定义类加载器加载指定classPath下的所有class及jar

    一.JVM中的类加载器类型 从Java虚拟机的角度讲,只有两种不同的类加载器:启动类加载器和其他类加载器. 1.启动类加载器(Boostrap ClassLoader):这个是由c++实现的,主要负责 ...

  5. C# 中的时间(DataTime)

    在做报表或查询的时候,常常会预设一些可选的日期范围,如本周.本月.本年等,利用 C# 内置的DateTime基本上都可以实现这些功能. 当前时间: DateTime dt = DateTime.Now ...

  6. 【LeetCode】Unique Binary Search Trees II 异构二叉查找树II

    本文为大便一箩筐的原创内容,转载请注明出处,谢谢:http://www.cnblogs.com/dbylk/p/4048209.html 原题: Given n, generate all struc ...

  7. pos提交提交数据时碰到Django csrf

    我的github(PS:希望star):https://github.com/thWinterSun/v-admin 最近在用Vue写前端代码,再用vue-resource向后台提交数据.项目后台是用 ...

  8. ubuntu下安装go语言;sublime+gocode搭建;go的卸载和环境变量配个人.bashrc;2空位3个网

    https://blog.csdn.net/needkane/article/details/36891949 https://www.jianshu.com/p/4f79ae4f081c http: ...

  9. zoj3888

    题解: 维护比这个大的第二大 代码: #include<cstdio> #include<algorithm> #include<queue> #include&l ...

  10. Python Django 前后端数据交互 之 后端向前端发送数据

    Django 前后台的数据传递 严正声明:作者:psklf出处: http://www.cnblogs.com/psklf/archive/2016/05/30/5542612.html欢迎转载,但未 ...