Question is in the title ^^. How do you go about saving say 2 textFields in a custom tableView cell. Pretend the reuse identifier doesn't exist (however is someone could show me how to get around that then that would be awesome). Thanks for anyone that can help.
In your custom UITableViewCell class you can add textfields from an array of textfields.
ie, in the viewDidLoad you can insert two textfields of tag 110 and 111 in an array.
-(void)viewDidLoad
{
arryTextfields = [NSMUtableArray alloc]init];
for(int i=0;i<2;i++)
{
UITextField * textField = [[UITextField alloc]init];
NSMutableDictionary * dic =[NSMutableDictionary alloc]init]
[dic setObject:textField forKey:[NSString stringWithFormat:#"11%d",i]];
[arryTextfields addObject:dic];
[textField release];
[dic release];
}
}
In your tableview delegate "cellForRowAtIndexPath".
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath: (NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"Cell";
YourCustomTableviewCell * cell = [[YourCustomTableviewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
UITextField * textfield1 = [[cell.arryTextfields objectAtIndex:indexPath.row]objectForKey:#"110"];
UITextField * textfield2 = [[cell.arryTextfields objectAtIndex:indexPath.row]objectForKey:#"111"];
return cell;
}
Related
I have a tableview where user enters his social media profile urls. I want to get the value of that textfields on click of a button. The tableview is dynamic. I have attached the screenshot for reference. I am using Objective C to build up an iOs App and I am new to it. Any suggestions on how to do that? Thanks in advance.
[![This is the tableview with textfield in cell]
I have tried this :
NSMutableArray *Array = [NSMutableArray array];
for (int i=0; i < getMYurl.count; i++){
NSIndexPath *indexPath = [NSIndexPath indexPathForRow: i inSection: 0];
SHOWURLTableViewCell *cellS = [table cellForRowAtIndexPath:indexPath];
NSMutableDictionary *Dict = [NSMutableDictionary dictionary];
UITextField *TextName= (UITextField*)[cellS.contentView viewWithTag:indexPath.row];
}
And my cellForRowAtIndexPath Method :
- (UITableViewCell *)tableView:(UITableView *)tableView
cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
cellS = (SHOWURLTableViewCell *) [tableView dequeueReusableCellWithIdentifier:#"cell"];
// cellS.labelDesc.text = mergeName;
NSString *urlText = [[getMYurl valueForKey:#"url"]objectAtIndex:indexPath.row];
cellS.labelUrl.text = urlText;
NSString *urlName = [[getMYurl valueForKey:#"url_name"]objectAtIndex:indexPath.row];
// [urlMap setValue:mergeName forKey:urlText];
cellS.urlNameET.tag = 100;
}
You should have to store text of textfiled in any array variable (index wise) after end typing. Because tableview destroy the tableview cell while that was not showing into a screen.
- (void)textFieldDidEndEditing:(UITextField *)textField{
UITableViewCell *cell = (UITableViewCell*) textField.superview.superview;
NSIndexPath *txtIndPath = [self.tblPendingDeliveryData indexPathForCell:cell];
[myMutableArray insertObject:textField.text atIndex:txtIndPath.row];
}
Don't forgot to set delegate to textfield
I want to select more then one rows from UIPickerView and place in UITextField . Language used in my project is objective c . I can't understand logic
As much I have understood what you want to achieve is you want to select more than one item from a list and show it in a UITextField. For that you can do this:
At first Drag and drop a UITableView into your storyboard and connect datasource and delegates.
Drag an UITableViewCell into your tableview and give the reuserIdentifier as TableCell.
Drag an UILabel and an UIImageView onto your tableCell. Give tags to your label and imageView as 888 and 999. Make the imageview like a small box and place it just beside your label.
Take an array for showing the list you want to show user to select items and populate the UITableView with it.
Take another array in which you want to store the selected items.
Just under your implementation declare two arrays like this:
#implementation ViewController {
NSMutableArray *arrItems;
NSMutableArray *arrSelectedItems;
}
Now in your viewDidLoad populate your item array.
- (void)viewDidLoad {
[super viewDidLoad];
arrItems = [[NSMutableArray alloc]init];
for (int i = 0; i < 10; i++) {
NSMutableDictionary *dict = [[NSMutableDictionary alloc]init];
[dict setObject:[NSString stringWithFormat:#"Item%d",i+1] forKey:#"itemName"];
[dict setObject:#"0" forKey:#"isSelected"];
[arrItems addObject:dict]
}
arrSelectedItems = [[NSMutableArray alloc]init];
}
Now implement the DataSource and Delegate methods of UITableView.
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return arrItems.count;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:#"TableCell"];
NSMutableDictionary *dict = [arrItems objectAtIndex:indexPath.row];
UILabel *lblItemName = (UILabel *)[cell.contentView viewWithTag:888];
lblItemName.text = [dict valueForKey:#"itemName"];
UIImageView *imageView = (UIImageView *)[cell.contentView viewWithTag:999];
if ([[dict valueForKey:#"isSelected"]isEqualToString:#"0"]) {
imageView.backgroundColor = [UIColor redColor];
} else {
imageView.backgroundColor = [UIColor greenColor];
}
return cell;
}
Now implement the multiple selection login in didSelectRowAtIndexPath like below:
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
NSMutableDictionary *dict = [arr objectAtIndex:indexPath.row];
if ([arrSelectedItems containsObject:dict]) {
[arrSelectedItems removeObject:dict];
} else {
[arrSelectedItems addObject:dict];
}
if ([[dict valueForKey:#"isSelected"]isEqualToString:#"0"]) {
[dict setObject:#"1" forKey:#"isSelected"];
} else {
[dict setObject:#"0" forKey:#"isSelected"];
}
[tableView reloadRowsAtIndexPaths:#[indexPath] withRowAnimation:UITableViewRowAnimationAutomatic];
//Here you can show the selected Objects in `UITextField`
self.yourTexField.text = [arrSelectedItems componentsJoinedByString:#","];
}
P.S: I have written the total code here, haven't tested it, so if any errors occurs feel free to leave a comment below, I will be there to solve it.
I think it will help you.
you can create a new tableview with each row as a language, then present it when user respond to textfield.
You can look here for some starter code on how to implement the multiple selection logic.
A textfield has a property call inputview, you can try replacing that with tableview.
I've encountered some very strange behaviour with my tableViewCells in a simple list application (iOS).
This is the basic function of my working app :
TableView with 2 sections. First section (e.g. with 2 cells) is always visible. Second section can be shown/hidden with a custom button in the section header. I've created two classes (i.e. FirstSectionItem and SecondSectionItem) both with a boolean property "completed" and some other properties.
After compilations the app runs as expected. Tapping cells results in showing the checkmarks and tapping them again hides the checkmarks (=custom imageView). However after tapping some different cells (random order) or showing/hiding the second section the checkmark (ImageViews) tend to stay checked no matter how much I tap the cells. After a while every cell is checked and can't be unchecked but the boolean values still keep changing.
Here's part of the code:
#property NSMutableArray *sectionTitleArray;
#property NSMutableDictionary *sectionContentDict;
#property NSMutableArray *firstSectionItems;
#property NSMutableArray *secondSectionItems;
ViewDidLoad:
- (void)viewDidLoad {
[super viewDidLoad];
if (!self.sectionTitleArray) {
self.sectionTitleArray = [NSMutableArray arrayWithObjects:#"section1", #"section2", nil];
}
self.firstSectionItems = [[NSMutableArray alloc] init];
self.secondSectionItems = [[NSMutableArray alloc] init];
[self loadInitialData];
self.sectionContentDict = [[NSMutableDictionary alloc] init];
self.arraySection1 = [[NSMutableArray alloc] init];
for (int i = 0; i < [self.firstSectionItems count]; i++)
{
FirstSectionItem *firstSectionItem = [self.firstSectionItem objectAtIndex:i];
[self.arraySection1 addObject:firstSectionItem.itemName];
}
self.arraySection2 = [[NSMutableArray alloc] init];
for (int i = 0; i < [self.secondSectionItems count]; i++)
{
SecondSectionItem *secondSectionItem = [self.secondSectionItems objectAtIndex:i];
[self.arrayFuture addObject:secondSectionItem.itemName];
}
[self.sectionContentDict setValue:self.arraySection1 forKey:[self.sectionTitleArray objectAtIndex:0]];
[self.sectionContentDict setValue:self.arraySection2 forKey:[self.sectionTitleArray objectAtIndex:1]];
}
CellForRowAtIndexPath:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:#"ListPrototypeCell" forIndexPath:indexPath];
// ... cell content ...
NSArray *content = [self.sectionContentDict valueForKey:[self.sectionTitleArray objectAtIndex:indexPath.section]];
//... constraints ...
UIImage *checkmark = [UIImage imageNamed:#"checkmark.png"];
UIImage *noCheckmark = [UIImage imageNamed:#"transparent.png"];
UIImageView *imageView = [[UIImageView alloc] init];
if (indexPath.section==0){
FirstSectionItem *firstSectionItem = [self.firstSectionItems objectAtIndex:indexPath.row];
imageView.image = firstSectionItem.completed ? checkmark : noCheckmark;
}
if (indexPath.section==1){
if(self.sec2isTapped == YES){
SecondSectionItem *secondSectionItem = [self.secondSectionItems objectAtIndex:indexPath.row];
imageView.image = secondSectionItem.completed ? checkmark : noCheckmark;
}
}
[cell.contentView addSubview:imageView];
// ... more constraints
return cell;
}
didSelectRowAtIndexPath
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
[tableView deselectRowAtIndexPath:indexPath animated:NO];
if (indexPath.section ==0){
FirstSectionItem *tappedItem = [self.firstSectionItems objectAtIndex:indexPath.row];
tappedItem.completed = !tappedItem.completed;
}
if (indexPath.section ==1){
SecondSectionItem *tappedItem = [self.secondSectionItems objectAtIndex:indexPath.row];
tappedItem.completed = !tappedItem.completed;
}
[tableView beginUpdates];
[tableView reloadRowsAtIndexPaths:#[indexPath] withRowAnimation:UITableViewRowAnimationMiddle];
[tableView endUpdates];
}
I've tried multiple approaches but none of them seem to work. I can't figure out what the problem is.
(I am new to objective C so some parts might seem a bit devious).
Thanks in advance.
The problem is that you are initializing the UIImageView each time cellForRowAtIndexPath is running. But remember that that cells are reused so what you are doing is reusing a cell that has a checkmark and adding a new clear checkmark on top.
What you need to do is either:
Add the imageView in the storyboard, tag it, and user the tag to find it in the code
Subclass UITableViewCell and assign the correct image in there.
There are MANY articles of how to do both online.
i got a view where the user is can input a name and an age :
the fields got the following names and the tag-value 1.
#property (weak, nonatomic) IBOutlet UITextField *name;
#property (weak, nonatomic) IBOutlet UITextField *amount;
If the user creates more textFields, they will be also named *name und *age and linked to the ViewController. The value will be counted to 2 for the second pair of Fields, 3 for the third pair etc.
I want to foreach an array which is as long as the number of textField pairs and count i++;
How do is the syntax for getting the values referring to name and tag? I googled and found the following solution which works if the tags are unique, which is not the case in my situation. How do I extend this selection by the elements name?
UITextField *textField = (UITextField*)[self.view viewWithTag:1];
NSString *amount1 = textField.text;
I would suggest moving this all into a UITableView. There are many ways to modify the way it looks by using custom UITableViewCells and adjusting the table's background color, separator insets, etc.
Look around for a tutorial that you like for creating a custom cell, and give it your 2 UITextFields as #property variables. Create a new UITableViewController instead of your existing controller and #import your custom cell class.
TableViewController.h
#interface TableViewController : UITableViewController <UITextFieldDelegate>
{
NSMutableArray *namesArray;
NSMutableArray *amountsArray;
UITextField *activeField;
}
TableViewController.m
// only relevant code displayed
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return [namesArray count] + 1; // extra row for the "Add Row" row
}
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
return 100.0f;
}
- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section
{
UIButton *addDataButton = [UIButton buttonWithType:UIButtonTypeCustom];
[addDataButton setTitle:#"Add Data to Array"];
[addDataButton addTarget:self action:#selector(addDataToArray) forControlEvents:UIControlEventTouchUpInside];
// customize your button as needed
UITableViewHeaderFooterView *footerView = [[UITableViewHeaderFooterView alloc] initWithFrame:CGRectMake(0, 0, tableView.frame.size.width, 100)];
[footerView addSubview:addDataButton];
return footerView;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
if(indexPath.row == [namesArray count]) // the last row
{
UITableViewCell *cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:#""];
cell.textLabel.text = #"Add Row";
return cell;
}
static NSString *reuseIdentifier = #"YourReuseID"; // use whatever you set in your custom cell class
MyCustomCell *cell = (MyCustomCell *)[tableView dequeueReusableCellWithIdentifier:reuseIdentifier];
if(!cell)
{
cell = [[[NSBundle mainBundle] loadNibNamed:#"MyCustomCell" owner:self options:nil] objectAtIndex:0];
}
cell.nameField.text = [namesArray objectAtIndex:indexPath.row];
cell.nameField.tag = indexPath.row;
cell.nameField.delegate = self;
cell.amountField.text = [amountsArray objectAtIndex:indexPath.row];
cell.amountField.tag = indexPath.row + 1000000; // to make sure it doesn't have the same tag as your nameField
cell.amountField.delegate = self;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
[tableView deselectRowAtIndexPath:indexPath animated:YES];
if(indexPath.row = [namesArray count])
{
[namesArray addObject:#""];
[amountsArray addObject:#""];
[tableView reloadData];
}
}
- (void)textFieldDidBeginEditing:(UITextField *)textField
{
activeTextField = textField;
}
- (BOOL)textFieldShouldEndEditing:(UITextField *)textField
{
if(textField.tag < 1000000)
{
[namesArray replaceObjectAtIndex:textField.tag withObject:textField.text];
}
else
{
[amountsArray replaceObjectAtIndex:textField.tag - 1000000 withObject:textField.text];
}
}
- (void)addDataToArray
{
[activeTextField resignFirstResponder];
// add your data as needed. The first row's name/amount will be available in [namesArray objectAtIndex:0] and [amountsArray objectAtIndex:0]
}
You can use tag with value: 1000 + i pour name and 2000 + j pour amount.
Then, to get value of name associated, use tag = tagOfName + 1000.
you need to have a logic which uniquely identifies textfields.
Set the tags of name and amount as mentioned below.
Tag of the name textfield for nth row should be (n * 2) + 1 where n=0,1,2.....n-1
Tag of the amount textfield for nth row should be (n * 2) + 2 where n=0,1,2.....n-1
You can get the value associated with textfield by using the below statements
UITextField *nameNTextField = (UITextField*)[self.view viewWithTag:(n * 2) + 1]; //where n=0,1,2.....n-1
NSString *nameN = nameTextField.text;
UITextField *amountNTextField = (UITextField*)[self.view viewWithTag:(n * 2) + 2]; //where n=0,1,2.....n-1
NSString *amountN = nameTextField.text;
I am trying to save my data by NSUserDefaults and view it in table view. I have already set four texts data to an array (dataArray) so I can view it in the table view. but I couldn't load the data to the table view. So am I missing something?
Thanks in advance.
-(IBAction)save{
NSUserDefaults *add1 = [NSUserDefaults standardUserDefaults];
[add1 setObject:txt1 forKey:#"txt1"];
[add1 setObject:txt2 forKey:#"txt2"];
[add1 setObject:txt3 forKey:#"txt3"];
[add1 setObject:txt4 forKey:#"txt4"];
[add1 synchronize];
}
- (void)viewDidLoad
{
[super viewDidLoad];
NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults];
self.dataArray = [NSArray arrayWithObjects:[prefs objectForKey:#"txt1"], [prefs objectForKey:#"txt2"], [prefs objectForKey:#"txt3"],[prefs objectForKey:#"txt4"] nil];
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 1;
}
// Set up the number of rows
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return [dataArray count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
NSString *string = [dataArray objectAtIndex:indexPath.row];
static NSString *CellIdentifier = #"Cell";
UITableView *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
}
You have to call [yourUserDefaultObject synchronize]; to save the state of the NSUserDefaults, but you don't really need to create 4 objects of NSUserDefaults, you can just create it one time and then call setObject:forKey: on the same instance and then synchronize.
Then just set cell.textLabel.text=string; to set the label of the cell in the cellForRowAtIndexPath callback.
First syncronize nsuserdefaults and
You need to alloc the tableviewcell and assign the text to the text label
Add following code in your cellForRowAtindexpath method:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
NSString *string = [dataArray objectAtIndex:indexPath.row];
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if(cell==nil)
{
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
}
cell.textLabel.text=string;
return cell;
}
Dude add following line because you need to add test somewhere in cell. So that can display on tablviewcell. If possible than please first make array than save whole array in NSUserDefaults by that way you save your time and that's better way to do coding.
cell.textLabel.text=string;
Edited>>>
UILabel *label;
label = [[UILabel alloc]initWithFrame:CGRectMake(10, 10, 50, 40)];
label.text = string;
// or
label.text = #"Hi I am here..";
label.backgroundColor = [UIColor greenColor];
label.textColor = [UIColor whiteColor];
[cell addSubview:label];