Eri*_*ric 68 iphone objective-c uitableview ios ios7
我看过很多关于类似事情的帖子,但没有一个匹配或解决这个问题.从iOS 7开始,每当我添加UIButton到一个UITableViewCell或甚至是一个footerview它都工作"很好",这意味着它接收目标动作,但它没有显示通常在点击时发生的小亮点UIButton.它使UI看起来很时髦而不显示按钮对触摸的反应.
我很确定这是iOS7中的一个错误,但是有人找到了解决方案或者可以帮我找到一个:)
编辑:我忘了提到它会突出显示我是否长按按钮,但如果只是添加到标准视图就不会像它那样快速点击.
码:
创建按钮:
UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
button.titleLabel.font = [UIFont systemFontOfSize:14];
button.titleLabel.textColor = [UIColor blueColor];
[button setTitle:@"Testing" forState:UIControlStateNormal];
[button addTarget:self action:@selector(buttonPressed:) forControlEvents: UIControlEventTouchDown];
button.frame = CGRectMake(0, 0, self.view.frame.size.width/2, 40);
Run Code Online (Sandbox Code Playgroud)
我测试的东西:
//删除手势识别器UITableView,以防他们挡路.
for (UIGestureRecognizer *recognizer in self.tableView.gestureRecognizers) {
recognizer.enabled = NO;
}
Run Code Online (Sandbox Code Playgroud)
//从Cell中删除手势
for (UIGestureRecognizer *recognizer in self.contentView.gestureRecognizers) {
recognizer.enabled = NO;
}
Run Code Online (Sandbox Code Playgroud)
//这显示了轻微的触摸,但这不是理想的外观
button.showsTouchWhenHighlighted = YES;
Run Code Online (Sandbox Code Playgroud)
小智 87
在该tableview中,您只需添加此属性即可.
tableview.delaysContentTouches = NO;
Run Code Online (Sandbox Code Playgroud)
并在启动单元格后添加cellForRowAtIndexPath,只需添加以下代码即可.在iOS 6和iOS 7中,单元格的结构明显不同
.iOS 7我们在UITableViewCell和内容View之间有一个控件UITableViewCellScrollView.
for (id obj in cell.subviews)
{
if ([NSStringFromClass([obj class]) isEqualToString:@"UITableViewCellScrollView"])
{
UIScrollView *scroll = (UIScrollView *) obj;
scroll.delaysContentTouches = NO;
break;
}
}
Run Code Online (Sandbox Code Playgroud)
Rom*_* B. 40
从iOS 8开始,我们需要将相同的技术应用于UITableView子视图(表包含隐藏的UITableViewWrapperView滚动视图).不再需要迭代UITableViewCell子视图.
for (UIView *currentView in tableView.subviews) {
if ([currentView isKindOfClass:[UIScrollView class]]) {
((UIScrollView *)currentView).delaysContentTouches = NO;
break;
}
}
Run Code Online (Sandbox Code Playgroud)
这个答案应与这个问题联系起来.
Eri*_*ric 27
我试图将此添加到已接受的答案中,但它从未经历过.这是关闭单元delayedContentTouches属性的一种更安全的方法,因为它不查找特定的类,而是查找响应选择器的任何内容.
在细胞中:
for (id obj in self.subviews) {
if ([obj respondsToSelector:@selector(setDelaysContentTouches:)]) {
[obj setDelaysContentTouches:NO];
}
}
Run Code Online (Sandbox Code Playgroud)
在TableView中:
self.tableView.delaysContentTouches = NO;
Run Code Online (Sandbox Code Playgroud)
Que*_*tin 18
对于适用于iOS7和iOS8的解决方案,请创建自定义UITableView子类和自定义UITableViewCell子类.
使用该样品UITableView的initWithFrame:
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self)
{
// iterate over all the UITableView's subviews
for (id view in self.subviews)
{
// looking for a UITableViewWrapperView
if ([NSStringFromClass([view class]) isEqualToString:@"UITableViewWrapperView"])
{
// this test is necessary for safety and because a "UITableViewWrapperView" is NOT a UIScrollView in iOS7
if([view isKindOfClass:[UIScrollView class]])
{
// turn OFF delaysContentTouches in the hidden subview
UIScrollView *scroll = (UIScrollView *) view;
scroll.delaysContentTouches = NO;
}
break;
}
}
}
return self;
}
Run Code Online (Sandbox Code Playgroud)
使用该样品UITableViewCell的initWithStyle:reuseIdentifier:
- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
if (self)
{
// iterate over all the UITableViewCell's subviews
for (id view in self.subviews)
{
// looking for a UITableViewCellScrollView
if ([NSStringFromClass([view class]) isEqualToString:@"UITableViewCellScrollView"])
{
// this test is here for safety only, also there is no UITableViewCellScrollView in iOS8
if([view isKindOfClass:[UIScrollView class]])
{
// turn OFF delaysContentTouches in the hidden subview
UIScrollView *scroll = (UIScrollView *) view;
scroll.delaysContentTouches = NO;
}
break;
}
}
}
return self;
}
Run Code Online (Sandbox Code Playgroud)
Rap*_*nto 16
我用来解决问题的方法是使用以下代码的UIButton类别:
- (void) touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
[super touchesBegan:touches withEvent:event];
[NSOperationQueue.mainQueue addOperationWithBlock:^{ self.highlighted = YES; }];
}
- (void) touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event
{
[super touchesCancelled:touches withEvent:event];
[self performSelector:@selector(setDefault) withObject:nil afterDelay:0.1];
}
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
[super touchesEnded:touches withEvent:event];
[self performSelector:@selector(setDefault) withObject:nil afterDelay:0.1];
}
- (void)setDefault
{
[NSOperationQueue.mainQueue addOperationWithBlock:^{ self.highlighted = NO; }];
}
Run Code Online (Sandbox Code Playgroud)
当我在UITableViewCell中按下按钮时,按钮会正确反应,而我的UITableView在delaysContentTouches没有强制的情况下表现正常.
bra*_*ipt 10
这是罗曼B在Swift 2中的答案:
for view in tableView.subviews {
if view is UIScrollView {
(view as? UIScrollView)!.delaysContentTouches = false
break
}
}
Run Code Online (Sandbox Code Playgroud)
- (void)viewDidLoad
{
[super viewDidLoad];
for (id view in self.tableView.subviews)
{
// looking for a UITableViewWrapperView
if ([NSStringFromClass([view class]) isEqualToString:@"UITableViewWrapperView"])
{
// this test is necessary for safety and because a "UITableViewWrapperView" is NOT a UIScrollView in iOS7
if([view isKindOfClass:[UIScrollView class]])
{
// turn OFF delaysContentTouches in the hidden subview
UIScrollView *scroll = (UIScrollView *) view;
scroll.delaysContentTouches = NO;
}
break;
}
}
}
Run Code Online (Sandbox Code Playgroud)

我在 UITableViewCell 中的纯文本 UIButton 遇到了类似的问题,没有在触摸时突出显示。对我来说修复它的是将 buttonType 从自定义更改回系统。
将 delaysContentTouches 设置为 NO 对同一 UITableViewCell 中的纯图像 UIButton 起到了作用。
self.tableView.delaysContentTouches = NO;
Run Code Online (Sandbox Code Playgroud)