Not visible cells in table view get overridden - ios

I have made a form by code to sign up within the application but if I scroll up and down the cells which are not visible get overridden with the setup of other cells... Has anyone experienced something like that? I'm pretty sure that I'm missing out on some configuration but I don't know which.
Here is my code.
Its a lot of code....sorry. But I really don't know where to start
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
if (section == 0) {
return 4;
} else if (section == 1) {
return 4;
} else {
return 5;
}
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 3;
}
-(NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {
if (section == 0) {
return #"Perönliche Informationen";
} else if (section == 1) {
return #"Anschrift";
} else {
return #"myClassico Profil";
}
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
if ([indexPath section] == 0) {
if ([indexPath row] == 3) {
return 210;
} else {
return 44;
}
}
else if ([indexPath section] == 1) {
if ([indexPath row] == 0) {
return 80;
} else if ([indexPath row] == 3) {
return 210;
} else {
return 44;
}
}
else if ([indexPath section] == 2) {
return 44;
}
else {
return 44;
}
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"anmeldenCell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
if ([indexPath section] == 0) {
if ([indexPath row] == 0) {
UISegmentedControl *gender = [[UISegmentedControl alloc] initWithItems:[NSArray arrayWithObjects:#"Frau", #"Herr", nil]];
gender.frame = CGRectMake(99, 8, 124, 28);
gender.selectedSegmentIndex = 0;
[cell.contentView addSubview:gender];
} else if ([indexPath row] == 1) {
UITextField *vorname = [[UITextField alloc] initWithFrame:CGRectMake(20, 0, 300, 44)];
vorname.placeholder = #"Vorname";
[cell.contentView addSubview:vorname];
} else if ([indexPath row] == 2) {
UITextField *nachname = [[UITextField alloc] initWithFrame:CGRectMake(20, 0, 300, 44)];
nachname.placeholder = #"Nachname";
[cell.contentView addSubview:nachname];
} else {
//Code below is backup code in case someone really dislikes the UIDatePicker. Code is fully functional.
/*
UITextField *gebTag = [[UITextField alloc] initWithFrame:CGRectMake(20, 0, 80, 44)];
UITextField *gebMonat = [[UITextField alloc] initWithFrame:CGRectMake(108, 0, 100, 44)];
UITextField *gebJahr = [[UITextField alloc] initWithFrame:CGRectMake(216, 0, 100, 44)];
gebTag.placeholder = #"TT";
gebMonat.placeholder = #"MM";
gebJahr.placeholder = #"JJ";
[cell.contentView addSubview:gebTag];
[cell.contentView addSubview:gebMonat];
[cell.contentView addSubview:gebJahr];*/
UILabel *gebLabel = [[UILabel alloc] initWithFrame:CGRectMake(20, 0, 280, 50)];
gebLabel.text = #"Geburtsdatum";
UIDatePicker *gender = [[UIDatePicker alloc] initWithFrame:CGRectMake(20, 30, 320, 180)];
[cell.contentView addSubview:gender];
[cell.contentView addSubview:gebLabel];
} //ENDE SECTION 1 (INFORMATIONEN ZUR PERSON)
} else if ([indexPath section] == 1) {
if ([indexPath row] == 0) {
UITextField *adresseStrasse = [[UITextField alloc] initWithFrame:CGRectMake(20, 0, 280, 44)];
adresseStrasse.placeholder = #"Straße";
UITextField *adresseHNR = [[UITextField alloc] initWithFrame:CGRectMake(180, 30, 120, 44)];
adresseHNR.textAlignment = 2;
adresseHNR.placeholder = #"Hausnummer";
[cell.contentView addSubview:adresseStrasse];
[cell.contentView addSubview:adresseHNR];
} else if ([indexPath row] == 1) {
UITextField *adresseZusatz = [[UITextField alloc] initWithFrame:CGRectMake(20, 0, 280, 44)];
adresseZusatz.placeholder = #"Adresszusatz (optional)";
[cell.contentView addSubview:adresseZusatz];
} else if ([indexPath row] == 2) {
UITextField *adressePLZ = [[UITextField alloc] initWithFrame:CGRectMake(20, 0, 100, 44)];
adressePLZ.placeholder = #"PLZ";
UITextField *adresseOrt = [[UITextField alloc] initWithFrame:CGRectMake(110, 0, 170, 44)];
adresseOrt.placeholder = #"Ort";
[cell.contentView addSubview:adressePLZ];
[cell.contentView addSubview:adresseOrt];
} else {
UILabel *land = [[UILabel alloc] initWithFrame:CGRectMake(20, 10, 150, 20)];
land.text = #"Land";
länderPicker = [[UIPickerView alloc] initWithFrame:CGRectMake(20, 20, 300, 180)];
[länderPicker setDelegate:self];
array = [[NSMutableArray alloc] init];
[array addObject:#"Deutschland"];
[array addObject:#"Österreich"];
[array addObject:#"Schweiz"];
[cell.contentView addSubview:land];
[cell.contentView addSubview:länderPicker];
} //ENDE SECTION 2 (ADRESSEN INFORMATIONEN)
} else if ([indexPath section] == 2){
if ([indexPath row] == 0) {
UITextField *email = [[UITextField alloc] initWithFrame:CGRectMake(20, 0, 280, 44)];
email.placeholder = #"E-Mail";
[cell.contentView addSubview:email];
} else if ([indexPath row] == 1) {
UITextField *telefon = [[UITextField alloc] initWithFrame:CGRectMake(20, 0, 280, 44)];
telefon.placeholder = #"Telefon";
[cell.contentView addSubview:telefon];
} else if ([indexPath row] == 2) {
UITextField *mtelefon = [[UITextField alloc] initWithFrame:CGRectMake(20, 0, 280, 44)];
mtelefon.placeholder = #"Mobiltelefon (optional)";
[cell.contentView addSubview:mtelefon];
} else if (indexPath.row == 3) {
UITextField *passwort = [[UITextField alloc] initWithFrame:CGRectMake(20, 0, 280, 44)];
passwort.placeholder = #"Passwort";
[cell.contentView addSubview:passwort];
} else {
UITextField *passwortWiederholung = [[UITextField alloc] initWithFrame:CGRectMake(20, 0, 280, 44)];
passwortWiederholung.placeholder = #"Passwort wiederholen";
[cell.contentView addSubview:passwortWiederholung];
} //ENDE SECTION 3 (MYCLASSICO ACCOUNT INFORMATIONEN)
}
return cell;
}
-(NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component {
return [array count];
}
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)thePickerView {
return 1;
}
- (NSString *)pickerView:(UIPickerView *)thePickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component {
return [array objectAtIndex:row];
}

Use different identifier for cells:
static NSString *CellIdentifier = #"anmeldenCell";
or prepare reset function for them.
Your problem is that cells are reused between sections. You are adding subview here [cell.contentView addSubview:gender]; and you never remove it - so it will be added several times to the same cell + the cell with this view will be used in other sections as well.
Best approach will be to prepare custom cell per section and use different CellIdentifier for them. Also when you reuse them assing a view to it's properly instead of adding subview several times.
BTW. use enumerations for sections to increase readability, instead of 1,2,3,4 use kSectionMyProfile, etc...

Related

multiple cells and still duplicate tableview content

i've been advised to create multiple subclasses for each section, since i'm creating an tableview with lots of different content, the problem is whatever i do it keep duplicating the content of each cell randomly. What am i doing wrong?
viewDidLoad
identifier0 = #"Cell0";
identifier1 = #"Cell1";
identifier2 = #"Cell2";
[self.tableView registerClass:[accountTableViewCell class] forCellReuseIdentifier:identifier0];
[self.tableView registerClass:[NotificationTableViewCell class] forCellReuseIdentifier:identifier1];
cellForRowAtIndexPath
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
//section 0
self.emailTextField.delegate = self;
self.cellName = [[UILabel alloc] initWithFrame:CGRectMake(20, 12, 130, 20)];
self.cellName.textColor = [UIColor colorWithRed:0.137 green:0.145 blue:0.157 alpha:1];
self.cellName.font = [UIFont fontWithName:#"HelveticaNeue" size:14];
self.nameTextField = [[UITextField alloc] initWithFrame:CGRectMake(110, 7, 190, 30)];
self.nameTextField.enabled = NO;
self.emailTextField = [[UITextField alloc] initWithFrame:CGRectMake(110, 7, 190, 30)];
[self.emailTextField setReturnKeyType:UIReturnKeyDone];
[self.emailTextField setKeyboardType:UIKeyboardTypeEmailAddress];
self.genderSwitch = [[UISwitch alloc] initWithFrame:CGRectMake(251, 6, 31, 51)];
//section 1
self.acceptanceSwitch = [[UISwitch alloc] initWithFrame:CGRectMake(251, 6, 31, 51)];
self.likeSwitch = [[UISwitch alloc] initWithFrame:CGRectMake(251, 6, 31, 51)];
self.messageSwitch = [[UISwitch alloc] initWithFrame:CGRectMake(251, 6, 31, 51)];
accountTableViewCell *cell0;
NotificationTableViewCell *cell1;
if(indexPath.section == 0)
{
cell0 = [tableView dequeueReusableCellWithIdentifier:identifier0 forIndexPath:indexPath];
} else if (indexPath.section == 1) {
cell1 = [tableView dequeueReusableCellWithIdentifier:identifier1 forIndexPath:indexPath];
}
// This part ensure you will initiate each cell with the correct identifier
if (!cell0)
{
cell0 = [[accountTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier0];
cell0.backgroundColor = [UIColor colorWithRed:0.953 green:0.953 blue:0.965 alpha:1] ;
} else if (!cell1) {
cell1 = [[NotificationTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier1];
cell1.backgroundColor = [UIColor colorWithRed:0.953 green:0.953 blue:0.965 alpha:1] ;
}
if (indexPath.section == 0) {
[cell0.contentView addSubview:self.cellName];
self.cellName.text = [accountArray objectAtIndex:indexPath.row];
if (indexPath.row == 0) {
[cell0.contentView addSubview:self.nameTextField];
} else if (indexPath.row == 1) {
[cell0.contentView addSubview:self.emailTextField];
} else if (indexPath.row == 2) {
[cell0.contentView addSubview:self.genderSwitch];
}
} else if(indexPath.section == 1) {
[cell1.contentView addSubview:self.cellName];
cell1.selectionStyle = UITableViewCellSelectionStyleNone;
self.cellName.text = [notArray objectAtIndex:indexPath.row];
if (indexPath.row == 0) {
[cell1.contentView addSubview:self.acceptanceSwitch];
} else if (indexPath.row == 1) {
[cell1.contentView addSubview:self.messageSwitch];
} else if (indexPath.row == 2) {
[cell1.contentView addSubview:self.likeSwitch];
}
}
return cell0;
return cell1;
}
You are seeing duplicate content because you have the custom contentView's subviews as properties of your UITableViewDataSource. For example, you use self.cellName for every cell in section 0. Let us say you have 5 cellName-type cells in section 0. If you want a custom UILabel on all 5 cells then you would need to alloc/init 5 separate UILabels for each cell and the key part is that you need 5 separate pointers for each label. Right now you are using one, self.nameLabel. There are many ways you could go about fixing this. One way would be making the custom subviews as properties of your two UITableViewCell subclasses.

Tableview not showing any of the arrays

I'm trying to create a in app setting tableview, which requires different sections with different content. i first tried all the code outside (!cell), but that resulted in duplicate of the content on scroll, i've then found thread where it said i needed to put inside (!cell), but now it does not show any content. What am i doing wrong?
CellForRowAtIndexPath
- (UITableViewCell *)tableView:(UITableView *)tableView
cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *MyIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:MyIdentifier];
if (!cell)
{
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault
reuseIdentifier:MyIdentifier];
cell.backgroundColor = [UIColor colorWithRed:0.953 green:0.953 blue:0.965 alpha:1] ;
self.emailTextField.delegate = self;
self.cellName = [[UILabel alloc] initWithFrame:CGRectMake(20, 12, 42, 20)];
self.cellName.textColor = [UIColor colorWithRed:0.137 green:0.145 blue:0.157 alpha:1];
self.cellName.font = [UIFont fontWithName:#"HelveticaNeue" size:14];
[cell.contentView addSubview:self.cellName];
if (indexPath.section == 1) {
cell.selectionStyle = UITableViewCellSelectionStyleNone;
self.cellName.text = [notArray objectAtIndex:indexPath.row];
if (indexPath.row == 0) {
self.acceptanceSwitch = [[UISwitch alloc] initWithFrame:CGRectMake(251, 6, 31, 51)];
[cell.contentView addSubview:self.acceptanceSwitch];
} else if (indexPath.row == 1) {
self.messageSwitch = [[UISwitch alloc] initWithFrame:CGRectMake(251, 6, 31, 51)];
[cell.contentView addSubview:self.messageSwitch];
} else if (indexPath.row == 2) {
self.likeSwitch = [[UISwitch alloc] initWithFrame:CGRectMake(251, 6, 31, 51)];
[cell.contentView addSubview:self.likeSwitch];
}
} else if (indexPath.section == 0) {
cell.selectionStyle = UITableViewCellSelectionStyleNone;
self.cellName.text = [accountArray objectAtIndex:indexPath.row];
if (indexPath.row == 0) {
self.nameTextField = [[UITextField alloc] initWithFrame:CGRectMake(110, 7, 190, 30)];
self.nameTextField.enabled = NO;
[cell.contentView addSubview:self.nameTextField];
} else if (indexPath.row == 1) {
self.emailTextField = [[UITextField alloc] initWithFrame:CGRectMake(110, 7, 190, 30)];
[cell.contentView addSubview:self.emailTextField];
[self.emailTextField setReturnKeyType:UIReturnKeyDone];
[self.emailTextField setKeyboardType:UIKeyboardTypeEmailAddress];
} else if (indexPath.row == 2) {
self.genderSwitch = [[UISwitch alloc] initWithFrame:CGRectMake(251, 6, 31, 51)];
[cell.contentView addSubview:self.genderSwitch];
}
}
}
if (indexPath.section == 0) {
cell.selectionStyle = UITableViewCellSelectionStyleNone;
self.cellName.text = [accountArray objectAtIndex:indexPath.row];
} else if (indexPath.section == 1) {
cell.selectionStyle = UITableViewCellSelectionStyleNone;
self.cellName.text = [notArray objectAtIndex:indexPath.row];
} else if (indexPath.section == 2) {
self.cellName.text = [policyArray objectAtIndex:indexPath.row];
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
} else if (indexPath.section == 3) {
self.cellName.text = [buttonsArray objectAtIndex:indexPath.row];
self.cellName.textAlignment = NSTextAlignmentCenter;
}
return cell;
}
Please try the following:
create a subclass of UITableViewCell with public properties (label, textfield). If you have different requirement for cells in different sections, maybe you should have different cell subclasses.
register these cell classes with the tableview in your view did load. e.g.
[self.tableView registerClass:[MySectionOneTableViewCell class] forCellReuseIdentifier:MySectionOneIdentifier];
[self.tableView registerClass:[MySectionTwoTableViewCell class] forCellReuseIdentifier:MySectionTwoIdentifier];
in the method below, just dequeue the cell after making a check for the section and set the properties.
(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath )indexPath {
UITableViewCell cell;
if (indexPath.section == 0){
cell = (MySectionOneTableViewCell*)[tableView dequeueReusableCellWithIdentifier:MySectionOneIdentifier forIndexPath:indexPath;
cell.textLabel = #"foo";
}
return cell;
}
implement method, prepare for reuse in your custom uitableviewcell classes where you initialize all your labels and text fields. This method prepares the views of the cell for reuse.
e.g.
(void)prepareForReuse {
self.textLabel.text = #"";
}
As mentionned #rmaddy, you shouldn't mention the cell generation in your (!cell). The "code illustration" is the following:
- (void)viewDidLoad {
// This part wasn't included in any specific if/else condition dealing with cell properties (like section or row), not either being modified during the cell generation
self.emailTextField.delegate = self;
self.cellName = [[UILabel alloc] initWithFrame:CGRectMake(20, 12, 42, 20)];
self.cellName.textColor = [UIColor colorWithRed:0.137 green:0.145 blue:0.157 alpha:1];
self.cellName.font = [UIFont fontWithName:#"HelveticaNeue" size:14];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *MyIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:MyIdentifier];
// This part ensure you will initiate each cell with the correct identifier
if (!cell)
{
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:MyIdentifier];
cell.backgroundColor = [UIColor colorWithRed:0.953 green:0.953 blue:0.965 alpha:1] ;
}
[cell.contentView addSubview:self.cellName];
// You should check your code and commute the common condition together
if (indexPath.section == 0) {
cell.selectionStyle = UITableViewCellSelectionStyleNone;
self.cellName.text = [accountArray objectAtIndex:indexPath.row];
// Be aware that your first section will contain only 3 rows
if (indexPath.row == 0) {
self.nameTextField = [[UITextField alloc] initWithFrame:CGRectMake(110, 7, 190, 30)];
self.nameTextField.enabled = NO;
[cell.contentView addSubview:self.nameTextField];
} else if (indexPath.row == 1) {
self.emailTextField = [[UITextField alloc] initWithFrame:CGRectMake(110, 7, 190, 30)];
[cell.contentView addSubview:self.emailTextField];
[self.emailTextField setReturnKeyType:UIReturnKeyDone];
[self.emailTextField setKeyboardType:UIKeyboardTypeEmailAddress];
} else if (indexPath.row == 2) {
self.genderSwitch = [[UISwitch alloc] initWithFrame:CGRectMake(251, 6, 31, 51)];
[cell.contentView addSubview:self.genderSwitch];
}
} else if (indexPath.section == 1) {
cell.selectionStyle = UITableViewCellSelectionStyleNone;
self.cellName.text = [notArray objectAtIndex:indexPath.row];
// Be aware that your second section will contain only 3 rows
if (indexPath.row == 0) {
self.acceptanceSwitch = [[UISwitch alloc] initWithFrame:CGRectMake(251, 6, 31, 51)];
[cell.contentView addSubview:self.acceptanceSwitch];
} else if (indexPath.row == 1) {
self.messageSwitch = [[UISwitch alloc] initWithFrame:CGRectMake(251, 6, 31, 51)];
[cell.contentView addSubview:self.messageSwitch];
} else if (indexPath.row == 2) {
self.likeSwitch = [[UISwitch alloc] initWithFrame:CGRectMake(251, 6, 31, 51)];
[cell.contentView addSubview:self.likeSwitch];
}
} else if (indexPath.section == 2) {
// Here you are not adding any specific content to your section 2
self.cellName.text = [policyArray objectAtIndex:indexPath.row];
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
} else if (indexPath.section == 3) {
// The same, your fourth section won't content any specific content
// You should add something like [cell addSubview:MyCustomView]
self.cellName.text = [buttonsArray objectAtIndex:indexPath.row];
self.cellName.textAlignment = NSTextAlignmentCenter;
}
return cell;
}
As I mentioned in my comments, if your local properties (cellName, nameTextField, genderSwitch, etc...) are well implemented, you will have only 6 row in all your table (3 in the first section and 3 in the second. Your third and fourth section will be empty since you didn't mention any subview within them.
Finally, since your code contained many confusion for me, it is possible I made some mistake in my rewriting, so feel free to correct me in this case.
Hopping it will help you in your work.

Loading text in Label inside cell UITableView

I have defined 3 sections with different numbers of cell on each. It works but when editing the text still there. I tried to clear the content when start typing but didn't work.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
// Configure the cell...
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
UITextField *fdates = [[UITextField alloc] initWithFrame:CGRectMake(110, 10, 185, 30)];
fdates.adjustsFontSizeToFitWidth = YES;
UITextField *fdeparture= [[UITextField alloc] initWithFrame:CGRectMake(110, 10, 185, 30)];
fdeparture.adjustsFontSizeToFitWidth = YES;
UITextField *farrival = [[UITextField alloc] initWithFrame:CGRectMake(110, 10, 185, 30)];
farrival.adjustsFontSizeToFitWidth = YES;
UITextField *faircraft = [[UITextField alloc] initWithFrame:CGRectMake(110, 10, 185, 30)];
faircraft.adjustsFontSizeToFitWidth = YES;
if ([indexPath section] == 0) {
if ([indexPath row] == 0) {
fdates.text = fplan.dates;
fdates.keyboardType = UIKeyboardTypeNumbersAndPunctuation;
fdates.returnKeyType = UIReturnKeyNext;
}
}else if ([indexPath section] == 1) {
if (indexPath.row == 0) {
fdeparture.text = fplan.depAirport;
fdeparture.keyboardType = UIKeyboardTypeDefault;
fdeparture.returnKeyType = UIReturnKeyNext;
}else {
farrival.text = fplan.destAirport;
farrival.keyboardType = UIKeyboardTypeDefault;
farrival.returnKeyType = UIReturnKeyNext;
}
}else {
if (indexPath.row == 0) {
faircraft.text = fplan.aircraft_id;
faircraft.keyboardType = UIKeyboardTypeDefault;
faircraft.returnKeyType = UIReturnKeyNext;
// faircraft.
}
}
[cell.contentView addSubview:fdates];
[cell.contentView addSubview:fdeparture];
[cell.contentView addSubview:farrival];
[cell.contentView addSubview:faircraft];
return cell;
}
To be more clear:
Thanks!!
try following code. i have edited your code use this
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
// Configure the cell...
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
UITextField *fdates = [[UITextField alloc] initWithFrame:CGRectMake(110, 10, 185, 30)];
fdates.adjustsFontSizeToFitWidth = YES;
if ([indexPath section] == 0) {
if ([indexPath row] == 0) {
fdates.text = fplan.dates;
fdates.keyboardType = UIKeyboardTypeNumbersAndPunctuation;
fdates.returnKeyType = UIReturnKeyNext;
}
}else if ([indexPath section] == 1) {
if (indexPath.row == 0) {
fdates.text = fplan.depAirport;
fdates.keyboardType = UIKeyboardTypeDefault;
fdates.returnKeyType = UIReturnKeyNext;
}else {
fdates.text = fplan.destAirport;
fdates.keyboardType = UIKeyboardTypeDefault;
fdates.returnKeyType = UIReturnKeyNext;
}
}else {
if (indexPath.row == 0) {
fdates.text = fplan.aircraft_id;
fdates.keyboardType = UIKeyboardTypeDefault;
fdates.returnKeyType = UIReturnKeyNext;
// faircraft.
}
}
[cell.contentView addSubview:fdates];
return cell;
}
Try to add on cell not on content view....for more info see below....
[cell addSubview:fdates];
[cell addSubview:fdeparture];
[cell addSubview:farrival];
[cell addSubview:faircraft];

Placeholders reappear when scrolling uitextField out of view - ios

I have a uiview consisting of a map view on top, and a uitableview below. the table view has cells with uitextFields in them with placeholders.
When I edit the fields the placeholder disappears correctly, but if I scroll the uitableviewcells out of sight (i.e. "behind" the map view that sits above the table view), the placeholders reappear together with the content that was written into the uitextfield.
If I replace the placeholders with normal text, and clear the text on textFieldDidBeginEditing, the same ghosting effect happens. Both the placeholder text and the input shows.
I have tried setting placeholders and text to nil in textFieldDidBeginEditing, but to no avail.
The table view uses michaeltyson's TPKeyboardAvoidingTableView.
edit
Added my cellForRowAtIndex:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:[NSString stringWithFormat:#"Cell%d", indexPath.row ]];
cell.textLabel.text = _tableData[indexPath.row];
if (indexPath.row == 0) {
UITextField *textField = [[UITextField alloc] initWithFrame:CGRectMake(110, 10, 185, 30)];
textField.delegate = self;
textField.placeholder = #"(Required)";
[cell.contentView addSubview:textField];
} else if (indexPath.row == 1) {
UILabel *textField = [[UILabel alloc] initWithFrame:CGRectMake(110, 6, 185, 30)];
textField.text = #"(Required)";
textField.textColor = [UIColor lightGrayColor];
textField.backgroundColor = [UIColor clearColor];
[cell.contentView addSubview:textField];
secondCellField = textField;
secondCell = cell;
} else if (indexPath.row == 2) {
UITextField *textField = [[UITextField alloc] initWithFrame:CGRectMake(110, 10, 185, 30)];
textField.delegate = self;
textField.placeholder = #"(Optional)";
[cell.contentView addSubview:textField];
} else if (indexPath.row == 3) {
UITextField *textField = [[UITextField alloc] initWithFrame:CGRectMake(110, 10, 185, 30)];
textField.delegate = self;
textField.placeholder = #"(Optional)";
[cell.contentView addSubview:textField];
} else if (indexPath.row == 4) {
UITextField *textField = [[UITextField alloc] initWithFrame:CGRectMake(110, 10, 185, 30)];
textField.delegate = self;
textField.placeholder = #"(Optional)";
[cell.contentView addSubview:textField];
} else if (indexPath.row == 5) {
UITextField *textField = [[UITextField alloc] initWithFrame:CGRectMake(110, 10, 185, 30)];
textField.delegate = self;
textField.placeholder = #"(Optional)";
[cell.contentView addSubview:textField];
} else if (indexPath.row == 6) {
UISwitch *textField = [[UISwitch alloc] initWithFrame:CGRectMake(210, 8, 50, 30)];
[textField addTarget:self action:#selector(segwayToWork) forControlEvents:UIControlEventValueChanged];
[cell.contentView addSubview:textField];
_workSwitch = textField;
}
return cell;
Your problem is mutiple TextFields are created for different cell. You need to use reusable cells
Like:
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:#"Cell"];
if (cell == nil) {
// allocate the cell:
cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:CellIdentifier] autorelease];
if (indexPath.row == 0) {
UITextField *textField = [[UITextField alloc] initWithFrame:CGRectMake(110, 10, 185, 30)];
textField.delegate = self;
textField.tag = 1111;//tag
textField.placeholder = #"(Required)";
[cell.contentView addSubview:textField];
} else if (indexPath.row == 1) {
UILabel *textField = [[UILabel alloc] initWithFrame:CGRectMake(110, 6, 185, 30)];
textField.text = #"(Required)";
textField.tag = 1111;//tag
textField.textColor = [UIColor lightGrayColor];
textField.backgroundColor = [UIColor clearColor];
[cell.contentView addSubview:textField];
secondCellField = textField;
secondCell = cell;
} else if (indexPath.row == 2) {
UITextField *textField = [[UITextField alloc] initWithFrame:CGRectMake(110, 10, 185, 30)];
textField.delegate = self;
textField.tag = 1111;//tag
textField.placeholder = #"(Optional)";
[cell.contentView addSubview:textField];
} else if (indexPath.row == 3) {
UITextField *textField = [[UITextField alloc] initWithFrame:CGRectMake(110, 10, 185, 30)];
textField.delegate = self;
textField.tag = 1111;//tag
textField.placeholder = #"(Optional)";
[cell.contentView addSubview:textField];
} else if (indexPath.row == 4) {
UITextField *textField = [[UITextField alloc] initWithFrame:CGRectMake(110, 10, 185, 30)];
textField.delegate = self;
textField.tag = 1111;//tag
textField.placeholder = #"(Optional)";
[cell.contentView addSubview:textField];
} else if (indexPath.row == 5) {
UITextField *textField = [[UITextField alloc] initWithFrame:CGRectMake(110, 10, 185, 30)];
textField.tag = 1111;//tag
textField.delegate = self;
textField.placeholder = #"(Optional)";
[cell.contentView addSubview:textField];
} elseif (indexPath.row == 6) {
UISwitch *textField = [[UISwitch alloc] initWithFrame:CGRectMake(210, 8, 50, 30)];
textField.tag = 1111;//tag
[textField addTarget:self action:#selector(segwayToWork) forControlEvents:UIControlEventValueChanged];
[cell.contentView addSubview:textField];
_workSwitch = textField;
}
}
cell.textLabel.text = _tableData[indexPath.row];
return cell;
}
I have tryied to reuse UITableViewCell So please customize it as per your requirement. you can use this code also.
Creating new text fields is indeed the problem. You should NOT alloc/init any text fields in cellForRowAtIndexPath.
Instead, either make a custom cell or use the "dynamic prototype" in your storyboard file. Give your text field a tag so you can easily configure it.
//above
#define TextFieldTag 42
// in cellForRowAtIndexPath:
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:#"Cell"];
// If #"Cell" is the identifier you specified
// if you have storyboard or a xib, no need to check for cell==nil
UITextField *textField = (UITextField*) [cell viewWithTag:TextFieldTag];
// configure the text field based on indexPath.row
In case you are not using storyboard or xibs, you can lazily instantiate the text field like this:
UITextField *textField = (UITextField*) [cell viewWithTag:TextFieldTag];
if (!textField) {
textField = [[UITextField alloc] initWithFrame:textFieldFrame];
textField.tag = TextFieldTag;
// more standard configuration
[cell.contentView addSubview:textField];
}
And by the way, here is another tip for you. You should not hard code your table view rows. This results in not very readable code. You can use a simple enum on top of your class:
typedef enum {
NameCell, AddressCell,
FirstOptionalCell, SecondOptionalCell,
ThirdOptionalCell, CellCount } TableCell;
You can then just return "CellCount" in numberOrRowsInSection and reference the rows like this:
if (indexPath.row == NameCell)
The problem here is that everytime a cell gets reloaded, you initialize a new text view which won't have the text that was previously entered. The best solution to this problem is for you to keep an NSMutableArray* that stores the user's input. You have different options, i'll just describe the steps for one possible solution:
declare an NSMutableArray property for storing the user inputs as follows:
#property (strong, nonatomic) NSMutableArray *userData;
and the getter that performs lazy initialisation:
-(NSMutableArray *)userData
{
if(!_userData){
_userData = [[NSMutableArray alloc] initWithCapacity:[self.tableData count]];
for (int i = 0; i < [self.tableData count]; i++)
[_userData addObject:#""];
}
return _userData;
}
Use the textfield's property tag to store the row for the particular textfield as follows:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:[NSString stringWithFormat:#"Cell%d", indexPath.row ]];
if(cell == nil)
cell = [[UITableViewCell alloc] init];
cell.textLabel.text = _tableData[indexPath.row];
UITextField *textField = [[UITextField alloc] initWithFrame:CGRectMake(110, 10, 185, 30)];
textField.delegate = self;
textField.tag = indexPath.row;
if (indexPath.row == 0) {
textField.placeholder = #"(Required)";
[cell.contentView addSubview:textField];
} else if (indexPath.row == 1) {
UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(110, 6, 185, 30)];
label.text = #"(Required)";
label.textColor = [UIColor lightGrayColor];
label.backgroundColor = [UIColor clearColor];
[cell.contentView addSubview:label];
secondCellField = label;
secondCell = cell;
} else if (indexPath.row == 2) {
textField.placeholder = #"(Optional)";
[cell.contentView addSubview:textField];
} else if (indexPath.row == 3) {
textField.placeholder = #"(Optional)";
[cell.contentView addSubview:textField];
} else if (indexPath.row == 4) {
textField.placeholder = #"(Optional)";
[cell.contentView addSubview:textField];
} else if (indexPath.row == 5) {
textField.placeholder = #"(Optional)";
[cell.contentView addSubview:textField];
} else if (indexPath.row == 6) {
UISwitch *switch = [[UISwitch alloc] initWithFrame:CGRectMake(210, 8, 50, 30)];
[switch addTarget:self action:#selector(segwayToWork) forControlEvents:UIControlEventValueChanged];
[cell.contentView addSubview:switch];
_workSwitch = switch;
}
if(![[self.userData objectAtIndex:indexPath.row] isEqualToString:#""]){
NSLog(#"%# at indexPath.row %d",[self.userData objectAtIndex:indexPath.row], indexPath.row);
textField.placeholder = nil;
textField.text = [self.userData objectAtIndex:indexPath.row];
}
return cell;
}
Finally, in your UITextField delegate, you can do something like:
- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
[textField resignFirstResponder];
self.userData[textField.tag] = textField.text;
return YES;
}
Now when a cell will be reloaded, it will first check whether there was already a user input, and if there was one, it will set the placeholder to nil and will instead show just the user input.

Grouped Table view in Iphone

I m using grouped table view in my app. I have 2 sections in table. first section has 3 rows and second section has more than 10 rows .when I scrolled sections it displays rows from section 1 after 5 th row of section second. what should I do.
Here is my code...
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
// Return the number of sections.
return 2;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
// Return the number of rows in the section.
switch (section) {
case 0:
return 3;
break;
case 1:
return 8;
break;
default:
break;
}
return 5;
}
// Customize the appearance of table view cells.
- (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] autorelease];
}
if (indexPath.section==0)
{
if (indexPath.row==0)
{
UILabel *Name=[[UILabel alloc]initWithFrame:CGRectMake(10, 10, 100, 20)];
[Name setText:#"First Name "];
[cell.contentView addSubview:Name];
UITextField *textName=[[UITextField alloc]initWithFrame:CGRectMake(140, 10, 150, 20)];
textName.placeholder=#"Enter First Name";
[textName setBorderStyle:UITextBorderStyleNone];
textName.clearButtonMode = UITextFieldViewModeWhileEditing;
textName.keyboardType=UIKeyboardTypeDefault;
textName.returnKeyType=UIReturnKeyDone;
textName.delegate=self;
[cell.contentView addSubview:textName];
}
if (indexPath.row==1)
{
UILabel *Name=[[UILabel alloc]initWithFrame:CGRectMake(10, 10, 110, 20)];
[Name setText:#"Middle Name"];
[cell.contentView addSubview:Name];
UITextField *textName=[[UITextField alloc]initWithFrame:CGRectMake(140, 10, 150, 20)];
textName.placeholder=#"Enter Middle Name";
[textName setBorderStyle:UITextBorderStyleNone];
textName.clearButtonMode = UITextFieldViewModeWhileEditing;
textName.keyboardType=UIKeyboardTypeDefault;
textName.returnKeyType=UIReturnKeyDone;
textName.delegate=self;
[cell.contentView addSubview:textName];
}
if (indexPath.row==2)
{
UILabel *Name=[[UILabel alloc]initWithFrame:CGRectMake(10, 10, 100, 20)];
[Name setText:#"Last Name "];
[cell.contentView addSubview:Name];
UITextField *textName=[[UITextField alloc]initWithFrame:CGRectMake(140, 10, 150, 20)];
textName.placeholder=#"Enter Last Name";
[textName setBorderStyle:UITextBorderStyleNone];
textName.clearButtonMode = UITextFieldViewModeWhileEditing;
textName.keyboardType=UIKeyboardTypeDefault;
textName.returnKeyType=UIReturnKeyDone;
textName.delegate=self;
[cell.contentView addSubview:textName];
}
}
if (indexPath.section==1) {
if (indexPath.row==0) {
cell.textLabel.text=#"1";
}
if (indexPath.row==1) {
cell.textLabel.text=#"2";
}
if (indexPath.row==2) {
cell.textLabel.text=#"3";
}
if (indexPath.row==3) {
cell.textLabel.text=#"4";
}
if (indexPath.row==4) {
cell.textLabel.text=#"5";
}
if (indexPath.row==5) {
cell.textLabel.text=#"6";
}
if (indexPath.row==6) {
cell.textLabel.text=#"7";
}
if (indexPath.row==7) {
cell.textLabel.text=#"8";
}
}
return cell;
}
You need 2 Cell identifiers. The problem is that you dequeue cells with the same identifier.
Paste the code and should work fine:D
P.S: I did not resolve your problems with your labels. You alloc them each time you display an cell. That is wrong.
#pragma mark -
#pragma mark Table view data source
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
// Return the number of sections.
return 2;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
// Return the number of rows in the section.
NSInteger returnValue = 5;
switch (section) {
case 0:
{
returnValue = 3;
break;
}
case 1:
{
returnValue = 8;
break;
}
default:
break;
}
return returnValue;
}
// Customize the appearance of table view cells.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifierSection1 = #"Cell1";
static NSString *CellIdentifierSection2 = #"Cell2";
UITableViewCell *cell;
if (indexPath.section==0)
{
cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifierSection1];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifierSection1] autorelease];
}
if (indexPath.row==0)
{
UILabel *Name=[[UILabel alloc]initWithFrame:CGRectMake(10, 10, 100, 20)];
[Name setText:#"First Name "];
[cell.contentView addSubview:Name];
UITextField *textName=[[UITextField alloc]initWithFrame:CGRectMake(140, 10, 150, 20)];
textName.placeholder=#"Enter First Name";
[textName setBorderStyle:UITextBorderStyleNone];
textName.clearButtonMode = UITextFieldViewModeWhileEditing;
textName.keyboardType=UIKeyboardTypeDefault;
textName.returnKeyType=UIReturnKeyDone;
textName.delegate=self;
[cell.contentView addSubview:textName];
}
if (indexPath.row==1)
{
UILabel *Name=[[UILabel alloc]initWithFrame:CGRectMake(10, 10, 110, 20)];
[Name setText:#"Middle Name"];
[cell.contentView addSubview:Name];
UITextField *textName=[[UITextField alloc]initWithFrame:CGRectMake(140, 10, 150, 20)];
textName.placeholder=#"Enter Middle Name";
[textName setBorderStyle:UITextBorderStyleNone];
textName.clearButtonMode = UITextFieldViewModeWhileEditing;
textName.keyboardType=UIKeyboardTypeDefault;
textName.returnKeyType=UIReturnKeyDone;
textName.delegate=self;
[cell.contentView addSubview:textName];
}
if (indexPath.row==2)
{
UILabel *Name=[[UILabel alloc]initWithFrame:CGRectMake(10, 10, 100, 20)];
[Name setText:#"Last Name "];
[cell.contentView addSubview:Name];
UITextField *textName=[[UITextField alloc]initWithFrame:CGRectMake(140, 10, 150, 20)];
textName.placeholder=#"Enter Last Name";
[textName setBorderStyle:UITextBorderStyleNone];
textName.clearButtonMode = UITextFieldViewModeWhileEditing;
textName.keyboardType=UIKeyboardTypeDefault;
textName.returnKeyType=UIReturnKeyDone;
textName.delegate=self;
[cell.contentView addSubview:textName];
}
}
if (indexPath.section==1) {
cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifierSection2];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifierSection2] autorelease];
}
if (indexPath.row==0) {
cell.textLabel.text=#"1";
}
if (indexPath.row==1) {
cell.textLabel.text=#"2";
}
if (indexPath.row==2) {
cell.textLabel.text=#"3";
}
if (indexPath.row==3) {
cell.textLabel.text=#"4";
}
if (indexPath.row==4) {
cell.textLabel.text=#"5";
}
if (indexPath.row==5) {
cell.textLabel.text=#"6";
}
if (indexPath.row==6) {
cell.textLabel.text=#"7";
}
if (indexPath.row==7) {
cell.textLabel.text=#"8";
}
}
return cell;
}
Edit:
You can use different identifier for each type of cell. So if you have 2 types of cell you use 2 identifiers if you have 1 type use one identifier and so on. The identifier is used to dequeue a specific type of cell, if no cell can be reused it will simply alloc a new one.

Resources