Seeing a TextField as a subview of a UICell - ios

I have added a TextField to a UICell in order to allow a user to change the TextLabel of said cell. Everything works fine in that the keyboard appears and the TextLabel changes to the correct value when I press return. I can't, however, SEE the text as it is being edited. Here is my code:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
[self.tableView scrollToRowAtIndexPath:indexPath atScrollPosition:UITableViewScrollPositionTop animated:YES];
UITableViewCell *aCell = [self.tableView cellForRowAtIndexPath:indexPath];
NSString *aCategory = aCell.textLabel.text;
[[aCell detailTextLabel] setText:aCategory];
[aCell setEditing:YES animated:YES];
UITextField *userText =[[UITextField alloc] init];
userText.tag = indexPath.row;
[aCell addSubview:userText];
NSArray *test =[aCell subviews];
NSLog(#"I have %d many subviews",[test count]);
userText.alpha=1.0;
[userText setDelegate:self];
userText.autocorrectionType = UITextAutocorrectionTypeNo;
[aCell bringSubviewToFront:userText];
[userText becomeFirstResponder];
}
I would like to do it without custom cells if possible. Thanks in advance.

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
[self.tableView scrollToRowAtIndexPath:indexPath atScrollPosition:UITableViewScrollPositionTop animated:YES];
UITableViewCell *aCell = [self.tableView cellForRowAtIndexPath:indexPath];
NSString *aCategory = aCell.textLabel.text;
[[aCell detailTextLabel] setText:aCategory];
UITextField *userText = [[UITextField alloc] initWithFrame:aCell.textLabel.frame];
userText.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;
userText.font = aCell.textLabel.font;
userText.text = aCell.textLabel.text;
aCell.textLabel.text = nil;
userText.tag = indexPath.row;
[aCell addSubview:userText];
[userText setDelegate:self];
userText.autocorrectionType = UITextAutocorrectionTypeNo;
[aCell bringSubviewToFront:userText];
[userText becomeFirstResponder];
}
- (void)textFieldDidEndEditing:(UITextField *)textField
{
NSIndexPath *indexPath = [NSIndexPath indexPathForRow:textField.tag inSection:0];
UITableViewCell *aCell = [self.tableView cellForRowAtIndexPath:indexPath];
aCell.textLabel.text = textField.text;
[textField removeFromSuperview];
}
- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
[textField resignFirstResponder];
return YES;
}

Related

scrollToRowAtIndexPath is not working fine?

I am using a tableview with autolayet corners set to (0,0,0,0).I had a custom cell set on the table view.my issue is the scrollToRowAtIndexPath is not working fine.my cellForRowAtIndexPath methods is looking like this
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
RTableViewCell *cell = (RTableViewCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell==nil)
{
for (UIView *currentView in tableView.subviews) {
if ([currentView isKindOfClass:[UIScrollView class]]) {
((UIScrollView *)currentView).delaysContentTouches = NO;
break;
}
}
UIView *myBackView = [[UIView alloc] initWithFrame:cell.frame];
myBackView.backgroundColor =[UIColor colorWithRed:216/255.0 green:231/255.0 blue:244/255.0 alpha:1.0];
cell.selectedBackgroundView = myBackView;
return cell;
}
i had dynamic number of sections and dynamic number of rows inside that.
and in my viewDidAppear i am calling like this
dispatch_async(dispatch_get_main_queue(), ^{
//int indexValue = (int)[sections indexOfObject:string];
[rtable reloaddata];
NSIndexPath *rowIndexPath = [NSIndexPath indexPathForRow:0 inSection:6];
[self.rtable scrollToRowAtIndexPath:rowIndexPath atScrollPosition:UITableViewScrollPositionTop animated:NO];
});
anybody knows where i am going wrong?
Try below code in viewWillAppear or viewDidLayoutSubviews:
[rtable reloaddata];
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.1 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
NSIndexPath *rowIndexPath = [NSIndexPath indexPathForRow:0 inSection:6];
[self.rtable scrollToRowAtIndexPath:rowIndexPath atScrollPosition:UITableViewScrollPositionBottom animated:YES];
});

how to change the textfield value when the button is clicked in uitableviewcell iOS

