外部获取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];
}
}

原文地址:https://www.cnblogs.com/tangyuanby2/p/10236634.html

时间: 2024-10-18 16:41:46

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

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

1.Map<String,Object> map =  ActionContext.getContext().getSession(); 2.HttpSession session = ServletActionContext.getRequest().getSession(); 3.让Action实现SessionAware接口,并实现public void setSession(Map<String, Object> session) {} 方法,Struts2会在实例化Act

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

在AngularJS中,可以从$rootScope中获取数据源,也可以把获取数据的逻辑封装在service中,然后注入到app.run函数中,或者注入到controller中.本篇就来整理获取数据的几种方式. ■ 数据源放在$rootScope中 var app = angular.module("app",[]); app.run(function($rootScope){ $rootScope.todos = [ {item:"",done:true}, {it

java中获取路径的几种方式

总是忘记, 备份一下,方便下次用. 第一种: File directory = new File("");//参数为空 String courseFile = directory.getCanonicalPath() ;System.out.println(courseFile); 结果:C:\Documents and Settings\Administrator\workspace\projectName获取当前类的所在工程路径; 第二种: File f = new File(th

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

原文链接: maven 引入外部jar包的几种方式 方式1:dependency 本地jar包 <dependency> <groupId>com.hope.cloud</groupId> <!--自定义--> <artifactId>cloud</artifactId> <!--自定义--> <version>1.0</version> <!--自定义--> <scope>

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

java动态获取WebService的两种方式(复杂参数类型) 第一种: @Override public OrderSearchListRes searchOrderList(Order_FlightOrderSearchRequest request) { Object myAllMessage; OrderSearchListRes response = null; try { String endpoint = carGlobalSetting.getEndpoint(); JaxWsD

点击li获取下标的几种方式(原生JS实现)

<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <meta http-equiv="X-UA-Compatible" con

Volley的Get、Post方式(JsonObjectRequest、StringRequest)以及Volley获取图片的3种方式

activity_main.xml 里面什么也没有 AndroidManifest.xml(重点是android:name="com.example.volley.MyApplication") <?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android"

Spring在代码中获取bean的几种方式(转:http://www.dexcoder.com/selfly/article/326)

方法一:在初始化时保存ApplicationContext对象 方法二:通过Spring提供的utils类获取ApplicationContext对象 方法三:继承自抽象类ApplicationObjectSupport 方法四:继承自抽象类WebApplicationObjectSupport 方法五:实现接口ApplicationContextAware 方法六:通过Spring提供的ContextLoader 获取spring中bean的方式总结: 方法一:在初始化时保存Applicati

struts2的action从request获取参数值的几种方式

使用jquery框架的ajax能够方便的向后台传递参数,以$.post为例,参数有2种方式字符串和键值对: $.post(url, "name=aty&age=25")和$.post(url, {"name":"aty","age":25}).显然,使用json的方式比使用字符串拼接要方 便.对于后台的action来说,这2种方式是等价的,而且jquery也提供了param.serialize.serialize能够实