how to add a textfield on last row in tableview in iPhone? - iphone

when i press the new button i need to add a textfield on last row of tableview.
can any one explain the flow.
Thanks in advance.

Just i tried this scenario.
Create a one Boolean variable like this
BOOL newButtonPress = NO;
then fallow the below code
-(IBAction)newButtonClicked:(id)sender{
if (self.newButtonPress == NO) {
//if new button press then newbuttpress is yes and reload the tableview
self.newButtonPress = YES;
[self .reloadTableview reloadData];
}
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
if(self.newButtonPress == YES)//creating noOfRows when newbutton pressed
{
return [self.itemNames count]+1;
}
else {
return [self.itemNames count];
}
}
Then add the below code into the cellForRowAtIndexPath method
//when new button is pressed adding new cell
if (self.newButtonPress == YES) {
if([self.itemNames count] == [indexPath row]){
cell.selectionStyle = UITableViewCellSelectionStyleNone;
//creating the textfield if new button is pressed
UITextField *extraField = [[UITextField alloc] initWithFrame:CGRectMake(80, 6, 100, 30)];
extraField.borderStyle = UITextBorderStyleNone;
extraField.textAlignment = UITextAlignmentCenter;
[extraField becomeFirstResponder];
[extraField setDelegate:self];
[cell addSubview:extraField];
[extraField release];
}
}

If you just want the text field to appear at the end of the table, why not consider using UITableview's tableFooterView property?
In that case you don't have to ensure that you return the UITextField in your delegate methods.
You should nearly be able to drag drop a new UITextField in your Tableview if you are using InterfaceBuilder or XCode 4.

I am very new to iphone coding,i want to share my thoughts. If that is wrong please ignore.
In cellForRowAtIndexPath method u need to set a textfield in
if(indexPath.row==numberOfRows-1) condition.
In button pressed method u can set textField.hidden=NO and in viewdidload textField.hidden=YES.

Related

how to implement textfield in table the way it showing in image

