Two clickable items in UITableView - ios

I have a UITableview as a contact list in which there are a lot of users. It has a thumbnail photo and profile details on each row. I want to make it like when clicking on thumbnail, it goes another page for photo and when clicking on the rest of the space it goes to somewhere else. By using table view delegate I know which row is clicked and pass data, like user id to a new ViewController. But can I know which row when the thumbnail is clicked?
I am using the tag to find the view from cell, like
UIImageView *thumbnailView = (UIImageView *) [cell viewWithTag:1];
I think I cannot label the row index by tag.

You can add gesture to thumbnail image and get events on it. Need to set tag for thumb image as per indexPath.row.

Add following code in you in cell datasource method (cellForRowIndexPath:) :
cell.YOURIMGVIEW.userInteractionEnabled = YES;
cell.YOURIMGVIEW.tag = indexPath.row;
UITapGestureRecognizer *clickable = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(imageClicked:)];
clickable.numberOfTapsRequired = 1;
[cell.YOURIMGVIEW addGestureRecognizer:clickable];
[clickable release];
And also used below method :
-(void)imageClicked:(id)sender
{
UITapGestureRecognizer *gesture = (UITapGestureRecognizer *) sender;
NSLog(#"image tag is = %d", gesture.view.tag);
////////
You custome come goes Here !!!!!!!!!!!!!!
///////
}

You can use the photo as accessory view. Or use a UIButton with the photograph as background and set the action accordingly. (e.g. call a method on the view controller which then performs the push or segue. Doing so you will have to pass some data to the view controller indicating which row was actually clicked in. The number of the row can be used or you can set the tag of the button with some numeric id.)

Go the Easy way because doing it hard-way won't grant you a president award, right ?
Instead of UIImageView take a UIButton and set the Image on UIButton instance.
Set Button tag as the indexPath.row so that when you retrieve it you know which row is clicked. You can also sort it the other way but it seems quiet handy.
Add target into the button to a custom function. [ btnObj addTarget ...... ]
tyepcast you sender to a UIButton and receive the tag (indexpath.row)

You can remove all gesture recognizers and buttons in tableviewcell, and in your controller, you can implement below delegate;
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath;
To make this method triggered, you need to set tableview delegate as your controller, either via code as below, or using storyboard.
- (void)viewDidLoad {
self.tableview.delegate = self;
}
By doing this, it won't matter which item you clicked in your cell, delegate method will be called always.
I hope this helps.

Related

Send cell string value with button action

Hello I have working json parsing with custom cell and i have some label values and i added buttons for every cell and i want to take ID when clicked button on cell. My codes here
In My Tableview codes
cell.IDLabel.text= ID;
cell.cellButton1.tag=200+indexPath.row;
Button Action
- (IBAction)didTapCellButton1:(UIButton *)sender {
int *myrow = sender.tag-199; // Now i know which row clicked with this code , But i want to know IDLabel value when this button clicked
}
i know which row clicked with didTapCellButton1 action code , But i want to send IDLabel value when this button clicked
Thank you !
You should create #protocol for your custom cell, something like - (void)myCustomCellDidSelectButton:(MyCustomCell *)cell, than implement delegate in your ViewController and set cell.delegate = self in cellForRowAtIndexPath:. Now when your button's action fires inside cell call delegate method. When you have cell you can check your ID even without your DataSource.
finally . I found easy way :)
My working codes here;
cell.IDLabel.text= ID;
int valueid = [cell.IDLabel.text intValue];
cell.cellButton1.tag=valueid;
And when button clicked
int myvalue = sender.tag;
I think will be help too many people
thanks !

Need to disable particular button in swipe cell?

