NSArray Data to Both UITableView and UICollectionView - ipad

I have an NSMutableArray I build with the below code
//Question Array Setup and Alloc
stratToolsDict = [[NSMutableDictionary alloc] initWithObjectsAndKeys:countButton,#"count",camerButton,#"camera",videoButton,#"video",textButton,#"text",probeButton,#"probe", nil];
stratTools = [[NSMutableArray alloc] initWithObjects:#"Tools",stratToolsDict, nil];
stratObjectsDict = [[NSMutableDictionary alloc]initWithObjectsAndKeys:stratTools,#"Strat1",stratTools,#"Strat2",stratTools,#"Strat3",stratTools,#"Strat4", nil];
stratObjects = [[NSMutableArray alloc]initWithObjects:#"Strategies:",stratObjectsDict,nil];
QuestionDict = [[NSMutableDictionary alloc]initWithObjectsAndKeys:stratObjects,#"Question 1?",stratObjects,#"Question 2?",stratObjects,#"Question 3?",stratObjects,#"Question 4?",stratObjects,#"Question 5?", nil];
//add strategys to questions
for (int i = 0; i < 1; i++) {
//Send Var to STVar
[[STVars myMutableArray]addObject:QuestionDict];
}
In the For Loop you will see "[STVars myMutableArray]" this is a singleton method used in storing the Question Array.
My problem lies in the fact that i have a UIPopOverController that has a UITableView which needs to be able to load all the questions titles.....ex(#"Question 1", #"Question 2").
They should then be populated as the datasource for the UITableView in the UIPopOverController.
As well in the class where i initialize the Question Array there is a UICollectionView which also needs to grab all the Strategies from the Question Array for the Question Clicked from the UIPopOverController.
Here is the UIPopOverController Accessing the Singleton Array which does have Data.
When i attach the Array and try to load the cells i get a "SIGABRT" error.....(Hate that One.)
- (id)initWithStyle:(UITableViewStyle)style
{
if ([super initWithStyle:style] != nil) {
//Log test
NSLog(#"QuestionList init method from STVars: %#",[STVars myMutableArray]);
QuestionsList = [STVars myMutableArray];
You can see that i check with NSLog first to make sure the Data is in the Array in the Singleton class. Then i assign the array to another NSMutableArray *QuestionsList.
When i assign it to the UITableView it never shows up.
Sizing the UIPopOvercontroller:
//Make row selections persist.
self.clearsSelectionOnViewWillAppear = NO;
//Calculate how tall the view should be by multiplying
//the individual row height by the total number of rows.
NSInteger rowsCount = [QuestionsList count];
NSInteger singleRowHeight = [self.tableView.delegate tableView:self.tableView
heightForRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:0]];
NSInteger totalRowsHeight = rowsCount * singleRowHeight;
//Calculate how wide the view should be by finding how
//wide each string is expected to be
CGFloat largestLabelWidth = 0;
for (NSString *questionName in QuestionsList) {
//Checks size of text using the default font for UITableViewCell's textLabel.
CGSize labelSize = [questionName sizeWithFont:[UIFont boldSystemFontOfSize:20.0f]];
if (labelSize.width > largestLabelWidth) {
largestLabelWidth = labelSize.width;
}
}
//Add a little padding to the width
CGFloat popoverWidth = largestLabelWidth + 100;
//Set the property to tell the popover container how big this view will be.
self.contentSizeForViewInPopover = CGSizeMake(popoverWidth, totalRowsHeight);
Table Cells Code:
- (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];
}
// Configure the cell...
cell.textLabel.text = [QuestionsList objectAtIndex:indexPath.row];
return cell;
}
I can not seem to get these Cells to populate any of the Questions from the Array.
A nice simple explanation as to why it may not be showing up would be really nice.
I have been developing for a few days straight and think it could be something simple but cant catch it....any help is greatly appreciated!

Related

Obj-C- Add new row to TableView when cell is tapped?

I've got a tableview that allows users to add an item (a row) to an invoice (the tableview) when an existing row is tapped. That said, I can't seem to add an empty row because my code is trying to set the information in the cell with data from my specified array, but naturally, the count in the array is different from my data source (as I want the count to be +1).
E.g. I want to return 3 cells even if there are only 2 dictionaries in my array, and the third cell should be empty.
I want this because the third cell allows my user to fill out empty fields, while the fields in the previous two rows are populated with their already input data. Here's how I'm trying to return the extra row right now, but as mentioned above, it crashes my app due to the imbalance of dictionaries returned in my array.
Here's my code so far:
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
self.allItems = [[NSMutableArray alloc] init];
self.itemDetails = [[NSMutableDictionary alloc] init];
}
//TableView delegates
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return 1;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return self.allItems.count + 1;
}
-(UITableViewCell *)tableView:(UITableView*)tableView cellForRowAtIndexPath:(nonnull NSIndexPath *)indexPath {
static NSString *ClientTableIdentifier = #"InvoiceDetailsTableViewCell";
InvoiceDetailsTableViewCell *cell = (InvoiceDetailsTableViewCell *)[self.tableView dequeueReusableCellWithIdentifier:ClientTableIdentifier];
if (cell == nil)
{
NSArray *nib = [[NSBundle mainBundle] loadNibNamed:#"InvoiceDetailsTableViewCell" owner:self options:nil];
cell = [nib objectAtIndex:0];
}
if (self.allItems.count == 0) {
} else {
cell.itemName.text = [self.allItems valueForKey:#"Item Name"][indexPath.row];
}
return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
InvoiceDetailsTableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
NSString *itemTitle = cell.itemName.text;
NSString *itemDescrip = cell.itemDescrip.text;
NSString *itemCost = cell.itemCost.text;
NSString *itemTax = cell.itemTax.text;
[self.itemDetails setValue:itemTitle forKey:#"Item Name"];
[self.itemDetails setValue:itemDescrip forKey:#"Item Description"];
[self.itemDetails setValue:itemCost forKey:#"Item Cost"];
[self.itemDetails setValue:itemTax forKey:#"Item Tax Rate"];
[self.allItems addObject:self.itemDetails];
[self.tableView reloadData];
}
One significant problem is the line that says:
cell.itemName.text = [self.allItems valueForKey:#"Item Name"][indexPath.row];
Since your row count exceeds the number of items in your array, you will want to check the row number before accessing the array:
NSInteger row = indexPath.row;
if (row < self.allItems.count) {
cell.itemName.text = self.allItems[row][#"Item Name"]; // personally, I’d get row first, and then keyed value second
} else {
cell.itemName.text = #"";
}
You want to check to make sure that the current row is not the last (blank) row.

Objective C - ImageView (custom checkmark) in UITableViewCell stops toggling after multiple cell selections

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.

Scroll to the last two rows of table

when the table has more records than the screen let you see, and the user scroll to the bottom for the last records in the table, the last two rows are not accessible (just in iOS7, in iOS8 everything is fine). Once the user take the finger from screen, the table goes up, and the last two rows are hidden.
This is one of the method used for tableView but i dont know if this come from here:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString * cellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier forIndexPath:indexPath];
// Configure the cell...
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
}
if (selectionCategMsg == 0)
{
if ([self.tableauMsgReceived count] < 1) {
cell.textLabel.text = NSLocalizedString(#"noMessagesYet", nil); //#"No messages yet !";
}
else
{
cell.textLabel.text = [NSString stringWithFormat:#"%#", [self.tableauMsgReceived objectAtIndex:indexPath.row]];
cell.textLabel.lineBreakMode = NSLineBreakByWordWrapping;
}
}
else
{
if ([self.tableauMsgSent count] < 1) {
cell.textLabel.text = NSLocalizedString(#"noReplyYet", nil);
}
else
{
cell.textLabel.text = [NSString stringWithUTF8String:[NSString stringWithFormat:#"%#", [self.tableauMsgSent objectAtIndex:indexPath.row]].UTF8String];
cell.textLabel.lineBreakMode = NSLineBreakByWordWrapping;
}
}
[cell setAccessoryType:UITableViewCellAccessoryNone];
return cell;
}
Can someone give an advice ? Thank you.
THIS IS THE SOLUTION THAT I FOUND:
// -> SET TABLE FRAME
CGFloat sideMargin = 0;
CGFloat originX = sideMargin;
CGFloat topBottomMargin = 100;
CGFloat originY = topBottomMargin;
// Width based on view size
CGFloat sizeWidth = self.view.bounds.size.width);
// Height based on view size
CGFloat sizeHeight = (self.view.bounds.size.height - topBottomMargin);
self.myTableView.frame = CGRectMake(originX, originY, sizeWidth, sizeHeight);
// <- END SET tableView frame
UITableView has the method scrollToRowAtIndexPath:atScrollPosition:animated:. That method lets you scroll the table view so a given indexPath is visible. Is that what you're looking for?
EDIT:
#Daij-Djan's post is a good theory on what's wrong. If your table view goes off the screen then it will position the last couple of cells inside it's view, but that view won't be visible. You're going to need to do some debugging to figure out what's wrong. You might try setting the table view's layer's borderWidth to a non-zero value so you can see the bounds of the table view.
In your viewDidLoad, add code like this:
myTableView.layer.borderWidth = 1;
myTableView.layer.borderColor= [UIColor redColor].CGColor;
(Where you'd replace "myTableView" with the name of your table view instance variable or property.)
You'll have to import QuartzCore.h in order for the code above to compile:
#import <QuartzCore/QuartzCore.h>

UILabel upon a few UITableViewCells

Am I able to create a UILabel that is layouted upon many UITableViewCells?
I'm trying to make something like (that is just one section of my UITableView, each section can have one or more rows):
---------------------------------------------
| Multi-lined label | row1 values |
| with some useless | row2 values |
| text | row3 values |
---------------------------------------------
I managed to create a UILabel (in the first row of a section) that is multi-lined and is not clipping to bounds. That works really well (it was a bit tricky to count each sections row heights, but doable) besides one case: when I'm scrolling UITableView from bottom to top - UITableView renders last row (without UILabel) so it has "no evidence" of having UILabel (because it is maintained in the first row of section). Can I force some kind of relayouting first cell in section? I tried reloadRowsAtIndexPaths:withRowAnimation: with first row in each section whenever I layouted not first cell in section but it gave me layouting errors that I really do not understand. Or maybe there is another idea to do so?
-- EDITED
To be clear: I have a custom UITableViewCell with an IB view, it has a few labels that each row consist of and a label named labelName that I want to be "multi-lined" along rows in that section. LabelName.text is empty for each row besides first one in each section.
I am adding somescreenshots:
Good screenshot - when I am scrolling to bottom I'm getting proper effect:
Bad screenshot - when I am scrolling up, UITableView renders last row of section firstly, and afterwards renders upper rows - that gives effect of cut label (because multi-line label is in the first row)
I am not sure if code here will add anything to question - it is rather simple and almost whole logic is in tableView:heightForRowAtIndexPath. I can only present how do I create custom UITableViewCell:
CustomTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:[CustomTableViewCell reuseIdentifier]];
if (cell == nil) {
cell = [[CustomTableViewCell alloc] initWithOwner:self];
cell.clipsToBounds = NO;
cell.labelName.clipsToBounds = NO;
cell.contentView.superview.clipsToBounds = NO;
}
-- EDIT 2
Here is most of the code:
- (void) reloadData
{
NSUInteger index = 0;
for (NSDictionary *object in self.list) {
CGFloat height = [[object objectForKey:#"name"] sizeWithFont:self.labelFont constrainedToSize:self.labelSize].height;
[self.labelHeights addObject:NSNumberFloat(ceilf(height))];
index++;
}
[self.tableView reloadData];
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
NSDictionary *object = [self.list objectAtIndex:indexPath.section];
CGFloat height = [[self.labelHeights objectAtIndex:indexPath.section] floatValue];
NSUInteger count = [[object objectForKey:#"list"] count];
CGFloat cellHeight = 30.f;
if((indexPath.row + 1) == count){
cellHeight = MAX(8.f + height - 30.f * indexPath.row, 30.f);
}
return cellHeight;
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return [self.list count];
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return [[[self.list objectAtIndex:section] objectForKey:#"list"] count];
}
- (UITableViewCell *) tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
NSDictionary *person = [self.list objectAtIndex:indexPath.section];
NSDictionary *object = [[person objectForKey:#"list"] objectAtIndex:indexPath.row];
CustomTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:[CustomTableViewCell reuseIdentifier]];
if (cell == nil) {
cell = [[CustomTableViewCell alloc] initWithOwner:self];
cell.backgroundColor = [UIColor clearColor];
cell.userInteractionEnabled = NO;
cell.clipsToBounds = NO;
cell.labelName.clipsToBounds = NO;
[cell.contentView.superview setClipsToBounds:NO];
}
if(indexPath.row == 0){
cell.labelName.text = [person objectForKey:#"name"];
CGFloat height = [[self.labelHeights objectAtIndex:indexPath.section] floatValue];
cell.labelName.numberOfLines = (int)(height / self.fontSizeHeight);
cell.labelName.frame = CGRectChangeHeight(cell.labelName.frame, height);
}
else{
cell.labelName.text = #"";
}
CGFloat cellHeight = [self tableView:self.tableView heightForRowAtIndexPath:indexPath];
cell.borderTop.hidden = YES;
cell.borderBottom.hidden = YES;
cell.borderBottomSmall.hidden = NO;
if(indexPath.row == 0){
cell.borderTop.hidden = NO;
}
if(indexPath.row + 1 == [[person objectForKey:#"list"] count]){
cell.borderBottom.hidden = NO;
cell.borderBottom.frame = CGRectChangeY(cell.borderBottom.frame, cellHeight - 1.f);
cell.borderBottomSmall.hidden = YES;
}
cell.labelDate.text = [object objectForKey:#"date"];
cell.labelPremium.text = [[object objectForKey:#"premium"];
return cell;
}
-- PARTIAL ANSWER
I managed to create a hack, that makes multi-line UILabel visibile when scrolling bottom to up at some point:
- (void) scrollViewDidScroll:(UIScrollView *)scrollView
{
NSArray *cells = [self.tableView visibleCells];
UITableViewCell *cell = [cells objectAtIndex:0];
[cell.superview bringSubviewToFront:cell];
}
I noticed that the part of the UILabel is covered by a row thats below of the UILabels row and that hack makes it would be properly displayed. But it has a drawback, when scrolling slowly from bottom to top it generates a flicker when label is created (part of it should be visible before real creation of UILabel).
Up mentioned answers are not solutions, but "hacks".
In the cell == nil block should be only the initialization.
You should not add any subviews in cell in cellForRowAtIndexPath.
The reason is simple: I will reuse a cell with some labels already added and add a new label.
Either use the default cell.textLabel, either create a subclass for UITableViewCell, with a
-(void)setData:(dictionary or string)object;
and in implementation just set the proper data to proper UI controls.
Add/create controls either in init method in the subclass, or in IB/Storyboard.
Call the dictionary or string should be picked in correspondence to indexPath, so you will always get proper data for proper cell at proper indexPath.
Try This
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *cellId = #"cellId";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellId];
if (cell==nil)
{
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellId];
}
for (UIView *subview in [cell.contentView subviews]) {
[subview removeFromSuperview];
}
/// your UI on cell goes here
return cell;
}

Custom cells with textfield not being kept in memory in a UITableView

I'm trying to create a registration form that contains around 20 different fields separated in four different sections.
I've created a custom cell for that, that contains a label and a UITextField and I've got an array of dictionaries on my tableview that indicates what the name of the label should be, as well as the tag of the texftield (because depending upon the tag I use a UIPicker to enter data).
The problem is that, when I start editing the fields, scrolling down and back up everything gets messed up, and changed on fields it should not... I've came across that this is obviously happening because I'm dequeing cells so it's creating duplicated references, so I tried to create a new cell every time, but that doesn't work since it will only initialize a tableview with the exact amount of cells I need but just blank cells with no content (label nor textfield) whatsoever inside.
What would be the easiest approach so I can keep a different memory reference for each one of my cells, considering the fact that I've got to retrieve their values when a button is pressed?
I've read the suggestions on this post but still have no idea about how to create an array of cells.
Any help?
This is the code for my definitions:
NSArray *section1 = [[NSArray alloc] initWithObjects:
[NSDictionary dictionaryWithObjectsAndKeys:#"100",#"tag",#"Title",#"title",#"title",#"fieldName",nil],
[NSDictionary dictionaryWithObjectsAndKeys:#"0",#"tag",#"First Name",#"title",#"first_name",#"fieldName",nil],
[NSDictionary dictionaryWithObjectsAndKeys:#"0",#"tag",#"Last Name",#"title",#"last_name",#"fieldName",nil],
[NSDictionary dictionaryWithObjectsAndKeys:#"0",#"tag",#"Job Title",#"title",#"job_title",#"fieldName",nil],
[NSDictionary dictionaryWithObjectsAndKeys:#"0",#"tag",#"Email",#"title",#"email",#"fieldName",nil],
[NSDictionary dictionaryWithObjectsAndKeys:#"0",#"tag",#"Confirm Email",#"title",#"confirm_email",#"fieldName",nil],
[NSDictionary dictionaryWithObjectsAndKeys:#"0",#"tag",#"Password",#"title",#"password",#"fieldName",nil],
[NSDictionary dictionaryWithObjectsAndKeys:#"0",#"tag",#"Confirm Password",#"title",#"conf_password",#"fieldName",nil],
[NSDictionary dictionaryWithObjectsAndKeys:#"0",#"tag",#"Phone",#"title",#"phone",#"fieldName",nil],
nil];
....
self.formItems = [[NSArray alloc] initWithObjects:
section1,
section2,
section3,
section4,
section5,
nil];
And then the code I use on cellForRowAtIndexPath is the following:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"freeTrialFormCell";
TSIFreeTrialFormCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[TSIFreeTrialFormCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
NSArray* sectionData = [self.formItems objectAtIndex:indexPath.section];
NSDictionary* myFieldInfo = [sectionData objectAtIndex:indexPath.row];
if ([[myFieldInfo objectForKey:#"fieldName"] rangeOfString:#"password"].location != NSNotFound) {
cell.textField.secureTextEntry = YES;
}
cell.fieldName = [myFieldInfo objectForKey:#"fieldName"];
cell.textField.placeholder = [myFieldInfo objectForKey:#"title"];
cell.textField.tag = [[myFieldInfo objectForKey:#"tag"] integerValue];
cell.label.text = [myFieldInfo objectForKey:#"title"];
return cell;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
TSIFreeTrialFormCell *cell = [[TSIFreeTrialFormCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:#"cell"] autorelease];
NSArray* sectionData = [self.formItems objectAtIndex:indexPath.section];
NSDictionary* myFieldInfo = [sectionData objectAtIndex:indexPath.row];
if ([[myFieldInfo objectForKey:#"fieldName"] rangeOfString:#"password"].location != NSNotFound) {
cell.textField.secureTextEntry = YES;
}
cell.fieldName = [myFieldInfo objectForKey:#"fieldName"];
cell.textField.placeholder = [myFieldInfo objectForKey:#"title"];
cell.textField.tag = [[myFieldInfo objectForKey:#"tag"] integerValue];
cell.label.text = [myFieldInfo objectForKey:#"title"];
return cell;
}
But this will have a performance hit as you are creating cell for each row
The UITableView reuses cells as you scroll up and down, so you will lose info that is only typed into that cell. What I do is create an array to hold the inputs for those cells.
Basically:
enteredText = [[NSMutableArray alloc] init]; //iVar, not local variable
for (int i=0; i<numTableRows; i++) {
[enteredText addObject:#""]; //fill with dummy values that you will replace as user types
}
Then in your table view text field, set yourself as the delegate of the text field and implement textFieldDidEndEditing. In that method, replace the value in enteredTextfor that row with the user's text.
When you make the cell, set the text field text to the value of in your array:
myCell.textField.text = [enteredText objectAtIndex:indexPath.row];
It'll be blank at first, and then will hold the user's text after they've put something in there.

Resources