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
}
}
Related
-(void)sendcomment:(UIButton *)sender{
NSLog(#" send commment server");
thirdviewcellTableViewCell *dja =[[thirdviewcellTableViewCell alloc] init];
NSString *vga = [[NSString alloc] init];
vga=dja.celltext;
NSLog(#"comment value is %#",vga);
NSLog(#"comment cell value is %#",dja.celltext);
}
-(void)sendcomment:(UIButton *)sender
{
UIButton *button = (UIButton*)sender;
NSIndexPath *indexpath = [NSIndexPath indexPathForRow:sender.tag inSection:0];
UITableViewCell *cell = (UITableViewCell*)[tblView cellForRowAtIndexPath: indexpath];
// If you have text field
UITextField *textfiled = (UITextField *)[cell.contentView viewWithTag:yourtextfeildtagvlaue];
// NSString *vga = textfiled.text
}
If the cell hold the button, you can do this:
-(void)sendcomment:(UIButton *)sender{
// 1. get the position the button you clicked
CGPoint correctedPoint = [sender convertPoint:button.bounds.origin toView:self.tableView];
// 2. find the cell via the position
NSIndexPath *indexPath = [self.tableView indexPathForRowAtPoint:correctedPoint];
thirdviewcellTableViewCell *cell = [self.tableView cellForRowAtIndexPath:indexPath];;
// 3. then you can get the TextField value
NSString *textFieldValue = [[NSString alloc] init];
textFieldValue = cell.celltext;
}
you get your cell using cell's hierarchy
-(void)sendcomment:(UIButton *)sender
{
UIButton *button = (UIButton*)sender;
NSIndexPath *indexpath = [NSIndexPath indexPathForRow:sender.tag inSection:0];
UITableViewCell *cell = (UITableViewCell*)sender.superview.superview;// get cell using view hierarchy
// If you have text field
UITextField *textfiled = (UITextField *)[cell.contentView viewWithTag:yourtextfeildtagvlaue];
// NSString *vga = textfiled.text
}
*
In your action method you can Find Your TextField as below :
You can find IndexPath from SuperView.
See below Code.
UIView *superView = btn.superview;
while (![superView isKindOfClass:[UITableViewCell class]]) {
superView = superView.superview;
}
CustomCell *cell = (CustomCell *)superView;
You will get UITextField object from cell.yourTextFeld .
And you can use TextField as per your requirement.
I have a UITableView and each UITableViewCell has a textfield and UILabel. How can I update the label string on a cell when the textfield in the corresponding cell has been edited.
I tried something like this, but it's not working
UITextField *myTextField = (UITextField *)[cell viewWithTag:100];
UILabel *myLabel = (UILabel *)[cell viewWithTag:101];
[myTextField addTarget:self action:#selector(textFieldEdited:event:) forControlEvents:UIControlEventEditingDidEnd];
- (void)textFieldEdited:(UITextField*)textfield event:(UIEvent*)event
{
NSIndexPath* indexPath = [myTable indexPathForRowAtPoint:
[[[event touchesForView:textfield] anyObject]
locationInView:cartTable]];
NSLog(#"%d",indexPath.row);
UILabel *myLabel = (UILabel *)[[myTable cellForRowAtIndexPath:indexPath] viewWithTag:101];
[myLabel setText:#"44"];
}
But it didn't work.
Try this.. This may help you.
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
UITableviewCell *currentCell = (UITableviewCell *) [textField superView];
UILabel *label = [currentCell viewWithTag:YOURLABELTAG];
label.text = [NSString stringWithFormat:#"%#%#",textField.text,string];
return YES;
}
Note: don't forget to set textFieldDelegate and set label Tag
In your textFieldEdited: update your data source and reload the table data
[myTable reloadData];
Handle the changes then in your cellForRowAtIndexPath: method to return the updated cell.
CGPoint location = [tableview convertPoint:yourtextfield.frame.origin fromView:yourtextfield.superview];
NSIndexPath *indexPath = [tblItemList indexPathForRowAtPoint:location];
UITableview *cell = [self.table cellForRowAtIndexPath:indexPath];
for (UIView *subView in cell.subviews)
{
if ([subView isKindOfClass:[UILabel class]])
{
[(UILabel *) subView setText:#"Hearts"];
}
}
for Finding indexpath of cell is simple one line code
NSIndexPath *indexPath = [tableView indexPathForCell:cell];
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;
}
First of all I know there are several more titles looks like same. I have checked them but what I ask is a specific problem.
What I want to do: Created Login table which includes email and password. UITextfields in UITableView. Just want to take those data into some NSString variable pointers for further process.
I have a Custom Cell Class named CustomCell, as below:
#import "CustomCell.h"
#import "QuartzCore/QuartzCore.h"
#implementation CustomCell
#synthesize textfield;
- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
if (self) {
textfield = [[UITextField alloc] initWithFrame:CGRectMake(5, 7, 160, 20)];
textfield.textColor = [UIColor blackColor];
textfield.font = [UIFont systemFontOfSize:14];
[self.contentView addSubview:textfield];
}
return self;
}
- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
[super setSelected:NO animated:NO];
// Configure the view for the selected state.
}
- (void)dealloc {
[super dealloc];
}
#end
And I have ViewController where I use the custom table below you may see my tableView cellForRowAtIndex method.
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
static NSString *CellIdentifier = #"Cell";
[tableView.layer setCornerRadius:10.0];
CustomCell *cell = (CustomCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[CustomCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];}
switch (indexPath.row) {
case 0:
cell.textfield.placeholder = #"Email";
break;
case 1:
cell.textfield.placeholder = #"Password";
cell.textfield.tag = 0;
break;
}
return cell;
}
And lastly below in same class I am trying to read email & password with
NSIndexPath *indexPath = [NSIndexPath indexPathForRow:1 inSection:0];
CustomCell *cell = [tableView cellForRowAtIndexPath:indexPath]; // ERROR!!!
NSString *userpassword = cell.textfield.text;
Error: Unknown receiver type tableView:
Attention in same line:
ClassMethod +cellforrowatindexpath not found return type defaults to id.
Did you see what I am doing wrong?
NSIndexPath *indexPath = [NSIndexPath indexPathForRow:1 inSection:0];
CustomCell *cell = (CustomCell *)[tableView cellForRowAtIndexPath:indexPath]; // Add This instead
NSString *userpassword = cell.textfield.text;
Yu can't use tableView outside the datasource and delegate methods. What's the name of your tableview in your interface definition? Use that one.
Is your ViewController inheriting UIView or UITableViewController? This is assuming you are calling tableView on the same controller as the Table View.
I have placed UITextField over each UITableView cell. While scrolling the UITableView cell the text over the UITextField is getting lost. What can I do to prevent this?
You need to get track of an NSMutableArray which stores the text field values. Make the initial values #"". When you insert the text field into the cell, get the value from the appropriate value in the array.
If the text field exits, do something like this to store your text field values in the array:
-(BOOL)textFieldShouldReturn:(UITextField *)textField
{
UITableViewCell *cell=(UITableViewCell *)textField.superview;
NSIndexPath *indexPath = [table indexPathForCell:cell];
NSMutableArray *rows=[sectionArray objectAtIndex:[indexPath section]];
NSString *string=[rows objectAtIndex:indexPath.row];
string=textField.text;
[[sectionArray objectAtIndex:[indexPath section]]replaceObjectAtIndex:[indexPath row] withObject:string];
[textField resignFirstResponder];
return YES; }
- (UITableViewCell *)tableView:(UITableView *)tableView
cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *myCellId= #"myDateCell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:myCellId];
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault
reuseIdentifier: myCellId];
CGRect textFieldFrame;
CGRect labelFrame;
labelFrame = CGRectMake(cell.frame.origin.x+45, cell.frame.origin.y+3, labelWidth, cell.bounds.size.height-5);
textFieldFrame = CGRectMake(cell.frame.origin.x+labelWidth+15, cell.frame.origin.y+12 ,cell.
UILabel *label = [[UILabel alloc]initWithFrame:labelFrame];
UITextField *textField = [[UITextField alloc]initWithFrame:textFieldFrame];
textField.keyboardType=UIKeyboardTypeNumberPad;
textField.delegate = self;
textField.text = [[sectionArray objectAtIndex:[indexPath section]]objectAtIndex:[indexPath row]];
[cell addSubview:label];
[cell addSubview:textField];
cell.selectionStyle = UITableViewCellSelectionStyleNone;
return cell;
}
It's because as you scroll, the tableview dynamically creates and destroys cells to save on memory. If you still haven't figured out a way to solve this problem let me know and I will help you find a solution