searched already some possible fixes but all did not solve mine.
i keep clicking the cell in the uitableview rather than the buttons inside it.
here is my code:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath: (NSIndexPath *)indexPath{ static NSString *CellIdentifier = @"Cell"; UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; if (cell == nil) { cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier]; } cell.backgroundColor = [UIColor blackColor]; UIView *v = nil; NSArray *array = [cell subviews]; for (v in array) { NSLog(@"%@",[v class]); if( [v isKindOfClass:[UIView class]] ){ [v removeFromSuperview]; } } //tb if (tableView == self.tb) { v = [[UIView alloc] initWithFrame: CGRectMake(0, 0, self.tb.bounds.size.width, 120.0)]; if([feeds count]>0){ UIImageView *box=[[UIImageView alloc] initWithImage:[UIImage imageNamed:@"statusBox.png"]]; box.userInteractionEnabled = YES; [v addSubview:box]; AsyncImageView *imageView1 = [[AsyncImageView alloc] initWithFrame:CGRectMake(10, 20.0f, 34.0f, 34.0f)]; imageView1.contentMode = UIViewContentModeScaleAspectFill; imageView1.clipsToBounds = YES; //cancel loading previous image for cell [[AsyncImageLoader sharedLoader] cancelLoadingImagesForTarget:imageView1]; if (users1 != nil && users1.imagelink != nil && (id) users1.imagelink != [NSNull null]){ imageView1.imageURL = [NSURL URLWithString:users1.imagelink]; } else{ imageView1.image=[UIImage imageNamed:@"default_ProfilePic.png"]; } UITapGestureRecognizer *tapped = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(myFunction:)]; tapped.numberOfTapsRequired = 1; [imageView1 addGestureRecognizer:tapped]; [tapped release]; imageView1.userInteractionEnabled = NO; [v addSubview:imageView1]; UIFont *font = [UIFont fontWithName:@"TrebuchetMS-Bold" size:11]; UILabel *descLabel1 = [[UILabel alloc] initWithFrame: CGRectMake(52, 23, 160, 48)]; descLabel1.text = [NSString stringWithFormat:@"%@ %@",users1.userfirstn,users1.userlastn]; descLabel1.textColor = [UIColor blackColor]; descLabel1.font = font; descLabel1.adjustsFontSizeToFitWidth=YES; descLabel1.numberOfLines = 0; CGSize expectedLabelSize = [descLabel1.text sizeWithFont:descLabel1.font constrainedToSize:descLabel1.frame.size lineBreakMode:UILineBreakModeWordWrap]; CGRect newFrame = descLabel1.frame; newFrame.size.height = expectedLabelSize.height; descLabel1.frame = newFrame; descLabel1.numberOfLines = 0; descLabel1.userInteractionEnabled = NO; [v addSubview:descLabel1]; UILabel *descLabel2= [[UILabel alloc] initWithFrame: CGRectMake(52, 43, 200, 48)]; StatusClass *stat1=[feeds objectAtIndex:indexPath.row]; descLabel2.text = [stat1 statcreate]; descLabel2.textColor = [UIColor blackColor]; descLabel2.font = font; descLabel2.adjustsFontSizeToFitWidth=YES; descLabel2.numberOfLines = 0; CGSize expectedLabelSize2 = [descLabel2.text sizeWithFont:descLabel2.font constrainedToSize:descLabel2.frame.size lineBreakMode:UILineBreakModeWordWrap]; CGRect newFrame2 = descLabel2.frame; newFrame2.size.height = expectedLabelSize2.height; descLabel2.frame = newFrame2; descLabel2.numberOfLines = 0; descLabel2.userInteractionEnabled = NO; [v addSubview:descLabel2]; UILabel *descLabel3= [[UILabel alloc] initWithFrame: CGRectMake(10, 63, 280, 80)]; StatusClass *stat=[feeds objectAtIndex:indexPath.row]; descLabel3.text = [stat stattext]; descLabel3.textColor = [UIColor blackColor]; descLabel3.font = font; descLabel3.adjustsFontSizeToFitWidth=YES; descLabel3.numberOfLines = 0; descLabel3.userInteractionEnabled = NO; [v addSubview:descLabel3]; //comment button UIButton *buttonC = [UIButton buttonWithType:UIButtonTypeRoundedRect]; [buttonC addTarget:self action:@selector(sendComment:) forControlEvents:UIControlEventTouchUpInside]; [buttonC setImage:[UIImage imageNamed:@"comment.png"] forState:UIControlStateNormal]; buttonC.frame = CGRectMake(2, 160, 145, 35); buttonC.userInteractionEnabled = YES; [v addSubview:buttonC]; //share button UIButton *buttonS = [UIButton buttonWithType:UIButtonTypeRoundedRect]; buttonS.tag = indexPath.row; [buttonS addTarget:self action:@selector(sendShare:) forControlEvents:UIControlEventTouchUpInside]; [buttonS setImage:[UIImage imageNamed:@"share.png"] forState:UIControlStateNormal]; buttonS.frame = CGRectMake(150, 160, 140, 35); buttonS.userInteractionEnabled = YES; [v addSubview:buttonS]; } v.userInteractionEnabled = YES; [cell addSubview:v]; } return cell; }
I also tried the UITapGestureRecognizer
for the buttons and still not working.
Thanks.
Interacting with a UIButton. To handle the interaction with a button, you can create the IBAction in your view controller as per normal (eg. drag the button action selector to your view controller). The trick is to get the superview of the button, which will be the cell, and then using tableView.indexPathForCell(cell) to get the index path.
But what if you have a UIButton in the cell, and the user tap on the button? Or it could be any UIView that can be tapped on in the cell. In those cases, the buttons or views themselves will handle the touches, and tableView:didSelectRowAtIndexPath: will NOT be called.
The trick is to get the superview of the button, which will be the cell, and then using tableView.indexPathForCell (cell) to get the index path. @IBAction func tapOnButton(sender: UIButton) { let cell = sender.superview as! UITableViewCell let indexPath = tableView.indexPathForCell(cell) }
We all know a cell is tapped wehn UITableViewDelegate method - tableView:didSelectRowAtIndexPath: - is called. But what if you have a UIButton in the cell, and the user tap on the button? Or it could be any UIView that can be tapped on in the cell.
I'm just baffled by this issue....
I managed to fix this problem by doing this on one project:
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { let cell = tableView.dequeueReusableCellWithIdentifier("RegCell", forIndexPath: indexPath) as! CustomCell cell.contentView.userInteractionEnabled = false // <<-- the solution return cell }
but the same did not work for a different project. I have no idea why...
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