UITableViewCell selection over UITextView - objective-c

I get stuck with my cell selection. I have custom cell with UITextView object embedded in. It is not editable, not scrollable, with user interaction enabled. And I can not select cell over this view.
I've tried the same thing but with UITextField object and selection works.
Any ideas how to make it work?
This is the test project. There is just one table view with one cell. There is one text view and one text field in this cell. You can select the cell over the text field and can not over the text view.

Turn off "User Interaction Enabled" on your text view and the selection will work fine (MainStoryboard.storyboard->Table View Controller->Table View->Table View Section->Custom Table View Cell->Text View). Allowing user interaction on the text view is capturing the taps before the table view cell gets them. Since your text view isn't scrollable or editable, you don't want user interaction enabled.

Just to expound a bit on John Stephen's answer (which is the best as far as I can tell).
I've tried this by subclassing UITextView and overriding canBecomeFirstResponder:
#interface MyTextView : UITextView
#end
#implementation MyTextView
- (BOOL)canBecomeFirstResponder {
return NO;
}
#end
I've also set editable, selectable, and scrollEnabled to NO:
MyTextView *textView = [MyTextView new];
textView.editable = NO;
textView.selectable = NO;
textView.scrollEnabled = NO;
None of these worked. :(

The equivalent of John's answer for Storyboards for programmatic solution is:
textView.isUserInteractionEnabled = false

Related

TextField on Custom Cells

I am using custom cell inside tableView. In that custom cell, there is label and textField....this custom cell is being reused to display ten items. I actually want to open a datepicker on one of the cell's text field but I am not able to understand that how this work can be achieved?
All you need to do is to create an instance of UIDatePicker and set it as inputView of one of your text fields. When the text field is tapped UIKit will animate appearance of the picker for you. In turn, you'll have to handle user's input and update the text field's text.
Please set the text field tag and delegate in cellForRowAtIndexPath method...
like..
cell.yourTextFieldName.tag = 1000;
cell.yourTextFieldName.delegate = self;
In .h file add the UITextFieldDelegate
Then in the textFieldShouldBeginEditing delegate you can get the picker.
-(BOOL)textFieldShouldBeginEditing:(UITextField *)textField{
NSInteger txtFldTag = textField.tag;
if(txtFldTag == 1000){
//write the method to show picker.
return NO;
}

UITableView EditingStyle clear button hides

I'm currently creating my TableView, but I'm finding some problems with the EditingStyle and a TextField in the UITableViewCell.
So basically, the UITableView is set to EnableEditing for true at the beginning, so users can easily delete the dynamic content they are giving in the UITextFields in my UITableViewCells.
But the problem now is, by enabling the editing mode from my TableView the Clear button of the Textfield goes to the right. Is there a way that i can solve this?
The second picture is from my Storyboard. I've aligned the UITextField right to the Cell.
You can subclass the UITextField Class and override this method
- (CGRect)clearButtonRectForBounds:(CGRect)bounds
{
return CGRectMake(bounds.origin.x - 30, bounds.origin.y, bounds.size.width, bounds.size.height);
}
Good luck
Used AutoLayout for making the constraints. Worked well now

How to get textFields in custom UITableView

Very beginner obj-c question.
I have plain UITableView with two sections, but I am interested only in first section now. This section have four custom cells (inherited from standard UITableViewCell), and they have a UITextField's as a property.
I need to improve custom Input Accessory View with buttons "Next", "Previous"(for switch between textFields in tableview) and "Done" (dismissimg of keyboard). http://uaimage.com/image/62f08045
In -textFieldShouldReturn i set tags for textFields from 0 to 3. My next plan is to add textFields into NSMutableArray in -viewDidLoad and then just set and resign first responder for the textFields. Approximate code listing for "Next" button:
- (void) inputAccessoryViewDidSelectNext:(FDInputAccessoryView *)view {
for (UITextField *textField in [self textFieldsArray]) {
if ([textField isFirstResponder]) {
[textField resignFirstResponder];
UITextField *field = [[self textFieldsArray] objectAtIndex:textField.tag + 1];
[field becomeFirstResponder];
}
}
}
Questions:
Is this a right way or maybe there is a better approach to solve problem?
Do I need to tag textfields or use indexPath of cells in what they are built in? (or what is the best to track textFields?)
And the main question: what is the syntax to "get" textField from cell?
Sorry for the dumb questions, I am a very beginner.
Thanks,
Alex.
I think you have the right idea, but a few things come to mind:
Just to be safe, don't start with tag number 0. Every view has a tag number defaulted to 0, so you may get something unexpected.
Don't set the text view's tags inside of the textFieldShouldReturn, set the tags in cellForRowAtIndexPath or viewDidLoad, wherever you init the textFields.
Add the textFields to the cell's contentView, not the cell itself.
You don't have to resign first responder from the first text field, you can just becomeFirstResponder on the new one.
Make sure you're handling the last text view edge case: You could loop around to the first text field or simply dismiss the keyboard at the end.
If you want to get the textField in the cell:
UITableViewCell *cell = [self.tableView cellForRowAtIndexPath:
[NSIndexPath indexPathForRow:ROW_NUMBER inSection:1]];
UITextField *textField = (UITextField *)[cell.contentView viewWithTag:TEXT_FIELD_TAG];

Xcode - setFocus on a text field, becomeFirstResponder isn't enough

At the moment, I trigger a method on 'Did End On Exit' in my app (I'm aware that this may not be the greatest way of doing it but I'm very new to Objective C and Xcode for that matter and I'm simply doing what feels comfortable to me).
This method resigns the firstResponder from the current text field and applies it to a later text field.
The problem I'm facing is that the keyboard covers the next text field so that the use has no idea where the focus is and therefore what they are required to type.
How do I get it so that my keyboard shifts down and actually shows the text box that is currently active? Making something the firstResponder simply doesn't do what I want it to, unless there's part of the implementation I'm missing.
Here's my simple method:
- (IBAction)firstNameNext:(id)sender {
[firstNameTextField resignFirstResponder];
[surnameTextField becomeFirstResponder];
}
Any advice would be super.
Add UIScrollView in your main view then all contents as subview to UIScrollView
Now when specific UITextField needs to be able to visible in view use its delegate like this:
Note: add UITextFieldDelegate in .h file like this
#interface yourViewController : UIViewController<UITextFieldDelegate>
Also bind with File's Owner
- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField;
{
if(textField == yourSpecficTextField) //one u want move upwards
{
yourScrollView.contentOffset = CGPointMake(0,200); //required offset
}
... //provide contentOffSet those who needed
return YES;
}
- (void)textFieldDidEndEditing:(UITextField *)textField
{
yourScrollView.contentOffset = CGPointMake(0,0); //make UIScrollView as it was before
}
If you have keyboard input fields that will be covered by the virtual keyboard, then you need to move those fields out from under the virtual keyboard.
The normal way to do this is to have the controller's view be a scrollable view like UIScrollView. Moving Content That Is Located Under the Keyboard gives a very robust way of adjusting your scroll view and ensuring the required field shows.

Best practice to pass a value from pop over control on iPad

It is an iPad app based on SDK 3.2.
I have a MainUIView, that is subclass from UIView, it have a UIButton and a UILabel. When user press the UIButton, the pop over control will be appeared with a table view. When the user select a cell from the table view, the UILabel changes content base on the user click, and the pop up table view will disappear.
The question is, how can I pass the "selected cell" to the UILabel. I am thinking making a "middle man" object. When the user click the UIButton, and the "middle man" will pass to the table. When the cell is selected, the "middle man" will store the idx, and call the UILabel change content from the value of "middle man".
But I think it is pretty complex to implement, is there any easier way to implement it? thz u.
The standard way to do this is to call a delegate method when the popover closes with a selected value. The method will have been created on the view controller that calls the popover and handles setting the value.
In your view controller:
- (void) popoverDone:(id)sender {
label.text = [sender someValue];
[sender dismissPopoverAnimated:YES];
}
And in the popover:
- (void)tableView:tableView didSelectRowAtIndexPath:indexPath {
[delegate performSelector:#selector(popoverDone:) withObject:self];
}
There are other ways of doing this, but the principle is the same.