gpt4 book ai didi

ios将自定义属性添加到uitableviewcell按钮

转载 作者:行者123 更新时间:2023-12-01 18:45:54 26 4
gpt4 key购买 nike

我在单个 View 中有 2 个表。 table_1table_2带有自定义单元格

customCell *cell = (customCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];

有两个按钮编辑和删除。
并为 editBtn 添加了目标方法:
[cell.btnEdit addTarget:self action:@selector(editBtnClick:) forControlEvents:UIControlEventTouchUpInside];

当我点击编辑按钮时,我想知道它是哪个表格的单元格按钮。为此,我想为编辑按钮提供像“tabName”这样的自定义属性。

有没有办法做到这一点?

最佳答案

尝试使用具有关联引用的类别。它更干净,适用于 UIButton 的所有实例。

UIButton+属性.h

#import <Foundation/Foundation.h>

@interface UIButton(Property)

@property (nonatomic, retain) NSObject *property;

@end

UIButton+Property.m
#import "UIButton+Property.h"
#import <objc/runtime.h>

@implementation UIButton(Property)

static char UIB_PROPERTY_KEY;

@dynamic property;

-(void)setProperty:(NSObject *)property
{
objc_setAssociatedObject(self, &UIB_PROPERTY_KEY, property,
OBJC_ASSOCIATION_RETAIN_NONATOMIC);
}

-(NSObject*)property
{
return (NSObject*)objc_getAssociatedObject(self, &UIB_PROPERTY_KEY);
}

@end

/示例用法
#import "UIButton+Property.h"

...
UIButton *button1 = [UIButton     
buttonWithType:UIButtonTypeRoundedRect];
button1.property = @"HELLO";
NSLog(@"Property %@", button1.property);
button1.property = nil;
NSLog(@"Property %@", button1.property);

关于ios将自定义属性添加到uitableviewcell按钮,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35937012/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com