UITextField resignFirstResponder working a bit then not - ios

I add in a UiTableViewCell a UITextField *gasPrice; like this:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
NSString *CellIdentifier;
if (indexPath.section == 0 && indexPath.row < [self.userCarsArray count])
CellIdentifier = [NSString stringWithFormat:#"Cell%d%d%#", indexPath.section, indexPath.row, [[self.userCarsArray objectAtIndex:indexPath.row] idCar]];
else if (indexPath.section == 0 && indexPath.row == [self.userCarsArray count])
CellIdentifier = [NSString stringWithFormat:#"Cell%d%d%#", indexPath.section, indexPath.row, #"AddCar"];
else
CellIdentifier = [NSString stringWithFormat:#"Cell%d%d", indexPath.section, indexPath.row];
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
if (indexPath.section == 1 && indexPath.row == 0) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:CellIdentifier] autorelease];
}
else if (indexPath.section == 2 && indexPath.row == 2) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:CellIdentifier] autorelease];
}
else {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
}
if (indexPath.section == 1 && indexPath.row == 1) {
UITextField *textField = [[UITextField alloc] initWithFrame:CGRectMake(170, 10, 125, 30)];
textField.adjustsFontSizeToFitWidth = YES;
textField.textColor = [UIColor colorWithRed:0.20f green:0.30f blue:0.49f alpha:1.0f];
textField.placeholder = #"0.00";
textField.keyboardType = UIKeyboardTypeDecimalPad;
textField.returnKeyType = UIReturnKeyDone;
textField.backgroundColor = [UIColor whiteColor];
textField.autocorrectionType = UITextAutocorrectionTypeNo; // no auto correction support
textField.autocapitalizationType = UITextAutocapitalizationTypeNone; // no auto capitalization support
textField.textAlignment = UITextAlignmentRight;
textField.tag = 0;
textField.delegate = self;
textField.clearButtonMode = UITextFieldViewModeNever; // no clear 'x' button to the right
[textField setEnabled: YES];
[cell addSubview:textField];
[textField release];
gasField = textField;
}
}
// Selection style.
cell.selectionStyle = UITableViewCellSelectionStyleGray;
// Vehicles cells.
if (indexPath.section == 0) {
// ...
}
// General cells.
if (indexPath.section == 1) {
if (indexPath.row == 0) {
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
cell.textLabel.text = #"Measurement System";
cell.textLabel.textColor = [UIColor darkGrayColor];
if ([EcoAppAppDelegate measurement] == MeasurementTypeMile)
cell.detailTextLabel.text = #"Miles";
else
cell.detailTextLabel.text = #"Meters";
}
if (indexPath.row == 1) {
cell.textLabel.textColor = [UIColor darkGrayColor];
cell.selectionStyle = UITableViewCellSelectionStyleNone;
if ([EcoAppAppDelegate measurement] == MeasurementTypeMile)
cell.textLabel.text = #"Gas Price (gal)";
else
cell.textLabel.text = #"Gas Price (L)";
// Gas price.
if ([EcoAppAppDelegate gasPrice] != 0.00)
gasField.text = [NSString stringWithFormat:#"%.2f", [EcoAppAppDelegate gasPrice]];
}
}
// Information cells.
if (indexPath.section == 2) {
// ...
}
return cell;
}
Then, to manage the keyboard when the user is done (because I use a keyboard without return key), I have those functions:
- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField {
UIBarButtonItem *doneButton = [[[UIBarButtonItem alloc]
initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:self action:#selector(done:)] autorelease];
[self.navigationItem setLeftBarButtonItem:doneButton animated:YES];
return YES;
}
- (BOOL)textFieldShouldEndEditing:(UITextField *)textField {
[self.navigationItem setLeftBarButtonItem:nil animated:YES];
// Save gas price.
if ([textField.text isEqualToString:#""])
[EcoAppAppDelegate setGasPrice:0.00];
else
[EcoAppAppDelegate setGasPrice:[textField.text floatValue]];
return YES;
}
- (BOOL)textFieldShouldReturn:(UITextField *)textField {
// Hide keyboard.
[textField resignFirstResponder];
return YES;
}
And the function for the navigation bar button "Done":
- (void)done:(id)sender {
if ([gasField canResignFirstResponder]) {
[gasField resignFirstResponder];
}
[self.navigationItem setLeftBarButtonItem:nil animated:YES];
}
So it works fine first, when I stay in my settings view controller, but when I do other stuff on some other cells (that push other view controllers, come back, etc), it stops working. It is probably because the gasField pointer has disappeared or something like that, the gasField object tho is still different from nil.
Any idea why it does that? Thanks!

I think you are using gasField as a property. use
self.gasField

Related

UITableView cells in side menu disappear

I am having some odd behavior in a side menu. Here is what it should look like:
After I click the button to the right of Organization I use this code to show some more cells
[self.tableView reloadSections:[NSIndexSet indexSetWithIndex:0] withRowAnimation:UITableViewRowAnimationFade];
If I navigate from the menu with the organization cells showing and then navigate back to the menu it shows this:
The cells show as hidden, but when I set the cells to not hidden like this:
cell.hidden = NO;
it still shows as hidden.
Any ideas as to what is going on? There is nowhere in my code that I set any cells as hidden.
If there is more code you need, then let me know.
Thanks
Here is some more code:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *cellIdentifier = #"MenuCell";
MenuTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
if (cell == nil)
{
cell = [[MenuTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
}
if (indexPath.section == 0)
{
if (showOrgs && indexPath.row < organizations.count)
{
cell.label.text = [organizations objectAtIndex:indexPath.row];
cell.label.textColor = [UIColor whiteColor];
cell.leftImageView.hidden = YES;
cell.contentView.backgroundColor = [UIColor colorWithHex:#"#ff6b01" alpha:1.0];
cell.tag = 10002;
cell.leftButton.hidden = YES;
cell.righttButton.hidden = YES;
cell.leftButton.enabled = NO;
cell.righttButton.enabled = NO;
}
else if (showOrgs && indexPath.row >= organizations.count)
{
if (indexPath.row == organizations.count)
{
cell.label.text = [allUpper objectAtIndex:indexPath.row];//[organizations objectAtIndex:indexPath.row];
cell.contentView.backgroundColor = [UIColor whiteColor];
cell.label.textColor = [UIColor blackColor];
cell.tag = 10001;
[cell.leftImageView setImage:[UIImage imageNamed:#"dashboard"]];
cell.leftImageView.hidden = NO;
}
else
{
cell.label.text = [allUpper objectAtIndex:indexPath.row];
cell.label.textColor = [UIColor blackColor];
cell.contentView.backgroundColor = [UIColor whiteColor];
cell.tag = 10003;
if ([[allUpper objectAtIndex:indexPath.row] isEqualToString:#"All Media"])
{
cell.leftImageView.image = [UIImage imageNamed:#"image-btn"];
cell.leftImageView.hidden = NO;
}
}
}
NSLog(#"Row: %li", (long)indexPath.row);
if (!showOrgs && [[upperSection objectAtIndex:indexPath.row] isEqualToString:#"Dashboard"])
{
cell.label.text = [upperSection objectAtIndex:indexPath.row];//[organizations objectAtIndex:indexPath.row];
cell.contentView.backgroundColor = [UIColor whiteColor];
cell.tag = 10001;
cell.hidden = NO;
[cell.leftImageView setImage:[UIImage imageNamed:#"dashboard"]];
}
else if (!showOrgs && [[upperSection objectAtIndex:indexPath.row] isEqualToString:#"All Media"])
{
cell.label.text = [upperSection objectAtIndex:indexPath.row];
cell.contentView.backgroundColor = [UIColor whiteColor];
cell.tag = 10003;
cell.hidden = NO;
if ([[upperSection objectAtIndex:indexPath.row] isEqualToString:#"All Media"])
{
cell.leftImageView.image = [UIImage imageNamed:#"image-btn"];
}
}
}
// if (indexPath.section == 0 && showOrgs)
// cell.label.text = [organizations objectAtIndex:indexPath.row];
else if (indexPath.section == 1)
cell.label.text = [currentEvents objectAtIndex:indexPath.row];
else
cell.label.text = [allEvents objectAtIndex:indexPath.row];
return cell;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
if (section == 0 && !showOrgs)
return upperSection.count;
else if (section == 0 && showOrgs)
return allUpper.count;
else if (section == 1)
return currentEvents.count;
else
return allEvents.count;
}
I made some changes to the blocks following the suggestion of wottle and then added this to the end of the block for when not showing orgs
cell.leftButton.hidden = NO;
cell.righttButton.hidden = NO;
cell.leftButton.enabled = YES;
cell.righttButton.enabled = YES;
cell.leftImageView.hidden = NO;
cell.label.hidden = NO;
cell.label.textColor = [UIColor blackColor];
That seems to have solved it. Thanks for all your help.

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];

UISwitch status change overlap

I have a problem with a UISwitch in a tableview cell.
This switch is normally in On position, then when a variable changes its value the switch go to the Off position but I have a problem like the switch in the second row:
This is the simple code into the cellForRowAtIndexPath section:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = #"Cell";
tableView.backgroundColor = [UIColor clearColor];
tableView.opaque = NO;
tableView.backgroundView = nil;
[tableView setBackgroundColor:[UIColor colorWithPatternImage:[UIImage imageNamed:#"myImage.png"]]];
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
if ([self.title isEqualToString:#"Test"]) {cell.textLabel.text = [self.tester objectAtIndex: [indexPath row]];cell.selectionStyle = UITableViewCellSelectionStyleNone; cell.accessoryType = UITableViewCellAccessoryNone;
if (([indexPath row] == 5) || ([indexPath row] == 4)){
NSArray *segmentItems = [NSArray arrayWithObjects:#"Down", #"Up", nil];
UISegmentedControl *testSegmentedControl = [[UISegmentedControl alloc] initWithItems:segmentItems];
testSegmentedControl.autoresizingMask = UIViewAutoresizingFlexibleWidth;
testSegmentedControl.segmentedControlStyle = UISegmentedControlStyleBar;
testSegmentedControl.tintColor = [UIColor grayColor];
testSegmentedControl.frame = CGRectMake(160, 10, 150, 30);
[testSegmentedControl setTag:[indexPath row]];
[[cell contentView] addSubview:testSegmentedControl];
[testSegmentedControl addTarget:self
action:#selector(testing:)
forControlEvents:UIControlEventValueChanged];
}
else {
mySwitch = [ [ UISwitch alloc ] initWithFrame: CGRectMake(200, 10, 0, 0) ];
mySwitch.tag = [indexPath row];
mySwitch.onTintColor = [UIColor grayColor];
if ([indexPath row] == 0){
if ([asd41 isEqualToString:#"False"]) mySwitch.on = NO;
else mySwitch.on = YES;
}
if ([indexPath row] == 1){
if ([asd40 isEqualToString:#"False"]) mySwitch.on = NO;
else mySwitch.on = YES;
}
if ([indexPath row] == 2){
if ([asd42 isEqualToString:#"False"]) mySwitch.on = NO;
else mySwitch.on = YES;
}
if ([indexPath row] == 3){
if ([asd76 isEqualToString:#"False"]) mySwitch.on = NO;
else mySwitch.on = YES;
}
[ cell addSubview: mySwitch ];
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration: 0.2];
[mySwitch addTarget:self action:#selector(switch1:) forControlEvents:UIControlEventValueChanged];}
}

Cells overlapping in table view

I am trying to achieve a UITableView with cells and textfields but the cells keep on overlapping and textfields are jumping from one section to another.
Can someone please check my code for any mistake that I have done?
listForItemsinfo =[[NSArray alloc]initWithObjects:#"Name",#"Price", nil];
listForBrandYear =[[NSArray alloc] initWithObjects:#"Year",#"Alfa",#"aston",#"audi",#"bentley",#"BMW",#"Cadillac",#"Chevrolet",#"Ferrari",#"Ford",#"Jaguar",#"Jeep",#"Kia",#"Landrover",#"Mazda",#"Mercedes",#"Mitsubishi",#"Nissan",#"Porshe",#"Subaru", nil];
listForkeywords =[[NSArray alloc] initWithObjects:#"KeyWord1",#"Keyword2", nil];
listForcarType =[[NSArray alloc] initWithObjects:#"Sedan",#"Convertible",#"Coupe",#"Heatchback", nil];
listForItemsCategory =[[NSArray alloc] initWithObjects:#"cat1",#"cat2",#"cat3",#"cat4",#"cat5",#"cat6", nil];
sections = [[NSArray alloc]initWithObjects:#"Item's info",#"Brand, Year",#"Keywords",#"Car Type",#"Category", nil];
listOfsections =[[NSArray alloc]initWithObjects:listForItemsinfo,listForBrandYear,listForkeywords,listForcarType,listForItemsCategory, nil];
- (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 (indexPath.section == 0) {
// Add a UITextField
UITextField *textField = [[UITextField alloc] init];
// Set a unique tag on each text field
textField.tag = 1+ indexPath.row;
// Add general UITextAttributes if necessary
textField.enablesReturnKeyAutomatically = YES;
textField.autocorrectionType = UITextAutocorrectionTypeNo;
textField.autocapitalizationType = UITextAutocapitalizationTypeNone;
[cell.contentView addSubview:textField];
}else if (indexPath.section == 1 ){
if (indexPath.row==0) {
UITextField *textField = [[UITextField alloc] init];
// Set a unique tag on each text field
textField.tag = 2+ indexPath.row;
// Add general UITextAttributes if necessary
textField.enablesReturnKeyAutomatically = YES;
textField.autocorrectionType = UITextAutocorrectionTypeNo;
textField.autocapitalizationType = UITextAutocapitalizationTypeNone;
[cell.contentView addSubview:textField];
}
}else if (indexPath.section == 2 ){
if (indexPath.row==0 | indexPath.row ==1) {
UITextField *textField = [[UITextField alloc] init];
// Set a unique tag on each text field
textField.tag = 3+ indexPath.row;
// Add general UITextAttributes if necessary
textField.enablesReturnKeyAutomatically = YES;
textField.autocorrectionType = UITextAutocorrectionTypeNo;
textField.autocapitalizationType = UITextAutocapitalizationTypeNone;
[cell.contentView addSubview:textField];
}else{
}
}
}
//cell.textLabel.text=#"wqewq";
//cell.textLabel.text = [[listOfsections objectAtIndex:indexPath.section] objectAtIndex:indexPath.row];
[self configureCell:cell atIndexPath:indexPath];
return cell;
}
- (void)configureCell:(UITableViewCell *)theCell atIndexPath:(NSIndexPath *)indexPath {
if (indexPath.section == 0) {
// Get the text field using the tag
UITextField *textField = (UITextField *)[theCell.contentView viewWithTag:1+indexPath.row];
// Position the text field within the cell bounds
CGRect cellBounds = theCell.bounds;
CGFloat textFieldBorder = 10.f;
// Don't align the field exactly in the vertical middle, as the text
// is not actually in the middle of the field.
CGRect aRect = CGRectMake(textFieldBorder, 9.f, CGRectGetWidth(cellBounds)-(2*textFieldBorder), 31.f );
textField.frame = aRect;
theCell.textLabel.text = NULL;
// Configure UITextAttributes for each field
if(indexPath.section == 0 & indexPath.row == 0) {
textField.placeholder = #"Name";
textField.returnKeyType = UIReturnKeyNext;
textField.autocapitalizationType = UITextAutocapitalizationTypeWords;
} else if(indexPath.section == 0 & indexPath.row == 1) {
textField.placeholder = #"Price";
textField.returnKeyType = UIReturnKeyNext;
textField.keyboardType = UIKeyboardTypeNumberPad;
}else if (indexPath.section ==0 & indexPath.row >1){
[textField removeFromSuperview];
}
}else if (indexPath.section == 1) {
// Get the text field using the tag
UITextField *textField = (UITextField *)[theCell.contentView viewWithTag:2+indexPath.row];
// Position the text field within the cell bounds
CGRect cellBounds = theCell.bounds;
CGFloat textFieldBorder = 10.f;
// Don't align the field exactly in the vertical middle, as the text
// is not actually in the middle of the field.
CGRect aRect = CGRectMake(textFieldBorder, 9.f, CGRectGetWidth(cellBounds)-(2*textFieldBorder), 31.f );
textField.frame = aRect;
// Configure UITextAttributes for each field
if(indexPath.section == 1 & indexPath.row == 0) {
textField.placeholder = #"Year";
textField.returnKeyType =UIReturnKeyDone;
textField.autocapitalizationType = UIKeyboardTypeNumberPad;
theCell.textLabel.text = NULL;
}else if(indexPath.section == 1 & indexPath.row != 0){
[textField removeFromSuperview];
theCell.textLabel.text = [[listOfsections objectAtIndex:indexPath.section] objectAtIndex:indexPath.row];
}
}else if (indexPath.section == 2) {
// Get the text field using the tag
UITextField *textField = (UITextField *)[theCell.contentView viewWithTag:3+indexPath.row];
// Position the text field within the cell bounds
CGRect cellBounds = theCell.bounds;
CGFloat textFieldBorder = 10.f;
// Don't align the field exactly in the vertical middle, as the text
// is not actually in the middle of the field.
CGRect aRect = CGRectMake(textFieldBorder, 9.f, CGRectGetWidth(cellBounds)-(2*textFieldBorder), 31.f );
textField.frame = aRect;
// Configure UITextAttributes for each field
if(indexPath.section == 2 & indexPath.row == 0) {
textField.placeholder = #"Keyword1";
textField.returnKeyType =UIReturnKeyDone;
textField.autocapitalizationType = UIKeyboardTypeNumberPad;
}else if(indexPath.section == 2 & indexPath.row == 1) {
textField.placeholder = #"Keyword2";
textField.returnKeyType =UIReturnKeyDone;
textField.autocapitalizationType = UIKeyboardTypeNumberPad;
}else{}
}
}
FIXED ! if anyone has the same problem (working code)
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
// Add a UITextField
UITextField *textField = [[UITextField alloc] init];
// Set a unique tag on each text field
textField.tag = 1 + indexPath.row;
textField.delegate=self;
// Add general UITextAttributes if necessary
textField.enablesReturnKeyAutomatically = YES;
textField.autocorrectionType = UITextAutocorrectionTypeNo;
textField.autocapitalizationType = UITextAutocapitalizationTypeNone;
// Position the text field within the cell bounds
CGRect cellBounds = cell.bounds;
CGFloat textFieldBorder = 10.f;
// Don't align the field exactly in the vertical middle, as the text
// is not actually in the middle of the field.
CGRect aRect = CGRectMake(textFieldBorder, 9.f, CGRectGetWidth(cellBounds)-(2*textFieldBorder), 31.f );
textField.frame = aRect;
textField.placeholder = #"Search for an item";
textField.returnKeyType = UIReturnKeyDefault;
textField.autocapitalizationType = UITextAutocapitalizationTypeWords;
if ((indexPath.section == 0) & (indexPath.row==0)) {
[cell.contentView addSubview:textField];
cell.textLabel.text=NULL;
}else{
[textField removeFromSuperview];
cell.textLabel.text = [[sections objectAtIndex:indexPath.section] objectAtIndex:indexPath.row];
}
The table won't make cells overlap, but cells do get reused. Your problem is that your only configuring the cell when you initialize it. In fact, you need to do that configuration whether you create a new cell or get an old one from -dequeueReusableCellWithIdentifier:.
Perhaps you were thinking that a table view saves every cell that you give it? That's not how it works... Reusable cells mean that a table only needs about as many cells as can be displayed at once. As you scroll the view, the cells that scroll off the top or bottom are recycled as new cells for new content. You're not setting the new content, though, so the old content shows up instead.
Add a close bracket after the first line of your if, so that the rest of the code is always executed:
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
if (indexPath.section == 0) {

Keeping text in UITextView after scrolling it off the screen

My table only has 2 sections. I have a UITextView as a subview in the 2nd section of my table. and a list of possible quotes in the first section.
I'm having a problem where once the user selects a particular quote which gets "pasted" into the UITextView like so:
replyTextView.text = [NSString stringWithFormat:#"#%# UserName writes... \n[\"%#\"]", replyPostCode,[separatedString objectAtIndex:indexPath.row]];
or types text into the textview, after they scroll away from the textview so it's off the screen it gets cleared. I guess this is because I keep releasing it from my table..
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = #"Cell";
NSString *replyCellIdentifier = #"replyCell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
if ([indexPath section] == 0) {
cell = [self CreateMultilinesCell:CellIdentifier];
}
else if ([indexPath section] == 1) {
//NSLog(#"TextField");
cell = [self CreateMultilinesCell:replyCellIdentifier];
if ([indexPath row] == 0) {
replyTextView = [[UITextView alloc] initWithFrame:CGRectMake(0, 0, 300, 150)];
//replyTextView.adjustsFontSizeToFitWidth = YES;
replyTextView.textColor = [UIColor blackColor];
replyTextView.keyboardType = UIKeyboardTypeASCIICapable;
replyTextView.returnKeyType = UIReturnKeyDefault;
replyTextView.backgroundColor = [UIColor whiteColor];
replyTextView.autocorrectionType = UITextAutocorrectionTypeNo;
replyTextView.autocapitalizationType = UITextAutocapitalizationTypeNone;
replyTextView.textAlignment = UITextAlignmentLeft;
replyTextView.tag = 0;
replyTextView.editable = YES;
replyTextView.delegate = self;
replyTextView.scrollEnabled = YES;
//[replyTextView becomeFirstResponder];
//replyTextView.clearButtonMode = UITextFieldViewModeNever;
//[replyTextView setEnabled: YES];
[cell.contentView addSubview:replyTextView];
[replyTextView release];
//cell.detailTextLabel.text = #"";
}
}
}
//NSLog(#"%d", [indexPath section]);
if ([indexPath section] == 0) {
cell.detailTextLabel.text = [separatedString objectAtIndex:indexPath.row];
}
return cell;
}
I'm just wondering just what is the best way to keep the text in my UITextView when the user scrolls the uitextview off the screen and back again?
update
- (UITableViewCell*) CreateMultilinesCell :(NSString*)cellIdentifier
{
//NSLog(#"Entering CreateMultilinesCell");
UITableViewCell *cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle
reuseIdentifier:cellIdentifier] autorelease];
cell.detailTextLabel.numberOfLines = 0;
cell.detailTextLabel.font = [self SubFont];
cell.detailTextLabel.textColor = [UIColor colorWithRed:10.0/255 green:10.0/255 blue:33.0/255 alpha:1.0];
[cell setBackgroundColor:[UIColor clearColor]];//]colorWithRed:.98 green:.98 blue:.99 alpha:1.0]];
[self.tableView setBackgroundColor:[UIColor clearColor]];//colorWithRed:.94 green:.96 blue:.99 alpha:1.0]];
//NSLog(#"Exiting CreateMultilinesCell");
return cell;
}
The easiest solution is to use a different cell identifier for the two types of cells.
Edit: I see you are using two different types, but you are not taking that into account in the dequeue call.

Resources