ios:自定义表格单元格中的按钮在滚动时不响应

标签 ios button cell tablecell

抱歉我的英语不好。我想在我的自定义表格单元格中添加一些可点击的按钮并且它工作正常但是当我向下滚动表格时我遇到了问题。滚动后,上部单元格中的按钮在单击时没有任何反应。但是当我滚动回单元格的原始位置时,按钮再次有响应。

我把这个

[cell.btn addTarget:self action:@selector(btnPressed:) forControlEvents:UIControlEventTouchUpInside];

cellForRowAtIndexPath方法; 和 btnPressed在我向下滚动之前已被触发。但是btnPressed滚动到该单元格原始位置的边界后无法触发。

希望有人能理解我的问题谢谢。 请告诉我如何使按钮响应甚至表格滚动?提前致谢


方法一

当我在 cellForRowAtIndexPath 中尝试 addtarget 方法时编码是这样的

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

            static NSString *cellIdentifier = @"CustomTableViewCell";
            CustomTableViewCell *cell = (CustomTableViewCell *)[tableView dequeueReusableCellWithIdentifier:cellIdentifier];
            if (cell == nil) {
                NSArray *nib = [[NSBundle mainBundle] loadNibNamed:cellIdentifier owner:self options:nil];
                cell = [nib objectAtIndex:0];
            }
            [cell.btn addTarget:self action:@selector(btnPressed:) 
            return cell;
}

方法二

我还尝试了另一种不在 cellForRowAtIndexPath 中添加目标的方法

这是我的委托(delegate)代码

在 customcell.h 中,我添加了这个

@protocol CustomCellDelegate <NSObject>
- (void)didClickOnbtn;
@end

在 customcell.m 中,我添加了这个

@interface CustomTableViewCell()
@property (nonatomic, strong) NSDictionary *currentDict;
@property (nonatomic,assign) id<CustomCellDelegate>delegate;
@end

- (IBAction)didClickOnbtn:(id)sender {
    if (self.delegate && [self.delegate respondsToSelector:@selector(didClickOnbtn)]) {
        [self.delegate didClickOnbtn];
    }
}

并且我已经将自定义单元格委托(delegate)设置为 tableview

在 tabelview Controller 中我也有方法 didClickOnbtn<CustomCellDelegate>

按钮可以触发didClickOnbtn在滚动表格之前,表格 View Controller 响应中的方法。


更新: 我在表格中制作了一个节标题 View 。我发现当我删除节标题 View 时,问题就解决了。虽然我不知道为什么节标题 View 会使按钮禁用,但它现在运行良好。希望这可以帮助和我有同样情况的人。

附注这两种方法都有效。

最佳答案

我遇到了同样的问题。我使用变通方法使用 Touch Down 而不是 Touch Up Inside。这不是一个完美的解决方案,而是一种解决方法,因为即使在滚动未完成时也会调用 Touch Down 的 IBAction。

关于ios:自定义表格单元格中的按钮在滚动时不响应,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28272937/

相关文章:

Excel Vba从2个单元格值创建数组,步长为1

iOS:PJSIP 后台 30-40 秒后没有来电

react-native - 在 React Native 中向 onPress 函数添加多个事件

android - 以编程方式更改按钮背景可绘制onClick

java - Android - 如何以编程方式在 LinearLayout 中添加多个具有不同布局边距的按钮?

arrays - 字符串元胞数组到 matlab 结构

jquery - 如何删除该表中的所有单元格?

ios - Swift:从 View 中获取值(value)的最佳方式

ios facebook sdk v3.11 共享对话框发布按钮已禁用

ios - Swift - 连续背景动画循环