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