i want to add textfield on tableview each and access the textfield's text by using their tag.
how can i do that see below image.
// To get the text from UITextField , add the bellow code to after you initialize the Textfield
[yourTextField addTarget:self action:#selector(getTextFieldValue:) forControlEvents:UIControlEventEditingDidEnd];
and this method
- (void) getTextFieldValue:(UITextField *)textField{
NSLog(#"GetTextvalue");
if (textField.tag == 0) {
NSLog(#"1--->%#",textField.text);
}else if(textField.tag == 1){
NSLog(#"2--->%#",textField.text);
}else if(textField.tag == 2){
NSLog(#"3--->%#",textField.text);
}
}
You have to customize your tableViewCell you can do that in your cellForRowAtIndexPath method.
While you create your cell in the method you can do following:
UITextField *textField = [[UITextField alloc] init];
// Set a unique tag on each text field
textField.tag = 101;
// Add general UITextAttributes if necessary
textField.enablesReturnKeyAutomatically = YES;
textField.autocorrectionType = UITextAutocorrectionTypeNo;
textField.autocapitalizationType = UITextAutocapitalizationTypeNone;
[cell.contentView addSubview:textField];
[textField release];
After that where every you want to access your text field you can do following:
UITextField *myTextField = (UITextField*)[cell viewWithTag:yourTag];
Hope this is what you are looking for..
Update
As you can see I assigned tag 101 as static. This can be any number but make sure that you don't have any other view with the same tag in the same cell. If you have in another cell than its not an issue.
Now at anyplace if lets say in didSelectRowAtIndexPath you can do following:
UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
UITextField *myTextField = (UITextFiled*)[cell viewWithTag:101];
This way you will get your cell first. Now your cell has only one view with 101 tag so you can get your text field regardless you have same tag for other textField because the cell is not same for all.
Cheers
UITextField *myTextField = (UITextField*)[cell viewWithTag:yourTag];
myTextField.tag=indexPath.row;
This way assign 10 textfields tags.
After in textfield delegate method you can retrieve that text from the different 10 tags.

Update TableView With a Button in each cell after BarButtonItem pressed

I have a tableviewcontroller with a uinavigation bar that has a barbuttonitem, called editBarButton. When editBarButton is pressed I want my tableview to be updated with a new button in each of the cells that says 'Edit'. What is the correct way to implement this?
- (void)onEditBarButtonPressed{
//TODO: update cells
}
You have to overwrite the accessoryView attribute in your UITableViewCell with your Edit button:
Create a custom button to overwrite the current accesoryView:
- (UIButton *) makeDetailDisclosureButton
{
UIButton * button = [UIButton yourEditButton];
[button addTarget: self
action: #selector(accessoryButtonTapped:withEvent:)
forControlEvents: UIControlEventTouchUpInside];
return ( button );
}
Then the button will call this routine when it's done, which then feeds the standard UITableViewDelegate routine for accessory buttons:
- (void) accessoryButtonTapped: (UIControl *) button withEvent: (UIEvent *) event
{
NSIndexPath * indexPath = [self.tableView indexPathForRowAtPoint: [[[event touchesForView: button] anyObject] locationInView: self.tableView]];
if ( indexPath == nil )
return;
[self.tableView.delegate tableView: self.tableView accessoryButtonTappedForRowWithIndexPath: indexPath];
}
You can see a similar question here: Using a custom image for a UITableViewCell's accessoryView and having it respond to UITableViewDelegate
I found a solution to my problem.
You should add a object level BOOL flag called editPressed.
It should be set to NO in viewDidLoad.
When making each cell, add a button to each and set it to hidden if need be:
[button setHidden:!editPressed];
It is important to use the flag so that when new cells are made they will keep the buttons hidden if they should be or visible otherwise.
Then have a object level NSMutableArray * of the buttons in the view controller and add each button to it:
[buttons addObject:button];
When you want to show each button, just change the hidden state:
editPressed = YES;
for(int i = 0; i != [butttons count]; i++){
[[buttons objectAtIndex:i] setHidden:!editPressed];
}
When you want to hide each button, once again, change the hidden state:
editPressed = NO;
for(int i = 0; i != [butttons count]; i++){
[[buttons objectAtIndex:i] setHidden:!editPressed];
}

Add UITableViewCell with UITextField Dynamically

I'm attempting to have a UITableView that I can dynamically add and remove rows from, and the rows have a UITextField in them. For adding the rows, I'm using the code:
- (void) addRow
{
[nameArray addObject:[NSString stringWithFormat:#"%i", x]];
[self.tableView reloadData];
x++;
}
And I'm just doing a count of nameArray to get how many rows I have in my tableView. Then, inside of cellForRowAtIndexPath, I've got the following code
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
...
/*Default Apple-y stuff*/
...
if ([indexPath row] == 0) {
playerTextFieldZero = [[UITextField alloc] initWithFrame:CGRectMake(20, 10, 185, 30)];
playerTextFieldZero.adjustsFontSizeToFitWidth = YES;
playerTextFieldZero.placeholder = #"Name";
playerTextFieldZero.textColor = [UIColor blackColor];
playerTextFieldZero.returnKeyType = UIReturnKeyDone;
playerTextFieldZero.backgroundColor = [UIColor whiteColor];
playerTextFieldZero.autocorrectionType = UITextAutocorrectionTypeNo; // no auto correction support
playerTextFieldZero.textAlignment = UITextAlignmentLeft;
playerTextFieldZero.tag = 0;
playerTextFieldZero.delegate = self;
playerTextFieldZero.clearButtonMode = UITextFieldViewModeNever; // no clear 'x' button to the right
[playerTextFieldZero setEnabled: YES];
[cell addSubview:playerTextFieldZero];
[playerTextFieldZero becomeFirstResponder];
[playerTextFieldZero release];
}
...
/*More of those in here*/
...
return cell;
}
I've got multiple issues with this code. The first issue is I'm doing a preset number of UITextFields, so that I can call them all in textFieldShouldReturn. Is there a good way for me to generate UITextFields that will return when I press the done key?
The second biggest issue with the way I'm doing this right now is my UITextFields get cleared every time I add a new one. Any idea why?
To solve your first issue I would begin by pulling the UITextField creation code into a method..
- (UITextField*)textFieldForCell:(UITableViewCell*)cell withDelegate:(id<UITextFieldDelegate>*)delegate {
UITextField textField = [[UITextField alloc] initWithFrame:CGRectMake(20, 10, 185, 30)];
textField.delegate = self;
....
[cell addSubview:playerTextFieldZero];
[textField release];
}
Then invoke the new method in your tableView:cellForRowAtIndexPath: method...
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
...
// Custom initialization code
[self textFieldForCell:cell withDelegate:self];
}
Now to make sure that your UITextField's respond to the return key implement the textFieldShouldReturn: method of your UITextFieldDelegate (probably your UITableViewController) to always return true...
-(bool)textFieldShouldReturn:(UITextField*)textField {
return YES;
}
As for your second issue, I believe this is a result of directly invoking reloadData. This will force your UITableView to recreate its cells. This in turn recreates your UITextFields and you subsequently lose their state/text. I think your next logical step will be to introduce a model (NSMutableArray) that stores the state of each UITextField. You could begin by saving the text of the field into the array upon the UITextFieldDelegate receiving the textFieldShouldReturn message.

Unable to Save Edits to Core Data Attributes

I'm trying to build a UI so the user can edit the attributes of a core data entity. When the user taps the edit button, selecting a row will push the listDetailViewController, which is just a table view that displays the attributes. It uses a custom table view cell with a label and a UITextField. The listDetailViewController displays the attributes properly, and will accept text as its supposed to, but I can't figure out how to get the user-inputted text to save.
If I'm not explaining clearly, here's an example. I want to change the list's name, so I tap Edit, tap the list, tap the List Name row, the keyboard pops up, I type in the new name, tap Done and it pops me back to the RVC with none of the changes saved. I've been banging my head on this for a few days and would love some help!
Here's the relevant code from ListDetailViewController:
- (void)viewDidLoad {
[super viewDidLoad];
UIBarButtonItem *doneButton = [[UIBarButtonItem alloc]
initWithBarButtonSystemItem:UIBarButtonSystemItemDone
target:self
action:#selector(done)];
self.navigationItem.rightBarButtonItem = doneButton;
[doneButton release];
self.tableView.allowsSelection = NO;
self.tableView.allowsSelectionDuringEditing = NO;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
// Return the number of rows in the section.
return 3;
}
// Customize the appearance of table view cells.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *DetailCellIdentifier = #"DetailCell";
ListDetailCell *cell = (ListDetailCell *)[tableView dequeueReusableCellWithIdentifier:DetailCellIdentifier];
if (cell == nil) {
[[NSBundle mainBundle] loadNibNamed:#"ListDetailCell" owner:self options:nil];
cell = listDetailCell;
self.listDetailCell = nil;
// Configure the cell...
// list name
if (0 == indexPath.row) {
cell.label.text = #"List Name";
cell.textField.text = self.selectedList.listName;
cell.textField.placeholder = #"Name";
}
// Detail 1
if (1 == indexPath.row) {
cell.label.text = #"Detail 1";
cell.textField.text = selectedList.detail1;
cell.textField.placeholder = #"Detail 1";
}
// Detail 2
if (2 == indexPath.row) {
cell.label.text = #"Detail 2";
cell.textField.text = selectedList.detail2;
cell.textField.placeholder = #"Detail 2";
}
}
return cell;
}
- (void)done {
[self.listDetailCell resignFirstResponder];
[self.navigationController popViewControllerAnimated:YES];
}
The ivars label and textField are declared in ListDetailCell, which is the table cell nib I mentioned earlier.
Not sure if I've got your problem correct or if my answer is the best method, but it's what I did recently. I have a TableViewController and UITableView which display a series of custom cells for editing data. This is basically replicating what I've seen a number of other applications do to create data editing screens.
Each of the custom cells has a UITextField. When the user finishes editing a cell, the UITextField triggers a message to a UITextFieldDelegate. So I added the UITextFieldDelegate protocol to the TableViewController and when setting up the custom cell in cellForRowAtIndexPath, I set the TableViewController as the UITextFields delegate. Then when the user finishes editing the end editing message is sent and I can then get the value from the UITextField and store it back in the managed entity object.
I apologise, but I don't have access to my code right now or I'd cut and paste an example for you.
Anyway, some things to watch out for:
In the code for the delegate message you need to first identify the UITextField that has triggered the call. The best way to do this is to set the Tag property on the UITextField when you create the UITableCell that contains it. Then in the delegate method you can use a switch statement to select which entity field to store the value in.
Getting ride of the keyboard when a user taps on a non-editable area of a UITableView can be tricky. You need to store a list of objects that can have a keyboard, and when a click happens, loop through them and do the resign first responder to remove the keyboard from the display.
tapping a save button on the navigation bar or something else outside of the UITableView will not remove the keyboard or resign the first responder, so the delegate of the field currently being edited does not get called. You need to add code to trigger the save sequence.
If you have any UITextView's they use a different delegate.
To add the delegate you will need to do something like this (taken from your code above):
#interface MyTableViewController : UITableViewController <UITextFieldDelegate>
....
if (0 == indexPath.row) {
cell.label.text = #"List Name";
cell.textField.text = self.selectedList.listName;
cell.textField.placeholder = #"Name";
cell.textField.delegate = self; // Setting controller as text field delegate.
cell.textField.tag = 1; // Really should use an enum here for clarity.
}
....
-(void) textFieldDidEndEditing:(UITextField *) textField {
switch(textField.tag) {
case 1: //Again with the enum.
// Save field 1.
entity.someProperty = textField.text;
....
}
}
This is for dealing with a number of text fields. Another solution I found was to store the changed values in a dictionary and only update the entity when the user taps save. With my solution above, you would also have to reset the entities properties if the user cancels. So it's horse for courses.

navigating between textfields of uitableview

I am making an coredata application in which I was instructed to make a tableview (filling form) with textfield in each row which I did successfully. Then I made a toolbar with previous and next button and add to textField's inputAccsoryView for navigating between the textfields of each cell. One textfield per row . I am also able to do the magic with previous and next button methods . Now the problem :
Suppose I have 3 sections . In first section 4 rows . In second section 6 rows and in the last section 1 row . Now when I start from the 1st row and presses next till the 11th row it is working properly but then I press next nothing happens as the first row is dequeued I get "nil" for it . The code I am using :
configuring the cell
if (cell == nil) {
NSLog(#"tag inside:%i",tag);
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1
reuseIdentifier:identifier] autorelease];
UITextField *theTextField = [[UITextField alloc] initWithFrame:CGRectMake(120, 10, 170, 25)];
theTextField.textAlignment = UITextAlignmentRight;
theTextField.delegate = self;
theTextField.tag = tag;
theTextField.inputAccessoryView = [self configureTheToolBar];
[theTextField setKeyboardAppearance:UIKeyboardAppearanceAlert];
[cell.contentView addSubview:theTextField];
[theTextField release];
}
cell.textLabel.text = rowLabel;
UITextField *textField = (UITextField *)[cell.contentView viewWithTag:tag];
textField.text = rowValue
I am providing tags for textfield like 601 ,602 etc .
Then getting the currentTextField tag
- (void)textFieldDidBeginEditing:(UITextField *)textField{
currentTextField = textField.tag;
}
then the next method
if(currentTextField == 611){
currentTextField = 601;
} else{
currentTextField = currentTextField + 1;
}
NSLog(#"current text fiedld = %i",currentTextField);
NSLog(#"text ; %#",[self cellForTag:currentTextField].textLabel.text);
UITextField *firstResponderField = (UITextField *) [[self cellForTag:currentTextField].contentView viewWithTag:currentTextField];
[firstResponderField becomeFirstResponder];
and the cellForTag method :
-(UITableViewCell *)cellForTag:(NSInteger)tag{
UITableViewCell *cell;
switch (tag) {
case 601:{
NSUInteger onlyRow[] = {0, 0};
NSIndexPath *onlyRowPath = [NSIndexPath indexPathWithIndexes:onlyRow length:2];
//[self.tableView scrollToRowAtIndexPath:onlyRowPath atScrollPosition:UITableViewScrollPositionTop animated:YES];
cell = [self.tableView cellForRowAtIndexPath:onlyRowPath];
break;
}
return cell
this code is working partially as once off screen cells get out of memory I can't reach textfield in that cell and next button stops working till the currentTextfield value reaches to any visiblerow . What could be the solution . my stack is over flowed. :)
Ok this thread does the trick but I don't know how . can anyone explain me how ?
Once a cell goes off screen it will no longer be accessible. Because of this you might want to grab the values of your text fields as soon as the user has finished editing them by wiring up the UIControlEventEditingDidEnd event:
[myTextField
addTarget:self
action:#selector(myTextChanged:)
forControlEvents:UIControlEventEditingDidEnd];
In your myTextChanged function you can store the values away, perhaps in a dictionary if you want to keep them keyed against your tags.