Tap on UIImageView in the Table Cell to show larger image - iphone

I am writing an app where I need to show the thumbnail image on each of the table cell, when this thumbnail image is taped it should push new view with larger image. So there should be two touch events on single cell, one for image and other for showing detail view. By default in the didSelectRowAtIndexPath I am invoking detail view which is working fine.
Can someone suggest an approach please.
Thanks,
Bhaskar

The simplest way would be to add the thumbnail in a button as the accessory view of the cell. When the button was hit it would call accessory view method which would load the next view.

from your question, it is understood that you are having a custom cell with an uiimageview in it. In that case, wherever you tap in that cell, may be in the image or the empty portion, the didSelectRow atIndexPath method is invoked. The better way is to use a button to show any of them,, similar to a discloure button in navigators.

I would recommend TechZen's approach: You could add a custom UIButton and place it wherever you like in the cell, and that freedom is nice, but in my experience these tend to really kill scrolling performance. Whatever optimizations are done for accessory views may make them a better experience for your users than buttons.

I agree with Alex. More info: This is what the Detail Disclosure Button accessory view is for on table rows. For an example of this, see the Favorites tab in the Phone application. Tapping on a row calls the person, but tapping on that blue circular button to the right takes you to the detail view for the person's contact info. Another example is the YouTube application.
I think you want something like the YouTube application where tapping on the table cell displays a larger image, and tapping the detail disclosure button takes you to metadata about the image.
See the docs for UITableViewCell for how to add a detail disclosure button. It's very simple.

