How to get textFields in custom UITableView - objective-c

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];

Related

get indexPath for customCell in TableView

I have a UITextField in a customCell. I want to be able to get the text that user inputs in it. In my customCell.m file I can get the text that is inputed with
[self.aTextField addTarget:self action:#selector(aTextFieldEditing:) forControlEvents:UIControlEventEditingChanged];
but I cannot get the indexPath so I can save it to the NSMutableArray that holds all the UITextFields.
In my TableView I cannot get the didSelectRowAtIndexPath to run. I also cannot get the textFieldDidBeginEditing method to output anything. If it helps I have a TableView with sections.
My idea at the moment is to get the indexpath.row and indexpath.section from the tableView and save them as an extern so I can access it in the customCell.m
I would be grateful for any ideas or specific examples of how I could do this. Or a different cleaner way to accomplish what I want.
You should be able to get the index path using the -indexPathForRowAtPoint: method on UITableView.
You can do something like this (not tested):
- (void)aTextFieldEditing:(UITextField *)textField {
CGPoint convertedPoint = [textField.superview convertPoint:textField.center toView:self.tableView];
NSIndexPath *indexPath = [self.tableView indexPathForRowAtPoint:convertedPoint];
// Use your index path...
}
Edit:
In the case your cell is receiving the edit events, you can always define a protocol and make your view controller a delegate of the cell. You could then call this delegate method from your aTextFieldEditing method and pass the cell. Now that you have the cell you can call -indexPathForCell on your table view. If none of this makes sense, look into the delegate pattern. It's very common in Cocoa/Cocoa Touch and is very well documented online.

make custom UIView become first responder in custom UITableViewCell

I have three UITableViewCell subclasses. I'm displaying these in a table view in a view controller. I have, in the view controller, returning nil for tableView:willSelectRowAtIndexPath: since I want the UITextFields, in two of my UITableViewCell subclasses, to get focus and become first responder.
For the third UITableViewCell subclass, I have a custom UIView subclass that houses a UIWebView. Currently the div in the html content loaded in the UIWebView is one line tall. I previously made the div height 100% but found that scrolling started to act up. If I tap in the div the keyboard comes up. However, if the user taps any where else within this custom UITableViewCell the UIWebView div is not selected and the keyboard does not come up.
How can I get either my custom UITableViewCell or custom UIView subclass to become the first responder? Or am I going about this the wrong way?
I believe you're going about this the wrong way. Returning nil for tableView:willSelectRowAtIndexPath: won't really help your cause here.
Instead, you have a couple options. First, you could make a UIButton that sits behind your text fields in your table cell which fills the entire view, and then respond to touches on it and tell the text field to become active. Like so:
Wire up the button within your UITableViewCell subclass:
[self.button addTarget:self action:#selector(handleTouch) forControlEvents:UIControlEventTouchUpInside];
Set focus on the text field when touched:
- (void)handleTouch
{
[self.textField becomeFirstResponder];
}
The other option, and the one I like less, is to handle the selection of the cell in the delegate methods and tell the cell's textfield to become active. Like so:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
MyCustomCellSubclass *cell = [tableView cellForRowAtIndexPath:indexPath];
[cell.textField becomeFirstResponder];
}
In which of course you would need to add a textField property to your MyCustomCellSubclass in order to do this.
Hope this helps!

Playing songs with button in UITableView

I'm creating a custom table that has a button which allows a user to preview a song when pressed. Most of my code works but I haven't figured out how to pass the player a particular song corresponding to the row in which the button was pressed.
For instance: if I have two rows and #1 says Jay Z and #2 says Red Hot Chili Peppers, I want to press the button in #1 to play Jay and to press the button in #2 for the Peppers. Simple. My code is flawed and no matter which row's button I press I can only get the same song to play.
I know why that's happening, but I don't know how to solve it. I'm just wondering if anyone could hit me with a few lines that could point me in the right direction.
I can't use didSelectRowAtIndexPath because I want something else to happen when the row itself is selected.
Will I need to create a method for this or is there something I've overlooked?
Thanks!
You could also set the tag property of each button you create during tableView: cellForRowAtIndexPath:, then when your buttonTapped event is called, look up the sender and find its tag. The tag property of UIView was provided for just this sort of problem.
If you need more information than that, you could create a UIButton subclass that stores any or all information needed about the associated song. Once again, you set that information during cellForRowAtIndexPath, to be retrieved when the button is tapped.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath;
{
// Dequeue a cell and set its usual properties.
// ...
UIButton *playButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[playButton addTarget:self action:#selector(playSelected:) forControlEvents:UIControlEventTouchUpInside];
// This assumes you only have one group of cells, so don't need to worry about the first index. If you have multiple groups, you'll need more sophisticated indexing to guarantee unique tag numbers.
[playButton setTag:[indexPath indexAtPosition:1]];
// ...
// Also need to set the size and other formatting on the play button, then make it the cell's accessoryView.
// For more efficiency, don't create a new play button if you dequeued a cell containing one - just set its tag appropriately.
}
- (void) playSelected:(id) sender;
{
NSLog(#"Play song number %d", [sender tag]);
}
Something like
- (void)buttonTapped:(UIView *)sender;
{
CGPoint pointInTableView = [sender convertPoint:sender.bounds.origin toView:self.tableView];
NSIndexPath *tappedRow = [self.tableView indexPathForRowAtPoint:pointInTableView];
// get song that should be played with indexPath and play it
}
something like in tableView: cellForRowAtIndexPath: give your button tag as index.row and bind the function below to button's touchup inside event
-(void)button_click:(UIView*)sender
{
NSInteger *index = sender.tag;
//play song on that index
}
I think this will help you!

tableview with text fields in each row having resignkeypad issue

i am adding the textfield on each row inside the cellforrow method of tableview,but when i type anything on any textfield ind try to resign textfield on view touch, but it only works for last row's textfield only. what is the solution for that issue,
please help.
Thanks
Dhananjay
if i understand you correctly this will probably work:
add the protocol to your viewController in question and add
- (void)textFieldDidEndEditing:(UITextField *)textField {
[textField resignFirstResponder];
// custom code
}
afterwards make sure that each textField has your viewController as delegate.

Text entry ios question

I have two UILabels in a view that display my band and song name as strings. I am working on also adding the option to change either of these strings manually. I want to keep it as is, and I've added 2 buttons to manually enter a song name or band name. The thing is, all the text editing as far as I understand it needs to have actual TextField or TextView to bring up the keyboard etc.
I just want to touch one button for "enter song name" and be given a keyboard and when enter is hit, replace the string in the uilabel with that string, and the same for enter band name uibutton, and change the uilabel again.
Problem is in the docs I don't really understand how to do this. Does anyone have an idea about text entry in ios and can give me a pointer/tip on how to do this?
Just Declare your label Global and implement in implementation method
Take all song and all album data in array
and display all data in table view
now by using Delegate method of
(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
change the text of Label as u want
#interface songViewController ()
{
UILabel *bandLabel;
UILabel *songLabel;
NSArray *bandData;
NSArray *songData;
}
#end
#implementation songCreateViewController
- (void)viewDidLoad {
[super viewDidLoad];
{
bandLabel=[[UILabel alloc]init];
bandLabel.frame=CGRectMake(40, 580, 500, 30);
bandLabel.text=#"Join group as";
[self.view addSubview:bandLabel];
songLabel=[[UILabel alloc]init];
songLabel.frame=CGRectMake(40, 580, 500, 30);
songLabel.text=#"Join group as";
[self.view addSubview:songLabel];
}
#pragma mark Table view Delegate
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
if(tableView==albumTableView)
{
bandLabel.text=[bandData objectAtIndex:indexPath.row];
}
else
if(tableView==songTableView)
{
songLabel.text=[songData objectAtIndex:indexPath.row];
}
#end
What you are going to have to do is replace the label with a text field and set that as the first responder when the user presses the button. You will need a class that is the delegate of the text field so you handle enter in the textFieldShouldReturn method to resign first responder on the text field (to close the keyboard) and change the text field back to the label view and update its contents.
It can be achieve by many ways..
1) you can use hidden in your button methods to hide label and show textView.
2) you can use textField in place of label, make textfield.editable=NO and when button is pressed just clear the textfield by textfield.text=nil and make textfield editable.
Try these..
You want like when user touch on button you want to add text and button should be look like UILable. is it your objective?
If I am right you can achieve this thing to add gesture in uilable, you don't need to add button and handle tab method you can change the text.
let tap = UITapGestureRecognizer(target: self, action: #selector(self.handleTap(_:)))
myView.addGestureRecognizer(tap)
#objc func handleTap(_ sender: UITapGestureRecognizer) {
print("Hello World")
}