Retrieve the insert user data from UItextField in Table View - objective-c

I create UITableView and add textfields to the content of cell to allow the user to enter his data and when the user end editing the text field I want to save the insert data in text field, but the problem is the UITextField retrieve null.
At cellForRowAtIndexPath I added
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil)
{
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
TextField1 = [[UITextField alloc] initWithFrame:CGRectMake(0, 0, 80,45)];
TextField2 = [[UITextField alloc] initWithFrame:CGRectMake(83, 0, 70,45)];
TextField3 = [[UITextField alloc] initWithFrame:CGRectMake(156, 0, 70,45)];
[TextField1 setDelegate:self];
[TextField2 setDelegate:self];
[TextField3 setDelegate:self];
TextField1.userInteractionEnabled=YES;
TextField1.enabled=YES;
TextField2.userInteractionEnabled=YES;
TextField2.enabled=YES;
TextField3.userInteractionEnabled=YES;
TextField3.enabled=YES;
[cell.contentView addSubview:TextField1];
[cell.contentView addSubview:TextField2];
[cell.contentView addSubview:TextField3];
return cell;
}
- (void)textFieldDidEndEditing:(UITextField *)textField{
NSString*VisitDateStr=[NSString stringWithFormat:#"%#",TextField1.text];
NSLog(#"%#",VisitDateStr);
}
The result (null).
How I can retrieve the user details which insert on textfield at the tableview?
Thanks, I appreciate any help.

Use the following code :
-(void)textChanged:(UITextField *)textField
{
NSString*VisitDateStr=[NSString stringWithFormat:#"%#",textField.text];
}
Use this delegate method:
Set delegate to self in cellforRowAtIndexPath
- (void)textFieldDidEndEditing:(UITextField *)textField;

try out this
-(void)textChanged:(UITextField *)textField
{
NSString*VisitDateStr=[[NSString alloc]init];
VisitDateStr=[NSString stringWithFormat:#"%#",TextField1.text];
}
or
-(void)textChanged:(UITextField *)textField
{
NSString*VisitDateStr=[[NSString alloc]init];
VisitDateStr=TextField1.text;
NSLog(#"%#",VisitDateStr);
}

Related

save UITextField in UITableViewCell

I've searched for a while but I cannot find the answer to my problem. I have a custom UITableViewCell that has a UITextField.
I use the cellForRowAtIndexPath to create a table with cells. In the UIView that takes the user to the UITableViewController the user specifies how many rows they wish to have. When the user goes back and forth the UITextField text seems to be lost and does not appear in the UITableView
Basically, I cannot get the text that the user inputs to save. Here is my code:
CustomeCell.m
#implementation customCell
#synthesize aTextField;
-(void)layoutSubviews{
aTextField = [[UITextField alloc] initWithFrame:CGRectMake(7, 3, 65, self.contentView.bounds.size.height-6)];
[aTextField setContentVerticalAlignment:UIControlContentVerticalAlignmentCenter];
[aTextField setAutocorrectionType:UITextAutocorrectionTypeNo];
[aTextField setAutocapitalizationType:UITextAutocapitalizationTypeNone];
[aTextField setBorderStyle:UITextBorderStyleRoundedRect];
aTextField.textAlignment = NSTextAlignmentCenter;
aTextField.keyboardType = UIKeyboardTypeNumberPad;
[self.aTextField addTarget:self action:#selector(aTextFieldEditing:) forControlEvents:UIControlEventEditingChanged];
[aTextField setAutocapitalizationType:UITextAutocapitalizationTypeWords];
[aTextField setPlaceholder:#"begin"];
[self.contentView addSubview:aTextField];
}
-(void)aTextFieldEditing:(UITextField*)textField{
aTextField.text = textField.text;
}
#end
from TableViewController.m
-(UITableViewCell *) tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
static NSString *CellIdentifier = #"Cell";
customCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[customCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
return cell;
}
Any recommendations? Thank you in advance.

How to directly edit a row in a UITableView, without entering in Editing Mode

In a UITableView, I would like to be able to edit the cell.textLabel.text property of the row, when such row is touched.
In other terms, I would like to be able to edit the row directly touching it, instead of entering into edit mode.
How can I implement this?
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"Cell";
CMTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[CMTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
}
int tagsCount = [[currentComic Tags] count];
[[cell textField] setTag:[indexPath row]];
[[cell textField] setText:[tagsDisplayName objectAtIndex:[indexPath row]]];
return cell;
}
And this is the subclass CMTableViewCell:
...
-(void)viewDidLoad
{
textField = [[UITextField alloc] init];
[textField setFrame:CGRectMake(5,5,50,400)];
[self addSubview:textField];
}
Add UITextField without borders as subview. Before adding it to subview - set Tag nubmer to UITextField from indexPath.row at tableview cellForRowAtIndexPath method. When user entered data - save it with UITextFieldDelegate methods when "Return" button was pressed. Unfortunately I can't give you code, because right now I'm on Windows. Home this will help
UPDATE: Tag number needed to change data in your DataSource. When you pressed "Return" button in your UITextField, you can save changed by getting UITableViewCell by this tag number from UITextField.
It's simple:
- (void)tableView:(UITableView *)tableView
didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
[tableView deselectRowAtIndexPath:indexPath animated:YES];
UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
cell.textLabel.text = #"...";
}
UPDATE
- (void)tableView:(UITableView *)tableView
didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
UITextField *textField = [[UITextField alloc] init];
[textField setTag:[indexPath row]];
[tagsTableView addSubview:textField];
FreeAndNil(textField);
}

UITextView in a grouped UITableView's cell

Please, explain me: how to programmatically create an UITextView in UITableViewCell (UITableView has a grouped style). The text view size should be equal to size of the cell.
The UITextView has a gap between its text and its border (top left corner), so I need the correct coordinates to properly place the text view on a cell.
UPDATE: I've solved my question. See my self-answer below. Thanks!
It was simple:
textView.frame = CGRectMake(0, 0, cell.contentView.frame.size.width, cell.contentView.frame.size.height);
Here you go:
- (UITableViewCell *) tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:#"Prototype Cell"];
UITextField *textfield;
if (!cell)
{
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle
reuseIdentifier:#"Prototype Cell"];
textfield = [[UITextField alloc] init];
[cell.contentView addSubview:textfield];
textfield.tag = TEXT_FIELD_TAG; //Suppose you defined TEXT_FIELD_TAG someplace else
}
else
{
textfield = [self.view viewWithTag: TEXT_FIELD_TAG];
textfield.frame = cell.frame;
}
return cell;
}
Set "contentInset" of UITextView
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *sCellIdentifier = #"cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:sCellIdentifier];
if (!cell)
{
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDetail
reuseIdentifier:sCellIdentifier];
}
UITextView *textView = [UITextView alloc]init];
textView.contentInset = UIEdgeInsetsMake(-8,-8,-8,-8); //Change according ur requirement
textView.frame = cell.frame;
[textView setUserInteractionEnabled:FALSE];
[textView setBackgroundColor:[UIColor clearColor]];
[cell.contentView addSubView:textView];
return cell;
}

