gpt4 book ai didi

ios - 检测 UITableViewCell 是否为重用单元格

转载 作者:行者123 更新时间:2023-12-01 20:23:30 26 4
gpt4 key购买 nike

在我的应用程序中,我有带有多个按钮的 UITableView 单元格。每个单元格 4 个 UIButton。对于每个 UIButton,我想添加一个 UILongPressGestureRecognizer。下面的代码:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

FriendViewCell *cell = (FriendViewCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];


for(int i =0; i<4; i++) {




UIButton *button = cell.buttons[i];
UILabel *label = cell.labels[i];

[button setTag:(int)indexPath.row*4+i];
[button addTarget:self action:@selector(friendTapped:) forControlEvents:UIControlEventTouchUpInside];

UILongPressGestureRecognizer *longPress = [[UILongPressGestureRecognizer alloc]initWithTarget:self action:@selector(longPressFriend:)];

[button addGestureRecognizer:longPress];

}

}

我刚刚意识到,如果重复使用一个单元格,那么我会在每个按钮上多次添加一个手势。有没有办法检测创建的​​单元格是重复使用的还是新的?我不认为我可以将代码移动到 FriendViewCell 类,因为我的手势目标friendTapped:在我的 UITableViewController 中。任何指针将不胜感激!谢谢

最佳答案

更简洁的方法是为您的 Button 创建一个自定义类。 .此类将有 UILongPressGestureRecognizer在初始化时创建,并在触发手势时调用委托(delegate)(您的 Controller )。

.h

@class MyLongPressedButton
@protocol MyLongPressedButtonDelegate
- (void)buttonIsLongPressed:(MyLongPressedButton *)button;
@end

@interface MyLongPressedButton
@property (nonatomic, weak) id<MyLongPressedButtonDelegate > delegate
@end

.m
-(id)init {
if (self = [super init]) {
UILongPressGestureRecognizer *longPress = [[UILongPressGestureRecognizer alloc]initWithTarget:self action:@selector(longPress:)];
[self addGestureRecognizer:longPress];

}
return self;
}

-(void)longPress:(id)sender {
[_delegate buttonIsLongPressed:self]
}

关于ios - 检测 UITableViewCell 是否为重用单元格,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28586342/

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