Reputation: 862
I am having an issue with my UITableViewCell
selection.I am using a UITableViewCell
with a UITextView
sub view. The UITextView
object is not editable, not scrollable, with user interaction enabled.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell=[[[NSBundle mainBundle]loadNibNamed:@"DemoCell" owner:self options:nil]objectAtIndex:0];
cell. accessoryType=UITableViewCellAccessoryDisclosureIndicator;
}
if([distanceArray count]){
UILabel *label1=(UILabel*)[cell viewWithTag:1];
label1.text=[locationNameArray objectAtIndex:[indexPath section]];
label1.textColor=[UIColor blueColor];
UILabel *label2=(UILabel*)[cell viewWithTag:2];
[label2 setText:[distanceArray objectAtIndex:[indexPath section]]];
UITextView *tview=(UITextView*)[cell viewWithTag:3];
[tview setText:[discriptionArray objectAtIndex:[indexPath section]]];
return cell;
}
return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
DetailedLocationVIew *dView=[[DetailedLocationVIew alloc]initWithNibName:@"DetailedLocationVIew" bundle:nil];
[self.navigationController pushViewController:dView animated:YES];
[dView release];
}
If i select the cell over the UITextView
, the delegate didSelectRowIndexPath
not calling. Apart from over UITextView
object and selection works fine?
Why Selection Not working Over UITextView?
Upvotes: 6
Views: 8457
Reputation: 777
If only one subview needs to be disabled, one can do the following in Swift.
theSubTextView.isUserInteractionEnabled = false
This also disables isEditable and isSelectable.
Upvotes: 2
Reputation: 562
In my case I am resizing the tableViewCell
depending on the textView.text
length, so I've added a UIView
on top of the textView
and set its colour to 0.1%white
.
Upvotes: 0
Reputation: 9740
Use tview.userInteractionEnabled=NO;
and It will solve your problem.
If this doesn't solve your problem then try this out
for(UIView * cellSubviews in cell.subviews)
{
cellSubviews.userInteractionEnabled = NO;
}
It loops for all the subviews within cell and sets userInteractionEnabled
property to NO
.
Upvotes: 15
Reputation: 356
These suggestions were not working for me in iOS 6. What did work is the following.
In your:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
Add this:
cell.textView.dataDetectorTypes = UIDataDetectorTypeAll;
cell.textView.editable = NO;
cell.textView.userInteractionEnabled = YES;
UITapGestureRecognizer * tap = [[UITapGestureRecognizer alloc] initWithTarget: self action: @selector(forwardToDidSelect:)];
cell.tag = indexPath.row;
[cell.textView addGestureRecognizer: tap];
Along with:
- (void) forwardToDidSelect: (UITapGestureRecognizer *) tap
{
[self tableView: self.tableView didSelectRowAtIndexPath: [NSIndexPath indexPathForRow: tap.view.tag inSection: kTextViewCellSection]];
}
It appears to be necessary to set userInteractionEnabled = YES after setting editable = NO. Setting these in the XIB did not seem to work.
Upvotes: 8
Reputation: 3491
Because the UITextView did catch the touch event, you can solve this in 2 ways:
1) Subclass UITextView, catch touch events then pass it to the superView's same method using: ex:
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
[self.superview touchesEnded:touches withEvent:event];
}
2) Using UITextView delegate:
- (void)textViewDidBeginEditing:(UITextView *)textView
{
NSIndexPath *a = [NSIndexPath indexPathForRow:0 inSection:[textView tag]];
[tableview didSelectRowAtIndexPath:
}
Upvotes: 3