首页 > 其他 > 详细

objc_setAssociatedObject获取cell上button对应所在的行

时间:2015-07-17 11:19:59      阅读:243      评论:0      收藏:0      [点我收藏+]
#import <UIKit/UIKit.h>

@interface TestCell : UITableViewCell
@property (weak, nonatomic) IBOutlet UIButton *btnTest;

@end
#import "ViewController.h"
#import "TestCell.h"
#import <objc/runtime.h>

static void *btnIndexPathKey = "btnIndexPathKey";
@interface ViewController ()<UITableViewDataSource,UITableViewDelegate>

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 5;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    TestCell *test = [tableView dequeueReusableCellWithIdentifier:@"TestCell" forIndexPath:indexPath];
    /**设置cell上button的关联对象*/
    objc_setAssociatedObject(test.btnTest, btnIndexPathKey, indexPath, OBJC_ASSOCIATION_ASSIGN);
    return test;
}
- (IBAction)btnClick:(UIButton *)sender
{
    /**获取对应关联对象的值*/
    NSIndexPath *btnIndexPath = objc_getAssociatedObject(sender, btnIndexPathKey);
    NSLog(@"btnIndexPath.row = %ld",btnIndexPath.row);
    
}

技术分享

objc_setAssociatedObject获取cell上button对应所在的行

原文:http://www.cnblogs.com/thbbsky/p/4653522.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!