Since we know that the image is in a cell and I hate subclassing, we can put together a little fun hack. You can just use the cell.imageView property and toss a category on UIImageView:
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event{
if([self.superview isKindOfClass:[UITableViewCell class]]){
if([touches anyObject].tapCount == 1){
//Image was tapped, issue notification, we use the cell as the object
[[NSNotificationCenter defaultCenter] postNotificationName:#"CellImageTapped" object:self.superview]
//just return after the notification
return;
}
}
//if it wasn't a tap, just forward the touch
[super touchesEnded:touches withEvent:event];
}
Then in the table view controller, you can resolve the selected cell by:
NSIndexPath indexOfSelectedCellImage = [self.tableview indexPathForCell:[notificaton object]];
I didn't check, but the superview of the cell.imageView may be the contentView, in that case just substitute in the following:
…if([self.superview.superview isKindOfClass:[UITableViewCell class]])…
…[[NSNotificationCenter defaultCenter] postNotificationName:#"CellImageTapped" object:self.superview.superview]…
Note: Be sure to have your table view controller subscribe to the notification.

Related

How do I tell the difference between the user tapping my table view cell and the user tapping a subview contained within the cell?

One of my view controllers contains a UITableView with custom UITableViewCell's. My custom UITableView cell contains a UIImageView subview that represents a tappable icon. When the user taps anywhere on the custom cell, except the icon subview, I want my didSelectRowAtIndexPath method to be called like normal. But when my icon subview gets tapped, I want a different method to get called, but I can't figure out how to do this. Do I have capture the touch position in the touchesDidBegin method and manually check if the user tapped the icon? That just feels so hacky. Other, cleaner ideas?
Thanks so much for your wisdom!
Simply add a tapGestureRecognizer to the UIImageView in your cell

iOS: cancel delete event UITAbleViewDelegate

I want to know the delegate method that gets called when the user cancel the delete operation of the UITableViewCell. Now to answer the potential question that "Why do I need this?", following is the scenario:
I have a table view where the item is displayed (which is left aligned to the main view) in a UILabel and its price is displayed in a separate UILabel (which is right aligned to the main view).
Now once the user presses the red (kind of no entry) button to delete any item, the whole cell is indented to the left and half of the price is clipped because of being out of the view. This looks quite ugly and hence I hide the price label upon press of this 'pre-delete' button (which works fine). But I want to display the price tag back when the user dismiss the delete button without deleting the cell. but I am unable to find the cancelDelete kind of event for tableview cell.
Thanx :-)
Here's something that might work instead, it stops the tableview from indenting the cells while editing:
Set UITableView's shouldIndentWhileEditing property to NO.
Implement the delegate-method tableView:editingStyleForRowAtIndexPath: method:
- (BOOL)tableView:(UITableView *)tableView shouldIndentWhileEditingRowAtIndexPath:(NSIndexPath *)indexPath {
return NO;
}
Or you can do this when you create your cells:
cell.shouldIndentWhileEditing = NO;
Hopefully this works for you.

iOS 5: Hide keyboard in UITableViewController with static cells and textfields

I'm developing an app that has an UITableViewController with static cells. Those static cells are custom ones and have UITextFields within them. If the user touches one of those textfields, the keyboard pops up. So, after that, what I need to do is to be able to dismiss the keyboard by touching anything on the background. By anything I mean the table view background, its cells and the content of those cells (like the text fields for example).
I used to do this by placing a View in the front of all my other subviews and capture the Touch Inside Up event to dismiss the keyboard. Unfortunately, I can't do this this time because I'm using a UITableViewController and I can't switch it back to an UIViewController because I've already done a lot of work with those static cells.
-(void)touchesBegan: (NSSet *)touches withEvent:(UIEvent *)event{
[yourTextField resignFirstResponder];
}
Try that.
EDIT
Hi,
I've got your answer :
-(BOOL)isFirstResponder
When you enter something in a text field, it becomes first responder and the keyboard appears. You know that the keyboard is active if [textField isFirstReponder] returns YES.
You may also see that link.may be it will help you.
how to hide the keyboard when empty area is touched on iphone
Well, I found the answer myself. What I need to do was just place programatically an UIView in the foreground and do as I said in the original post. I couldn't do this before because I was trying to achieve that from the Interface Builder.

How to draw a button over a last visible uitableview cell?

I need to display search button at the edge of last visible cell in uiTableView. Something like this:
I don't know how to get the right CGRect to draw this button (view) and how to redraw this, when the user scrolls the tableview.
Tnx.
The UITableView method
- (CGRect)rectForRowAtIndexPath:(NSIndexPath *)indexPath
should help you.
You may need to offset this by the tableview.frame.origin if you are drawing it in the parent view.
Hope this helps!
Another way would be to have the button attached to the View (rather than cell), On tapping the button you could find the NSIndexPath for last cell by iterating through visibleCells.

UITableViewCell subview (left side)

Is it possible to implement a subview of a UITableViewCell on the left side of the cell that crossfades when the table enters the editing mode?
Another problem I face is that the bounds of the cell.textLabel are read-only. Is it possible to use some sort of inset for that label? (Because I, like mentioned above, want to use a View on the left side)
Edit: How do I perform an action (in this case fade a subview) when the whole table enters editingmode? (Not through a swipe over a cell). The reason why I want to implement this is because the tableview shows the "-" button on the left side of the cells if it is in editingmode. (I want to show my own button on the left side of the cells if editing=NO, fade it out if editing=YES and show it again if the tableView leaves editingmode (editing=NO))
A first thought directs me to something like this:
- (void)setEditing:(BOOL)editing animated:(BOOL)animated {
[super setEditing:editing animated:animated];
if(editing) {
//fade out my own button
} else {
//show my own button
}
}
But I would have to loop through all the cells and I think this leads to a substantial performance worsening. What do you think?
You want to set the Editing Accessory View of a UITableViewCell. See the editingAccessoryView property for information on this. More information can be found in the Customizing Cells section of the TableView Programming Guide.
The label within the content view of a table view cell is read-only so you can not reassign it but its properties are mutable. You can actually move the label around within the bounds of the table view cell. It's probably confusing to grasp, but have a look at the Characteristics of Cell Objects section of the Table View Programming Guide. In it, the bounding boxes for each of the nested views are shown (not all of them are always visible). For your purposes, a good exercise would be to set the background color of each subview of a UITableViewCell to a different color and then try to adjust the sizes of them. Doing so and understanding what's going on will probably let you achieve the end-result that you desire.
In the end I created a custom UILabel that I used instead of the default UITableView textLabel and added a custom button on the left side. In the setEditing method I fade this button in/out.