I am using https://github.com/CEWendel/SWTableViewCell library to my project.
Certain situation I need to disable the particular button action of swipe cell.
I cannot find any property in their class file. If anyone crossed this, give me answer.
Here I have attaced my swipe options image:
For ex
: I want to disable the share button action.
Let's assume your share button is in the leftButtonsArray. In the method:
- (void)swipeableTableViewCell:(SWTableViewCell *)cell scrollingToState:(SWCellState)state
{
//case:left buttons opened
UIButton *shareButton = leftButtonsArray[theIndexOfTheShareButton];
shareButton.enabled = NO;
}
#karthikeyan You can hide the button for a particular row in tableview by the following code:
- (void)updateRightUtilityButtons:(NSArray *)rightUtilityButtons WithButtonWidth:(CGFloat) width {
_rightUtilityButtons = rightUtilityButtons;
[self.rightUtilityButtonsView updateUtilityButtons:rightUtilityButtons WithButtonWidth:width];
[self.rightUtilityButtonsView layoutIfNeeded];
[self layoutIfNeeded];
}
Add/update this methods to SWTableViewCell.m class, where rightUtilityButtons is an array of buttons you need to display for the particular row.
In case if you want to disable just user interaction you can achieve while adding button into array, just disable user interaction for that button by shareButton.userInteration = NO and then add to array and then pass the array to the method defined above. By this you can be sure that button is disabled.
But please provide the sample code that you have worked so that can update your code directly.
In case if you still didn't get revert back I'll give you the working code directly here.

How to do (custom) selection with custom UITableViewCell subclass

I have a UITableView with a custom UITableCellView subclass. I want to show one of the rows as active or selected. The controls in my cell have their own gestures, but I have area on the right where I can do the selection. I had (naively) thought I could just indicate I wanted a checkmark style accessory but Apple's docs say
This control does not track touches. The delegate of the table view
can manage check marks in a section of rows (possibly limiting the
check mark to one row of the section) in its
tableView:didSelectRowAtIndexPath: method.
I'm not sure how to interpret that, but I put the tableView:didSelectRowAtIndexPath: method in my delegate, and it never fires. Regardless of where I touch my cell (on the part where there are touchable controls or not). So I'm curious why that doesn't work? I also noticed that regardless of how I set the selected property on the cells, the checkmark always shows on, so I don't think it's really meant to be used a selection indicator.
But I'm actually kind of OK with that. I don't really want a checkmark, I'd rather do something like mail does when it does multi select on the left side, the radio-button-esque style circles. Which then leads me to a quandary about how to proceed. Should I just add a button control to the right side, manipulate the images appropriately in my custom cell subclass? Do I mess with the background image of the button in that case? Or just the image? And since selecting one needs to deselect the others, what's the best way to connect this to the table view delegate, rather than the subclass? Or should I make a custom NSView subclass?
UPDATE
I removed the accessory. I added an UIImageView to show selection state. Because the sub control in my cell uses a hold gesture, the selection tap makes it through just about everywhere for the cell. I synchronize the visual selection state using the following method:
- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
[super setSelected:selected animated:animated];
self.selectionView.image = self.selected ? [UIImage imageNamed: #"selected"] : [UIImage imageNamed: #"not_selected"];
}
To my custom controller, I added the following method:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
[Site setCurrent: self.allSites[indexPath.row]];
[[Site current] pullValves];
}
It finally donned on me that that the first method was for updating the visual state, but not for responding to the users intent, that belongs in the second method. The first fires at various times, but the second applies when the user actually does the tap.
Finally, I had to programmatically set
self.clearsSelectionOnViewWillAppear= NO;
Not matter what I set in the storyboard, that value was YES, so returning to the list was clearing my selection.
You're adding a gesture to each of the cells you create? I would back that out of there and create a gesture on the main controller which holds your tableview (not the individual cells).
Add this to your main controller so the controller can handle the gestures accordingly.
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer {
return YES;
}
If you need to track the cell that was touched (say, for a panning gesture), you can grab the index path given the gesture recognizer.
CGPoint p = [recognizer locationInView:self.tableView];
NSIndexPath *indexPath = [self.tableView indexPathForRowAtPoint:p];
From there, we go back to your goal: tracking your active cell. tableView:didSelectRowAtIndexPath needs to be hit (hopefully the steps above help here). Store some sort of identifier of the item you need to keep selected. When tableView:willDisplayCell:forRowAtIndexPath is called you can trigger the selection state.

UICollectionViewCell subview receive tap insted