I'm developing a shopping app, in which i'm implementing a shopping cart. In my app, i need to increase the product quantity when the plus button is clicked and reduce the product quantity when the minus button is clicked. Here my problem is, when i click the plus button, all text field value is changing in the tableviewcell. Help me,below is
plus button action method
-(IBAction)plusBtn:(UIButton*)plus
{
[self.tbleView beginUpdates];
UITableViewCell *clickedCell = (UITableViewCell *)[[plus superview] superview];
NSIndexPath *clickedButtonIndexPath = [self.tbleView indexPathForCell:clickedCell];
plus.tag = clickedButtonIndexPath.row;
quantity.tag = clickedButtonIndexPath.row;
_curr =_curr+1;
quantity.text = [NSString stringWithFormat:#"%d",_curr];
[self.tbleView endUpdates];
[self.tbleView reloadData];
}
like this
getting..
can u check this functionality. it will useful for you. i hope it.
- (void)viewDidLoad
{
[super viewDidLoad];
quantity =[NSMutableArray new];
occu_list = [[NSArray alloc] initWithObjects:#"Occupation", #"two", #"three", #"four", #"five", #"six", #"seven", #"eight", nil];
for(int i=0;i<[occu_list count];i++)
{
[quantity addObject:#"0"];
}
click_textView=[[UIView alloc]init];
click_textView.frame=self.view.frame;
[self.view addSubview:click_textView];
[self tableviewone];
}
-(void)tableviewone
{
tbl_view = [[UITableView alloc] initWithFrame:self.view.frame style:UITableViewStylePlain];
tbl_view.delegate = self;
tbl_view.dataSource = self;
tbl_view.backgroundColor = [UIColor whiteColor];
tbl_view.layer.borderColor=[[UIColor orangeColor]CGColor];
tbl_view.layer.borderWidth=2.0f;
tbl_view.layer.cornerRadius=5.0f;
tbl_view.layer.masksToBounds=YES;
[click_textView addSubview:tbl_view];
}
-(IBAction)check_btn_action:(id)sender
{
UIButton *btntag=(UIButton*)sender;
NSLog(#"%li",(long)btntag.tag);
NSLog(#"%#",[quantity objectAtIndex:btntag.tag]);
int ad=[[quantity objectAtIndex:btntag.tag]integerValue];
ad=ad+1;
[quantity removeObjectAtIndex:btntag.tag];
[quantity insertObject:[NSString stringWithFormat:#"%i",ad] atIndex:btntag.tag];
[tbl_view reloadData];
}
Write action method for your button and then inside that just use this one line of code for setting your value to the textfield:-
self.yourTxtFld.text=#"yourString"
Don`t write the Button action in ViewController,
write it in the CustomCell class.
example:
in the CustomCell.m
- (IBAction)plusButtonDidClicked:(UIButton *)sender {
int i = [self.textField.text intValue];
i++;
self.textField.text = [NSString stringWithFormat:#"%d", i];
}
Then back to the ViewController.m
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
CustomCell *cell = [tableView dequeueReusableCellWithIdentifier:CustomCellIdentifier];
return cell;
}
It will be work well.
-(IBAction)plusBtn:(UIButton*)plus
{
UITableViewCell *clickedCell = (UITableViewCell *)[[plus superview] superview];
_curr=_curr+1;
UITextField *qty = (UITextField *) [clickedCell viewWithTag:90];
qty.text=[NSString stringWithFormat:#"%d",_curr];
}

UITextFiled don't show clearButton sometimes

I add a simple UITableView to my project,and every cell include a UITextField with clearButtonMode = UITextFieldViewModeAlways.
but strange issue occurs .when I reloadData,some textfield don't show clear button.
so I grab the part and build a sample and never find this issue.
I just wonder is some property has a effect to the textField.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *cellIdentifier = #"userIdentifier";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
if (!cell) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault
reuseIdentifier:cellIdentifier];
}
for (UIView *sv in cell.contentView.subviews) {
[sv removeFromSuperview];
}
UITextField *tf = [[UITextField alloc] initWithFrame:cell.bounds];
tf.text = [self.tb_data[indexPath.row];
tf.rightViewMode = UITextFieldViewModeAlways;
tf.clearButtonMode = UITextFieldViewModeAlways;
tf.tag = indexPath.row+100;
tf.delegate = self;
[[cell contentView] addSubview:tf];
return cell;
}
- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField
{
if (textField.tag >= 100) {
return NO;
}
return YES;
}
- (BOOL)textFieldShouldClear:(UITextField *)textField
{
if (textField.tag >= 100) {
[self.tb_data removeObjectAtIndex:textField.tag-100];
[self.tb beginUpdates];
[self.tb deleteRowsAtIndexPaths:#[[NSIndexPath indexPathForRow:textField.tag-100 inSection:0]] withRowAnimation:UITableViewRowAnimationLeft];
[UIView animateWithDuration:0.3
animations:^{
CGRect frame = self.tb.frame;
frame.size.height = 40*self.tb_data.count;
self.tb.frame = frame;
}];
[self.tb endUpdates];
[self.tb reloadData];
}
return YES;
}

uiTableViewCell Add row with and image and remove row with second tap

I've been trying to figuered this out for hours. I'm just plain old stuck here. What im trying to accomplish is basically inserting a row directly below the row just tapped in the tableview in addition i would like to add and image to the row and and make the image clickable to respond to its click event.
So here is my code.
I implemented (i belive) the nessesary methods to handle all the actions for the uitableview.
when the user taps the cell i handle that action by executing the following code.
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
if (debug==1) {
NSLog(#"running line 225%# '%#'", self.class, NSStringFromSelector(_cmd));
}
Locations *location = nil;
Locations *tempObject = [[Locations alloc]init];
//test to see if we are looking for the search box or if we are essentially looking from the main view controller.
if (self.searchDisplayController.active) {
location= [self.searchResults objectAtIndex:indexPath.row];
NSLog(#"location : %#" ,location.locationName);
} else {
location = [self.locations objectAtIndex:indexPath.row];
NSLog(#"location : %#" ,location.locationName);
//set the new indexpath to 1 more then before so that we can essetially add a row right below the actual tapped item.
NSIndexPath *newPath = [NSIndexPath indexPathForRow:indexPath.row + 1 inSection:indexPath.section];
indexPath = newPath;
[self.locations insertObject:tempObject atIndex:indexPath.row ];
[tableView insertRowsAtIndexPaths:#[indexPath] withRowAnimation:UITableViewRowAnimationBottom];
self.visibleCell =YES; //set this boolean variable so that we can add a specific row image to this var
// self.locations[0].isItVisible = YES;
}//ends the else statement.
[tableView deselectRowAtIndexPath:indexPath animated:YES];
}
the above code inserts an empty cell into my tableview.
however how can i set the cell so that its custom and not the same as the others. In other words my initial cells data-source are basically bound to an nsobject and a string property location-name. However when i go try to update the table cells in the above method i obviously cannot add an image into a string so I'm running in to a error.
so i tried to instead make the update on the
(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
by basically checking if a variable is set to true or false but that turned out to be buggy because even when i scroll this method gets called.
How should i go about doing this. I think i have to do it all in the didselectrowindexaspath method. But i cant figured out how to change the newly inserted cell to contain an image only.
Any help would be appreciated.
EDIT
here is what im doing to try to add the image under the cellforrowindexpath method.
if(self.visibleCell==YES){
UIImage *clkImg = [UIImage imageNamed:#"alarm_clock.png"];
cell.imageView.image = clkImg;
}
Im a noob so im not sure im doing this correctly.
EDIT
this is the full cellforatindexpath
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
if (debug==1) {
NSLog(#"running line 159 %# '%#'", self.class, NSStringFromSelector(_cmd));
}
// NSLog(#"cell for row at index path just got called");
//JAMcustomCell *myCell = [[JAMcustomCell alloc]init];
static NSString *CellIdentifier = #"ListPrototypeCell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
Locations * locations = [[Locations alloc]init];
//tableView.backgroundColor = [UIColor blackColor];
// NSLog(#"this is visible '%hhd'", locations.isItVisible);
if(self.visibleCell==YES){
UIImage *clkImg = [UIImage imageNamed:#"alarm_clock.png"];
cell.imageView.image = clkImg;
}
if (tableView == self.searchDisplayController.searchResultsTableView)
{
locations = [self.searchResults objectAtIndex:indexPath.row];
}
else{
locations = [self.locations objectAtIndex:indexPath.row];
}
cell.textLabel.text = locations.locationName;
cell.textLabel.textColor = [UIColor whiteColor];
//cell.backgroundColor =[UIColor blackColor];
// cell.backgroundColor =[UIColor colorWithPatternImage:[UIImage imageNamed:#"Graytbl.fw.png"]];
cell.backgroundView = [[UIImageView alloc]initWithImage:[UIImage imageNamed:#"blueTbl.fw.png"]];
cell.selectedBackgroundView = [[UIImageView alloc]initWithImage:[UIImage imageNamed:#"blueTbl.fw.png"]];
// UIFont *myFont = [ UIFont fontWithName: #"Oswald" size: 25.0 ];
// cell.textLabel.font = myFont;
cell.textLabel.font= self.MyFont;//[UIFont fontWithName:#"Oswald-Regular.ttf" size:15];
return cell;
}
Try this approach, I used your idea of Bool
#pragma mark - Table View Data Source
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return self.numberOfRows;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
if(self.visibleCell){
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:#"imageViewCell" forIndexPath:indexPath];//ListPrototypeCell
UIImageView *imageVIew = (UIImageView *)[cell viewWithTag:1];
[imageVIew setImage:[UIImage imageNamed:#"alarm_clock.png"]];
return cell;
}else{
return [tableView dequeueReusableCellWithIdentifier:#"ListPrototypeCell" forIndexPath:indexPath];
}
}
#pragma mark - Table View Delegate
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
[tableView deselectRowAtIndexPath:indexPath animated:YES];
if(!self.visibleCell){
self.numberOfRows++;
self.visibleCell = YES;
NSIndexPath *indexPathCell = [NSIndexPath indexPathForRow:indexPath.row + 1 inSection:0];
[self.tableView insertRowsAtIndexPaths:#[indexPathCell] withRowAnimation:UITableViewRowAnimationBottom];
}else{
self.numberOfRows--;
self.visibleCell = NO;
NSIndexPath *indexPathCell = [NSIndexPath indexPathForRow:indexPath.row + 1 inSection:0];
[self.tableView deleteRowsAtIndexPaths:#[indexPathCell] withRowAnimation:UITableViewRowAnimationTop];
}
}
I created a demo project for you.
I hope it helps

How to get UITableviewcell value on click on button

I want to get UITableviewCell value on click of Login button. When user press on Login button then i want to fetch first cell(Email) and second cell(password) value. Please help me.
Thanks!
Shailesh
Please try to use this one.I hope it may help you. But you should assign tag 1 to email and 2 to password text field.
-(IBAction)loginBtnPressed:(IBAction)sender
{
UITableViewCell *emailCell = [self.tblView cellForRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:0]];
UITableViewCell *passwordCell = [self.tblView cellForRowAtIndexPath:[NSIndexPath indexPathForRow:1 inSection:0]] ;//pass
UITextField *emailTxtFLd = (UITextField *)[emailCell viewWithTag:1];
UITextField *passwordTxtFLd = (UITextField *)[passwordCell viewWithTag:2];
NSLog(#"Email : %#",emailTxtFLd.text);
NSLog(#"Password : %#",passwordTxtFLd.text);
}
You do one thing, when you add textFiled as a subView to the cell, set a tag to the textFiled like as given,
[textField setTag:-1];
And when you want to get the text from the textFiled, follow the steps below.
// Getting email
CustomCell *tableCell = (CustomCell*)[myTableView cellForRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:0]];
UITextField *myTextField = (UITextField *)[tableCell viewWithTag:-1];
NSString *emailString = [myTextField text];
NSLog(#"~~~~ email: %#", emailString);
// Getting Password
tableCell = [myTableView cellForRowAtIndexPath:[NSIndexPath indexPathForRow:1 inSection:0]];
myTextField = (UITextField *)[tableCell viewWithTag:-1];
NSString *passwordString = [myTextField text];
NSLog(#"~~~~ password: %#", passwordString);
Hope this will help you :)
Since both cells are visible, you can just ask for them this way:
UITableViewCell *emailCell = [myTableView cellForRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:0] //email
UITableViewCell *passwordCell = [myTableView cellForRowAtIndexPath:[NSIndexPath indexPathForRow:1 inSection:0] //password
Then assuming both UITableViewCell have a property in their header exposed for the UILabel you can do:
emailCell.myLabel.text //Value of the text in the cell
If email and password has allocated then You can directly access them in your IIBAction method like that
in your .h file delcare this to email and password UItextfield
#interface ViewController : UIViewController
{
UITextField *email;
UITextField *pass;
}
Now alloc and add it in your cell
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
email = [[UITextField alloc] initWithFrame:CGRectMake(0, 0, 320, 140)];
pass = [[UITextField alloc] initWithFrame:CGRectMake(0, 0, 320, 140)];
}
// Table View Data Source methods
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return 2;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:nil];
if(cell==nil)
{
cell=[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
if(indexPath.row==0)
{
[cell addSubview:email];
}
if(indexPath.row==1)
{
[cell addSubview:pass];
}
return cell;
}
Now apply this IBAction on your login button
- (IBAction)loginBtnTouchUpInside:(id)sender
{
NSLog(#"email ==== %#",email.text);
NSLog(#"pass ==== %#",pass.text);
NSString *strEmail = email.text;
NSString *strPassword = pass.text;
}
I hope this is works for you
The easiest way is, save the textField value in a separate string in textField delegate like below.
-(void)textFieldDidEndEditing:(UITextField *)textField{
stringEmail = textField.text;
}
and use the string value for further process.Do it for both by setting tag.
Thank You.
-(void)ActivateLogin{
NSIndexPath *tmpIndexPath0 = [NSIndexPath indexPathForRow:0 inSection:0];
NSIndexPath *tmpIndexPath1 = [NSIndexPath indexPathForRow:1 inSection:0];
CustomCell *cellCopy0 =(CustomCell*) [tableViewName cellForRowAtIndexPath:tmpIndexPath0];
CustomCell *cellCopy1 =(CustomCell*) [tableViewName cellForRowAtIndexPath:tmpIndexPath1];
NSString *strEmail = #""; NSString *strPassword = #"";
strEmail = cellCopy0.tf.text;
strPassword = cellCopy1.tf.text;
if(![strEmail isEqualToString:#""] && ![strPassword isEqualToString:#""]){
... //Do what you want to do with the data
}
else{
//Alert user to enter credentials
}
}

Resources