Looping through subview of a custom cell in didSelectRowAtIndexPath to get UITextField text

i know im asking a very common question. but after going through a lots of similar questions and articles im not able understand
How can i access UITextfield inside UITableViewCell inside didSelectRowAtIndexPath delegate method.
im posting my code below. i may b not going with expected way although..
i want to make textfield (of selected row) [becomeFirstResponder] in didSelectRowAtIndexPath
so that i can display uiPicker below to this textfield. Please help me out with this.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
if(self.isTableForHospital)
{
UIView *myview=[self createRowViewForHospitalTable:[customArray objectAtIndex:indexPath.row]];
myview.tag=indexPath.row;
[cell addSubview:myview];
cell.selectionStyle=UITableViewCellSelectionStyleNone;
}
else
{
cell.textLabel.text=[customArray objectAtIndex:indexPath.row];
}
}
return cell;
}
-(UIView *)createRowViewForHospitalTable:(NSString *)rowText
{
UIView *hospital_row_view=[[UIView alloc]init];
UILabel *lblRowText=[[UILabel alloc]initWithFrame:CGRectMake(0, 0, 300, 50)];
lblRowText.text=rowText;
txtDepartment=[[UITextField alloc]initWithFrame:CGRectMake(310, 0, 185, 30)];
//...rest styling for textfield goes here..
txtRole=[[UITextField alloc]initWithFrame:CGRectMake(495, 0, 185, 30)];
[hospital_row_view addSubview:lblRowText];
[hospital_row_view addSubview:txtDepartment];
[hospital_row_view addSubview:txtRole];
return hospital_row_view;
}
- (void)textFieldDidEndEditing:(UITextField *)textField { }
- (void)textFieldFinished:(id)sender{
NSString *value=#"sa";
[sender resignFirstResponder];
}
// Override to support conditional editing of the table view.
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath
{
return YES;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cellSelected = [tableView cellForRowAtIndexPath: indexPath];
UIView *myview=[cellSelected.contentView viewWithTag:indexPath.row];
for(UIView *item in [myview subviews])
{
if([item isKindOfClass:[UITextField class]])
{
[item becomeFirstResponder];
}
}
}
In terms of iterating through the cell subviews and getting the UITextField, I think I have your answer.
I found this on another thread here, and modified it to be more generic:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
UITextField* textField = [[UITextField alloc] init];
UITableViewCell* cell = [tableView cellForRowAtIndexPath:indexPath];
UIView* subview = [[cell.contentView subviews] lastObject]; // Make sure your UITextField really *is* the last object you added.
if ([subview isKindOfClass:[UITextField class]]) {
textField = (UITextField*)subview;
}
[textField becomeFirstResponder];
}
...where "textField" is the placeholder for your actual text field object.
Hope this helps!
I don't think you need to alloc init the textfield, for it will ultimately be pointing to the cell's textfield (if any).
I believe you are allocating it unnecessarily.

