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.
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...
There is another potential cause of problems like this one, and that is UITableViewCells now have a contentView. This can and will be placed in front of other views in your custom table view cells in order to pick up touches and detect cell selection. In doing so, it may block touches to any views behind it. I've especially noticed this annoyance when creating cells using nibs.
The accepted means for dealing with this is to ensure that all subviews are added not to the tableview cell itself, but to its contentView. This is a read-only property that adjusts its own frame under certain circumstances, for example when slid sideways to reveal the delete button, or during editing mode. You should therefore ensure that any items you do add to the contentVieware intelligently sized and have the correct resizing behaviour.
Also be aware that by adding cells to the contentView, you may yourself block touches to it and thus prevent the cell from being selected. I personally try not to allow cell selection in tableviews containing cells with custom user-enabled controls. It only leads to confusion and awkward interfaces. In fact, I'm seriously considering replacing all UITableViews in my future projects with UICollectionViews, which are far more adaptable.
-Ash
I was doing this:
let plusButton: UIButton = {
let v = UIButton()
v.setImage(plusImg, for: .normal)
v.tintColor = .dark
v.translatesAutoresizingMaskIntoConstraints = false
// adding targets here did not work
v.addTarget(self, action: #selector(plusButtonHandler), for: .touchUpInside)
return v
}()
And it appears invoking the addTarget method inside of the closure did not work.
When I factored the addTarget method out and instead put it in the initializer, everything worked!
In my case I tried to added the UIButton direct to the cell by self.addSubview(myButton), so I changed to self.contentView.addSubview(myButton) and it worked
It seems the contentView was in front of the myButton so the button won't receive the tap gesture.
Your UIButton='s y offset is given as 160 and UIView's height is just given as 120. Please change the UIButton's y offset and try.
The UITableViewCell is handling all the gestures by default.
Set cell.selectionStyle = UITableViewCellSelectionStyleNone; to make it disable the default behaviour.
For Swift 3 and Storyboard
For me, I had to enable multiple touch inside the content view, and enable user interaction on everything - the table view, table view cell, content view, and UIButton. I was using storyboard.
I also set up my tableview cell as a subclass.
Since I couldn't find an answer here when I was exploring the problem, I posted another question on stack overflow about this. You can see my code and download a project with this working here: Can't click button inside UITableViewCell?
I ran into this problem, but in my case it was not related to the table view, instead it was because I was using subviews in my button for which userInteractionEnabled was true, setting that value for false (NO) for all the button's subviews fixed the issue for me.
I have the same issue, and somehow I figured out my mistake.
After I changed the constraints with self(UITableViewCell) to constraints with contentView of self(UITableViewCell),my cell's button is clickable again.
Update swift 4.2
in func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell
add it : cell.buttonCheckBox.isUserInteractionEnabled = false
OR, uncheck User Interaction Enabled inspector
Now you can select the button as well as a cell.
This works for me :)
Create a custom UITableViewCell. Refer this link.
Say you have a UIButton by the name of displayButton, you can do something like this:
Inside your - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath, after you create your cell,
[cell.dislayButton addTarget: self
action: #selector(replyButtonPressed:withEvent:)
forControlEvents: UIControlEventTouchUpInside];
And the create the method like so in your ViewController:
- (void) replyButtonPressed: (id) sender withEvent: (UIEvent *) event
{
UITouch * touch = [[event allTouches] anyObject];
CGPoint location = [touch locationInView: self.tableView];
NSIndexPath * indexPath = [self.tableView indexPathForRowAtPoint: location];
}
try this
public override void SetSelected(bool selected, bool animated)
{
// this.interestsButton.Selected = true;
if (selected)
{
// this.TextLabel.TextColor = UIColor.Red;
//this.interestsButton.ShowsTouchWhenHighlighted = true;
this.interestsButton.BackgroundColor = UIColor.Purple;
//this.interestsButton.SetTitleColor(UIColor.Red, UIControlState.Highlighted);
}
else
{
}
// cell.ContentView.UserInteractionEnabled = false;
base.SetSelected(selected, animated);
}
Tested on "Mvvmcross.iOS" only
Just to add to the discussion:
FOR SWIFT 5+
Is important to set the variable as lazy var when invoking the addTarget method inside of the closure. See:
lazy var plusButton: UIButton = {
let v = UIButton()
v.setImage(plusImg, for: .normal)
v.tintColor = .dark
v.translatesAutoresizingMaskIntoConstraints = false
v.addTarget(self, action: #selector(plusButtonHandler), for: .touchUpInside)
return v
}()
Now, about the contentView of the tableViewCells that appears above the cell stoping the click at the cell: I used the code above and also set the contentView to .ishidden = true and it worked!
Probably there is something linked to the lifecycle of the methods.
Hope it helps too.
If you have no alternative selector for the UIButton pressed inside some UITableViewCell you need turn off userInteractionEnabled for the button
cellButton.userInteractionEnabled = false
In this case any button touch will propagate to the cell touch.
I have a tableview with a custom tableviewCell. In this tableview cell I have a button for deleting a row.
First of all I create this tableview by the following code.
UITableView *tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 70, 320, 150) style:UITableViewStylePlain];
tableView.autoresizingMask = UIViewAutoresizingFlexibleHeight|UIViewAutoresizingFlexibleWidth;
tableView.delegate = self;
tableView.dataSource = self;
tableView.backgroundColor = [UIColor blackColor];
tableView.separatorStyle = normal;
[tableView reloadData];
I have an appointments array that I use as my datasource. In my cellForRowAtIndex I use the following code to attach my button to an action.
cell.btnDelete.tag = indexPath.row;
[cell.btnDelete addTarget:self action:#selector(deleteAppointment:) forControlEvents:UIControlEventTouchUpInside];
And then I have my action itself.
-(void)deleteAppointment:(id) sender{
NSLog(#"till here");
UIButton *button = (UIButton *)sender;
NSIndexPath *indexPath = [NSIndexPath indexPathForRow:button.tag inSection:0];
NSLog(#"indexpathc: %d",indexPath.row);
[tableView beginUpdates];
[tableView deleteRowsAtIndexPaths: [NSArray arrayWithObject:indexPath]
withRowAnimation: UITableViewRowAnimationFade];
[_exceptions removeObjectAtIndex:indexPath.row];
[tableView endUpdates];
}
It gives back the right index path in my log but it don't delete the row inside my tableview. Can anybody help me?
Kind regards
You also need to update the method numberOfRowsInSection. For example if you are keeping your elements in an array, delete one element from it and return updated array in numberOfRowsInSection.
I have my UITableViewCell subclass in which I use an UISwitch as accessoryView in this way:
mySwitch = [[UISwitch alloc] initWithFrame:CGRectZero];
self.accessoryView = mySwitch;
and all is ok! The app works fine.
Now I need to add some UIImageView above the switch, so I thought "Well, let's make a custom accessoryView!":
UIView *myView = [[UIView alloc] initWithFrame:CGRectMake(0.0f, 10.0f, 100.0f, 60.0f)];
...
mySwitch = [[UISwitch alloc] initWithFrame:CGRectMake(0.0f, 22.0f, 94.0f, 27.0f)];
[myView addSubview:mySwitch];
self.accessoryView = myView;
[myView release];
All seems to be ok, but there is a strange behavior. When I open another viewcontroller and get back to the table the switches are mysteriously changed...
It's not a problem in data management, but only in the cell redraw...
Please, help me, what can I do?
Thanks in advance
This happens because cells are reused. So if you added subview to cell's content view and then after that cell is reused in another row, that view will appear in that row. The best way to avoid this, is to hold NSArray in your table's datasource (it's usually your view controller) that contains all the custom views (with subviews). Then you can do like this:
-(UITableViewCell*) tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath*) indexPath {
NSInteger row = indexPath.row;
static NSString* cellIdentifier = #"Trololo";
UITableViewCell* cell = [tableView dequeueReusableCellWithIdentifier: cellIdentifier];
if( !cell ) {
cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier: cellIdentifier] autorelease];
}
[[cell.contentView subviews] makeObjectsPerformSelector: #selector(removeFromSuperview)];
[cell.contentView addSubview: [_your_cell_views objectAtIndex: row]];
return cell;
}
Emh... I found the problem... it was not linked to table redraw...
On UIControlEventValueChanged the selector retrieves the switch value and the cell indexPath.row in this way:
UISwitch *tempSwitch = (UISwitch *)sender;
UITableViewCell *cell = (UITableViewCell *)[tempSwitch superview];
and then it updates the corresponding object (of a data logic class) saved in a NSMutableArray (the datasource of the tableview)
But now the switch is a not the accessoryView, but a subview of the accessoryView, so the object is updates in a unpredictable way. I solved with a second superview message.
Sorry for my mistake and thanks to all...
I'm trying to create the exact same effect as in the AppStore app for viewing screenshots: inside a UITableView, I have custom subclasses of UITableViewCell's. One of them is designed to show previews of some product, say 4 images. I want them to show the same way as the AppStore presents screenshots of an app : inside a horizontal UIScrollView with its attached UIPageControl.
So I add my UIScrollView and my UIPageControl to my UITableViewCell, just like that :
#implementation phVolumePreviewCell
- (id)init {
if (self = [super initWithStyle:UITableViewCellStyleDefault reuseIdentifier:kVolumePreviewCellIdentifier]) {
[self setAccessoryType:UITableViewCellAccessoryDisclosureIndicator];
[self setSelectionStyle:UITableViewCellSeparatorStyleNone];
previews = [[NSMutableArray alloc] initWithCapacity:4];
for (int i = 0; i < 4; i++) {
[previews addObject:#"dummy"];
}
scrollView = [[UIScrollView alloc] initWithFrame:CGRectZero];
[scrollView setPagingEnabled:YES];
[scrollView setBackgroundColor:[UIColor grayColor]];
[scrollView setIndicatorStyle:UIScrollViewIndicatorStyleBlack];
[scrollView setShowsHorizontalScrollIndicator:YES];
[scrollView setBounces:YES];
[scrollView setScrollEnabled:YES];
[scrollView setDelegate:self];
[self.contentView addSubview:scrollView];
[scrollView release];
pageControl = [[UIPageControl alloc] initWithFrame:CGRectZero];
[pageControl setNumberOfPages:[previews count]];
[pageControl setBackgroundColor:[UIColor grayColor]];
[self.contentView addSubview:pageControl];
[pageControl release];
}
return self;
}
(note: I'm populating "previews" with dummy data here, just in order to have a [previews count] that works ; I want to view the scrollView's scroll indicator for test purposes only, I'll hide them later on).
My problem is that I can scroll the whole UITableView containing this phVolumePreviewCell (subclass of UITableViewCell), but I can't scroll the UIScrollView. How can I achieve this?
The only information I found is there: http://theexciter.com/articles/touches-and-uiscrollview-inside-a-uitableview.html
But it talks of old SDKs (namely 2.2) and I'm sure there is a more "recent" approach to doing this.
Some precisions :
I can scroll the UIScrollView with two fingers. That's not what I want, I want it to be able to scroll with one finger only.
When I scroll with two fingers, the TableView still intercepts the touches and scroll a little bit to the top or the bottom. I'd like the TableView to stick at its position when I touch the ScrollView.
I believe I have to work out how to intercept touches on my TableView, and if the touch is on the phVolumePreviewCell (custom UITableViewCell subclass), pass it to the cell instead of handling it on the TableView.
For the record, my TableView is created programmatically in a UITableViewController subclass:
#interface phVolumeController : UITableViewController <UITableViewDelegate> {
LocalLibrary *lib;
NSString *volumeID;
LLVolume *volume;
}
- (id)initWithLibrary:(LocalLibrary *)newLib andVolumeID:(NSString *)newVolumeID;
#end
#implementation phVolumeController
#pragma mark -
#pragma mark Initialization
- (id)initWithLibrary:(LocalLibrary *)newLib andVolumeID:(NSString *)newVolumeID {
if (self = [super initWithStyle:UITableViewStylePlain]) {
lib = [newLib retain];
volumeID = newVolumeID;
volume = [(LLVolume *)[LLVolume alloc] initFromLibrary:lib forID:volumeID];
[[self navigationItem] setTitle:[volume title]];
}
return self;
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return 1;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:kVolumePreviewCellIdentifier];
if (cell == nil) {
cell = [[[phVolumePreviewCell alloc] init] autorelease];
}
[(phVolumePreviewCell *)cell setVolume:volume];
return (UITableViewCell *)cell;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
return kVolumePreviewCellHeight;
}
Thanks for any help!
I have found a workaround.
Recreating from scratch a very simple UITableView (8 rows), and inserting a UIScrollView in just the second row, works perfectly. Nested scroll views (the TableView and the ScrollView) scroll independently as expected. All that was done in a single-file test project, in the AppDelegate.
So... first I thought "this might be a delegation problem", so I told the scrollView that its delegate was the TableView, not my custom TableViewCell subclass. To no avail.
Then instead of resorting to a custom, clean TableViewCell subclass that just adds the ScrollView and the PageControl, I resorted to creating a plain TableViewCell in the TableView's cellForRowAtIndexPath: method. Then, I create a UIScrollView right after initializing the TableViewCell, then add it to the TableViewCell, and shazam... it works!
Before:
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:kVolumePreviewCellIdentifier];
if (cell == nil) {
cell = [[[phVolumePreviewCell alloc] init] autorelease];
}
[(phVolumePreviewCell *)cell setVolume:volume];
// That does the job of creating the cell's scrollView and pageControl
return (UITableViewCell *)cell
After:
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:kvcPreviewRowIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:kvcPreviewRowIdentifier] autorelease];
previewScrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, tableView.frame.size.width, kvcPreviewScrollViewHeight)];
[previewScrollView setContentSize:CGSizeMake(1000, kvcPreviewScrollViewHeight)];
[[cell contentView] addSubview:previewScrollView];
previewPageControl = [[UIPageControl alloc] initWithFrame:CGRectMake(0, kvcPreviewScrollViewHeight, tableView.frame.size.width, kvcPreviewPageControlHeight)];
[previewPageControl setNumberOfPages:4];
[[cell contentView] addSubview:previewPageControl];
}
return (UITableViewCell *)cell;
How the heck comes that when I create my scrollView from the TVCell subclass, it doesn't play nice ; but when I create the scrollView from the TableView right after creating a "classical" TVCell, it works?
I might have found a solution, but I'm still puzzled by the reason.
I have a tableview cell with a custom textview in it, I am now left wondering how do I possibly access the text in the textbox after text has been edited/added.
I would normally know how this is done when I draw the textfield through IB, but my textviewcell is dynamically drawn.
(i.e. I would like to capture the data that is updated in detailLabel.text)
Here is the related code to adapt to your answer.
Thanks again!
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier] autorelease];
//Big Text Box
UITextView *detailLabel = [[UITextView alloc] initWithFrame:CGRectMake(30, 80, 500, 150)];
detailLabel.tag = 20;
[cell.contentView addSubview:detailLabel];
detailLabel.layer.borderWidth = 1;
detailLabel.layer.borderColor = [[UIColor colorWithRed:0.5 green:0.5 blue:0.5 alpha:0.9] CGColor];
detailLabel.layer.cornerRadius = 10;
detailLabel.font = [UIFont fontWithName:#"Helvetica" size:17];
[detailLabel release];
}
UITextView * detailLabel = (UITextView *) [cell.contentView viewWithTag:20];
switch (indexPath.row) {
case 0:
detailLabel.text = #"no";
break;
default:
detailLabel.text = [NSString stringWithFormat:#"%d", indexPath.row];
detailLabel.hidden = NO;
}
You'll want to listen for messages sent by the UITextView. So look to implement the UITextViewDelegate protocol and register the implementing class with the detailLabel using its delegate property.
When your delegate is notified of a change in the UITextView you'll have to identify the cell which currently owns the UITextView. In doing so you must remember that cells can be reused.
I would start by looking at the following method in the UITableView class:
- (NSArray *)visibleCells
We know that if the user has made a change to the UITextView's contents it must currently be on screen and therefore be present in the pre-mentioned array. To find it we use the pointer to the UITextView which changed (it's a parameter in the textViewDidChange protocol method). So simply iterate over the visibleCells array and retrieve the UITextView and compare it against the UITextView which changed.
- (void)textViewDidChange:(UITextView *)textView {
....
cellsLabel = (UITextView *) [cell.contentView viewWithTag:20];
if (cellsLabel == textView)
...
You'll now have a handle to a cell in the UITableView. To find the index use the following UITableView method:
- (NSIndexPath *)indexPathForCell:(UITableViewCell *)cell