I have a UICollectionViewCell with some my content on it as image, labels, views.
I would like to obtain this behavior (like an opposite behavior of a normal cell):
this cell by default have a view (a simple UIView with an alpha set to show some opacity) like when it is not selected.
when a user select the cell this alpha view have to be removed.
If I try to install a UITapRecognizer:
// Create gesture recognizer
UITapGestureRecognizer *oneTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(onShadowLayerPressed)];
// Set required taps and number of touches
[oneTap setNumberOfTapsRequired:1];
[oneTap setNumberOfTouchesRequired:1];
// Add the gesture to the view
[self.shadowView addGestureRecognizer:oneTap];
and after the method
- (void)onShadowLayerPressed
{
[super setSelected:YES];
[self setSelected:YES];
}
The tap is received from the self.shadowView but it doesn't send to the cell....
How can I solve this issue?
Thanks
You are using #property (nonatomic, getter=isSelected) BOOL selected and according to the docs,
You typically do not set the value of this property directly. Changing the value of this property programmatically does not change the appearance of the cell. The preferred way to select the cell and highlight it is to use the selection methods of the collection view object.
So you are supposed to use:
- (void)selectItemAtIndexPath:(NSIndexPath *)indexPath animated:(BOOL)animated scrollPosition:(UICollectionViewScrollPosition)scrollPosition
Which is documented here.

iPhone SDK: Opening a cell with a dedicated button, not by cell tapping

I have a simple TableView containing several cells. Normally, I switch to selected cell details by tapping this cell. But what if I need a dedicated button for every cell? I've seen "Table View Cell" properties in Interface Builder, it has what I need, but it can't be added to existing cells.
How to properly add this kind of button to every cell of standard TableView?
I do something similar in an app I am working on right now. I have a cell that has a button on it, and I need to know which button was pushed in which cell. I do that like this..
I add my button to each cell..
// add buy button to each cell
UIImage *image;
buyButton = [UIButton buttonWithType:UIButtonTypeCustom];
image = [UIImage imageNamed:#"buy.png"];
[buyButton setBackgroundImage:image forState:UIControlStateNormal];
buyButton.frame = CGRectMake(220, 35, 96, 34);
[buyButton setTag:cellIndex];
[buyButton addTarget:self action:#selector(buyTickets:) forControlEvents:UIControlEventTouchUpInside];
[cell.contentView addSubview:buyButton];
The method used to determine which "button" in which cell was selected, I then push another view controller with the information of the selected button...
// buy tickets button pressed from main table view
- (void) buyTickets:(id)sender{
ResultViewController *vc = [[ResultViewController alloc] init];
vc.buyMovieID = [sender tag]; // "sender tag" is the cell id the button is located in
[[super navigationController] pushViewController:vc animated:YES];
[vc release];
}
This is what the button looks like on each cell.
Hope this helps!
P.S. Tapping on the CELL, would push another view controller, but tapping on "Buy Tickets" button pushes a different one.
alt text http://luistovar.com/ultratableview.jpg
I think the detail disclosure accessory type is what you need. The doc can be found on UITableViewCell class reference.
It says :
The accessory view appears in the the right side of the cell in the table view’s normal (default) state. The standard accessory views include the disclosure chevron; for a description of valid accessoryType constants, see “Cell Accessory Type.” The default is UITableViewCellAccessoryNone. If a custom accessory view is set through the accessoryView property, the value of this property is ignored. If the cell is enabled and the accessory type is UITableViewCellAccessoryDetailDisclosureButton, the accessory view tracks touches and, when tapped, sends the data-source object a tableView:accessoryButtonTappedForRowWithIndexPath: message.
Setting your cell's accessoryType property to UITableViewCellAccessoryDetailDisclosureButton, you can easily do whatever you wan when the detail disclosure button is pressed. What's great about using this accessory type is that it's a standard button, so it is user-friendly and it does all the job of tracking which cell has been touched for you.
If you want to use a custom button, you should set the accessoryView property to that custom button and listen to events on it.
The way i added a button to a tableview cell was by subclassing UITableViewCell and then building its view in interface builder.

Resources