UITableViewCell not always selectable?
I'm in the middle of trying to debug an issue with a new app, and something isn't right. In the app, I'm setting up custom UITableViewCells by adding 2 UILabels and 1 UIImageView directly to the cell.contentView
In my app, certain table view cells werent selectable ( they werent responding to tap events ). The 2nd cell on the screen was always never selectable, and then random other cells also werent selectable.
In my effort to debug, I stripped everything down the following bare essentials of code:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPat开发者_StackOverflow中文版h {
static NSString *CellIdentifier = @"ReviewCell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
}
cell.textLabel.text = @"foo";
return cell;
}
Even this is generic, boiler plate code, that looks like the following:
not all the cells are selectable.
What am I missing?
update
as an updated here is my row selection code if interested
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
ReviewWebViewController *rvc = [[ReviewWebViewController alloc] initWithReview:[self.reviews objectAtIndex:indexPath.row]];
[self.navigationController pushViewController:rvc animated:YES];
[rvc release], rvc=nil;
}
A lot of things have been mentioned, which I will not re-iterate.
But make sure that the elements you add to the UITableViewCell have their userInteractionEnabled
set to NO
. Especially when not using IB, never make too many assumptions about defaults.
I got hit by a similar sounding bug a few weeks ago. I had buttons in a scroll view that weren't responding to touches. It turned out to be an invisible UILabel
that I left in my IB file, never filled with text and forgot about. It was sitting over the buttons and intercepting their touches.
The way I found it was to loop through all the subviews of the scroll view, turning the background color of each view yellow. Could you have a stray view which is interfering with touches?
You could also try sizing the labels and images in your cell to something tiny (10x10) to see if they are interfering. As a last resort, you could start with fresh code and add back in parts to see where it breaks.
One last point: You say that you're using a custom UITableViewCell
, but you initialize the cell with the default type:
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault
reuseIdentifier:CellIdentifier] autorelease];
Shouldn't you be using your custom cell here? Is that just to simplify the example?
What does your view hierarchy look like? Two things come to mind: either there are other views besides the table view that could be intercepting the touch events, or the table view itself is outside of its superviews frame (and clipping is turned off).
Long story short - lot of suggestions focusing on the implementation of the table view itself, I'd take a look at everything around it.
I tried with a sample application, which entirely mimicked your approach. It all worked absolutely fine. I wonder why your code doesn't work when every single bit of code mentioned above is correct. If required I can share my code with you. I am using the following code
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
NSLog(@"Row Tapped At %d",indexPath.row);
NextViewController *rvc = [[NextViewController alloc] initWithReview:[self.reviews objectAtIndex:indexPath.row]];
[self.navigationController pushViewController:rvc animated:YES];
[rvc release], rvc=nil;
}
where Review *first = [[[Review alloc] initWithReviewId:1 andTitle:@"First"] autorelease]; and so on... self.reviews = [[NSArray alloc] initWithObjects:first,second,third,fourth,fifth,sixth,seven,eight,nine,ten,eleven,twelve,thirteen,fourteen,fifteen,sizteen,nil];
I am using iPhone SDK 4.2.Please let me know If this mimics your approach and correct me If I have made any mistake. Thanks
If Nevan's solution doesn't help, then try removing the tableView:didSelectRowAtIndexPath method (or commenting it out) and see if the buttons glow with the blue hightlight when tapped. If they do, then I would suspect that
ReviewWebViewController *rvc = [[ReviewWebViewController alloc] initWithReview:[self.reviews objectAtIndex:indexPath.row]];
Is not always returning an object. To confirm this, set a breakpoint on the following line and check that rvc is not null. If it is, then that's the issue.
What happens when you take out the dequeueReusableCellWithIdentifier: codepath, and always use a fresh cell - are they all selectable then?
Is there a parent view to the UITableViewController whose frame could be set smaller than the bounds of the tableView? If so, touches outside the bounds of this parent will not register in the tableView. You'd still see the full tableView as long as the parent's clipSubviews was NO.
Also, check to see that your app window has "wants full screen at launch" set in the nib. If not, then the window may be the wrong size and touch events may not be reaching your tableview.
精彩评论