I have a custom button where the image changes upon a press. This button is in the contentView of a UITableViewCell within a UITableView.
There is a noticeable delay when pressing the button before the image changes. Take the button out of the cell and into a UIView the change of image on press is instant.
How do I remove this delay?
It's worth pointing out that I have tried setting delaysContentTouches on the UITableView but it makes no difference that I can see.
Here is some test code that proves the issue. Create new project and add a UITableView to the storyboard.
#import "JWViewController.h"
@implementation JWViewController
- (void)viewDidLoad
{
[super viewDidLoad];
self.tableView.delegate = self;
self.tableView.dataSource = self;
self.tableView.delaysContentTouches = NO;
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView { return 1; }
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { return 1; }
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath { return return 65.0f; }
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = [[UITableViewCell alloc] init];
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
[button setImage:[UIImage imageNamed:@"apple"] forState:UIControlStateNormal];
[button setImage:[UIImage imageNamed:@"apple_selected"] forState:UIControlStateHighlighted];
button.frame = CGRectMake(0, 0, 65, 65);
[cell.contentView addSubview:button];
return cell;
}
@end
I have searched and there appears to be a number of other posts relating to this but none with a working answer.
This blog post doesn't appear to work either in this simple case.
Finally got solution of your issue.
Try this, it will work fine. Add steps in your existing code:- 1.) Set tag of the button which is in content view. 2.) Add target on that button.
- (void)viewDidLoad
{
[super viewDidLoad];
self.tableView.delegate = self;
self.tableView.dataSource = self;
self.tableView.delaysContentTouches = NO;
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView { return 1; }
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { return 1; }
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath { return 65.0f; }
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = [[UITableViewCell alloc] init];
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
[button setImage:[UIImage imageNamed:@"apple.jpg"] forState:UIControlStateNormal];
[button setImage:[UIImage imageNamed:@"apple_selected.png"] forState:UIControlStateHighlighted];
button.frame = CGRectMake(0, 0, 65, 65);
button.tag = indexPath.row;
[button addTarget:self action:@selector(buttonPressed:) forControlEvents:UIControlEventAllTouchEvents];
[cell.contentView addSubview:button];
return cell;
}
- (void)buttonPressed:(UIButton *)sender
{
UITableViewCell *cell = [self.tableView cellForRowAtIndexPath:[NSIndexPath indexPathForRow:sender.tag inSection:0]];
for (UIView *currentView in cell.subviews)
{
if ([NSStringFromClass([currentView class]) isEqualToString:@"UITableViewCellScrollView"])
{
UIScrollView *svTemp = (UIScrollView *) currentView;
[svTemp setDelaysContentTouches:NO];
break;
}
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With