iOS tableview cell handling

I have this code, where "lineaRedToday" is an UIImageView:
- (void)viewDidLoad {
[super viewDidLoad];
lineaRedToday = [UIImage imageNamed:#"line4.png"];}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *MyIdentifier = #"MyIdentifier";
MyIdentifier = #"tblCellView";
TableViewCell *cell = (TableViewCell*)[tableView dequeueReusableCellWithIdentifier:MyIdentifier];
if(cell == nil) {
[[NSBundle mainBundle] loadNibNamed:#"TableViewCell" owner:self options:nil];
cell = tblCell;
}
cell.selectionStyle = UITableViewCellSelectionStyleNone;
[[lineDay objectAtIndex:currentTodaylineRed -1] setImage:lineaRedToday];
return cell;
- (IBAction) change{
lineaRedToday = [UIImage imageNamed:#"linea.png"];
[lineDay setImage:lineaRedToday];
[myTableView reloadData];
}
It is a UITableView with 15 custom UITableViewCell and I want to change the image at an UIImageView, this ImageView is "lineDay", lineDay is present in all cells, and I want change its image at all cells, but the IBAction "change" change the UIImageView only in the last cell and not at all....why?
yes it will change the image in last cell because it has the reference of only last cell, if you want to do this then when you are creating cell in cellForRowAtIndexPath check for a BOOL and set image according that BOOL. Also in IBAction change that BOOL value and call reloadData on table view to reload it. As -
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"Test";
UITableViewCell *cellView = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cellView == nil)
{
cellView = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier] autorelease];
UIImageView *bgImgView = [[UIImageView alloc] initWithFrame:CGRectMake(0.0f, 0.0f, 320.0f, 85.0f)];
[bgImgView setTag:1];
[bgImgView setBackgroundColor:[UIColor clearColor]];
[cellView.contentView addSubview:bgImgView];
[bgImgView release];
}
UIImageView *imgView = (UIImageView*)[cellView viewWithTag:1];
if (buttonPressed)
{
[imgView setImage:[UIImage imageNamed:#"listing_bg"]];
}
else
{
[imgView setImage:[UIImage imageNamed:#"featured_listing_bg"]];
}
return cellView;
}
- (IBAction) change{
buttonPressed = !buttonPressed;
[myTableView reloadData];
}
First, lineaRedToday is an UIImage, not an UIImageView. The first is an image, the second an object in the view hierarchy.
Then, in your code
if(cell == nil) {
[[NSBundle mainBundle] loadNibNamed:#"TableViewCell" owner:self options:nil];
cell = tblCell;
}
you use tblCell. I cannot see what this is. You should assign something from what you were just loading from the nib.
I don't know what lineDay is exactly, but a single view (i.e. UIImageView) can only be present in one cell, not in many. If you change the image (lineaRedToday), you still have to set this new image in your views. There should be something like
cell.yourImageView.image = linaRedToday;
when configuring the cell.