I have a table view with custom cells, each cell contains a textField. When the user tap on the textfield a UIPickerView will be displayed. I have this approach already done and work good. The thing is, the picker should display different data depending on which textField has been tapped, how can I detect this?
What I've done:
In viewDidLoad I create a view property which contains a UIPickerView.
In cellForRowAtIndexPath: I assign that view with the picker as an inputView for the textField (I did it this way to avoid creating a picker everytime a cell is rendered)
Ok, I found a solution that works really smooth:
I disabled the textField on every cell
I stop adding the picker as an inputView in cellForRowAtIndexPath
Once the user tap on didSelectRowAtIndexPath (which is called when they tap on the textField) I do the following:
if ([dataInfo[indexPath.row] isEqualToString:#"Accessibility"]) {
pickerItems = accessibilityItems;
}
else if ([dataInfo[indexPath.row] isEqualToString:#"Floor"]){
pickerItems = floorItems;
}
[picker reloadAllComponents];
SubCellTableViewCell * cell = [tableView cellForRowAtIndexPath:indexPath];
[cell.subcellSubtitleTextField setEnabled:YES];
cell.subcellSubtitleTextField.inputView = viewContainingPicker;
[cell.subcellSubtitleTextField becomeFirstResponder];
I hope this can help someone else!
Related
I want to display the pickerview as input for textfield in the tableview. I am reusing the same cell xib for all the rows in tableview. at indexpath.row == 1 want to change the age, so the input for textfield is pickerview containing numbers from 18 to 100. In the next row when click on textfield i need to change gender, so the data in picker view should be male and female. Please help me in achieving this.
Thanks in advance.
You can simply reload the picker view:
-(void)textFieldDidBeginEditing:(UITextField *)textField
{
[Yourpickerview reloadAllComponents];
}
Set the tag of the textfield and in textFieldShouldBeginEditing get the tag and then add
pickerVw = [[UIPickerView alloc] init];
pickerVw.dataSource = self;
pickerVw.delegate = self;
pickerVw.tag = TAG_PICKER;
// ... ...
[pickerVw reloadAllComponents];
Please See the GitHub link Has demo made for the purpose in swift
Thanks
I've searched this for a while but can't find anything quite the same.
I have a UITableView, and when a certain row is selected, I insert another row below.
The inserted row is a custom tableviewcell which hold a UIPickerView.
The pickerview works fine, and when an item is selected it can trigger the notification, sending selected info back to the tableviewcontroller, and then remove the "pickerviewcell". All good there.
But this isn't ideal if the user wants to scroll back and forth on the uipickerview. So I've added a uitoolbar to the uipickerview with a Cancel & Done button.
But the Cancel and Done buttons never get fired.
From other items I have read, they talk about UIFirstResponder etc etc, but they are all related to making the uipickerview an inputaccessoryview for a uitextfield. But that is not what I am doing.
I've tried doing it all in code and via Storyboards, with the same results each time.
Some example below..
// (in my CustomTableViewCell's AwakeFromNib function)
let screenSize: CGRect = UIScreen.mainScreen().bounds
pickerView = UIPickerView(frame: CGRectMake(0,0, screenSize.width, 162))
pickerView.delegate = self
pickerView.dataSource = self
pickerToolbar.barStyle = UIBarStyle.Default
pickerToolbar.translucent = true
pickerToolbar.tintColor = UIColor.orangeColor()
pickerToolbar.sizeToFit()
pickerToolbar.userInteractionEnabled = true
pickerView.addSubview(pickerToolbar)
self.contentView.insertSubview(pickerView, atIndex: 3)
// both these logs show correct output
NSLog("picker subviews: %#", pickerView.subviews.description)
NSLog("toolbar subviews: %#", pickerToolbar.subviews.description)
Screenshot example:
By clicking on the "To" cell, the new cell is inserted which has the picker. The picker works fine by itself. But the Cancel button doesn't get triggered. It has an IBAction linked to it from Storyboard.
Clicking any cell also closes/removes the pickercell correctly.
I have made a picker within a UITextview and I using
-(BOOL) textFieldShouldReturn:(UITextField *)textField{
[textField resignFirstResponder];
return NO;
and then dismissing the keyboard with
-(void)dismissKeyboard {
[_date resignFirstResponder];
}
where _date was my UITextfield property
Dismissing UIPickerView with Done button on UIToolBar
this is a really good post I found on this topic as well! I hope it helped.
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.
Here is the UI:
I have a table view that has rows including text fields. For example row1 has textField1 and row2 has textField2.
Now, if the user taps the textField1, everything works fine. The delegate is called and everything.
If the user, then, taps the textField2, only the textFieldDidEndEditing: is called for textField1. textFieldDidBeginEditing: is not called for textField2 (I am pretty sure that textField2 delegate is set, because when I then tap textField2 (again), it starts editing (textFieldDidBeginEditing: is now called)
You may think this is not a real problem. The problem is that the iOS keeps showing the keyboard! With no text fields associated to it! I even loop to all text fields in all visible rows and resign them with no luck.
Seems a bug in iOS, right?
Edit: Here is the code in the delegate
- (void) textFieldDidBeginEditing:(UITextField *)textField {
[self fixTableViewOffsets];
RSMCellEditPricesCell *cell = (RSMCellEditPricesCell *) [[textField superview] superview];
[self.tableView scrollToRowAtIndexPath:[self.tableView indexPathForCell:cell]
atScrollPosition:UITableViewScrollPositionTop
animated:YES];
}
- (void) textFieldDidEndEditing:(UITextField *)textField {
self.currentPriceTextField = nil;
[self fixTableViewOffsets];
[self.tableView reloadData];
}
First of all, be careful with the naming conventions, if you are adding a label at the end of a variable that variable should be a UILabel not a UITextField. Second your problem might be in the textFieldDidEndEditing method, you are calling resignFirstResponder on all your text fields(if the cell.priceLabel is a text field). You shouldn't callresignFirstResponder in textFieldDidEndEditing or textFieldSouldReturn because if those methods are called the text fields are already resign as first responder.
I couldn't find a fix for it. However, as a workaround, I disabled the second text field. When the user taps this field, it will trigger the touch event on cell, not the text field which will dismiss the keyboard. Then, I enable all text fields and the user can select this second text field again.
It's not quite a solution and it doesn't provide the best UX. Still, it work.
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.