UITableView does not respond to scrolling and touches
I've created a custom UIView, and inside that UIView, I add a UITableView like thus:
treeView = [[UITableView alloc] initWithFrame:CGRectMake(0, 80, slider.frame.size.width, slider.frame.size.height - 80)];
[treeView setDelegate:self];
[treeView setDataSource:self];
[self addSubview:treeView];
[treeView release];
When the app loads, the table seems to load fine. However, when I try to scroll the table, it does not respond to the touches at all.
I also implement these delegate/data source methods:
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView;
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section;
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath;
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath;
Does anyone have any idea why this is so?
Tha开发者_如何转开发nks
There are several possibilities:
- Your custom view, or one of its superviews, has
userInteractionEnabled
set to NO. - There is another view on top of your custom view, or on top of the table view within the custom view.
- The custom view that the table view is inside is smaller than the table view (with clipsToBounds=NO, a subview that extends beyond the bounds of its parent can be seen but not normally interacted with). Or the same for another view in the stack.
- The custom view (or one of its superviews) overrides
pointInside:withEvent:
orhitTest:withEvent:
incorrectly.
Make sure userInteractionEnabled is YES on the view(s) that need to receive touch events. Also try calling becomeFirstResponder on the view as well and call resignFirstResponder when you're done. Sorry I don't have more detail but it's been a while since I did this. Good luck!
Here's another reason why it can happen...
The UITableView is a child of another view where the frame is smaller than the table view. It doesn't seem make much sense but it can happen! I had a case ware the parent of the tableview had a width=0.
Which of course caused pointInside:withEvent: or hitTest:withEvent: to return NO!
For me, The reason why my TabView was not responding is because of the transform animation. I was animating my Table view using
view.layer.transform = CATransform3DMakeScale(0.5,0.5,0);
which is 3D transformation property. I found out that by using 2D transformation instead toVC.view.layer.affineTransform = CGAffineTransformMakeScale(0.5, 0.5);
Then my table view becomes responding to my touches and scrolling.
I am not sure exactly what is the root cause but this is my observation.
Maybe someOne will need this answer:
make your view add to cell directly, not to
cell.content
This work for me, when not use cell in table, but normal view. It seems touches will not forward to contentView in this moment.
精彩评论