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.
Related
When I scroll UITableView the memory keeps on increasing and I get this sort of results when I run "Allocations".
I see their that VM:UITableViewLabel memory keeps on increasing and is persistent, Is their any way to I could remove this constant increase in memory
-(UITableViewCell*) tableView:(UITableView*)tableView cellForRowAtIndexPath:(NSIndexPath*)indexPath
{
NSString *text = #"";
BOOL defaultStyle = YES;
static NSString *CellIdentifier = #"Cell";
UITableViewCell *defaultCell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (defaultCell == nil) {
defaultCell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
}
else{
NSLog(#"reusing the cell");
}
defaultCell.textLabel.textAlignment = NSTextAlignmentLeft;
defaultCell.userInteractionEnabled = YES;
defaultCell.textLabel.font = [UIFont systemFontOfSize:26.0f];
UIImageView *iv = defaultCell.imageView;
int totalSections = [self numberOfSectionsInTableView:tableView];
NSInteger computedSection = indexPath.section;
if (defaultStyle)
{
defaultCell.selectionStyle = UITableViewCellSelectionStyleBlue;
defaultCell.backgroundColor = [UIColor whiteColor];
defaultCell.textLabel.textColor = [UIColor blackColor];
}
else if (computedSection == 0)
{
const int totalRows = [self tableView:self.tableView numberOfRowsInSection:indexPath.section];
if (indexPath.row == 0) {
text = #"Style1";
defaultStyle = NO;
if (self.style1 == nil)
{
defaultCell.selectionStyle = UITableViewCellSelectionStyleNone;
defaultCell.textLabel.textColor = [UIColor grayColor];
}
else
{
defaultCell.selectionStyle = UITableViewCellSelectionStyleBlue;
defaultCell.textLabel.textColor = [UIColor blackColor];
}
iv.image = [UIImage imageNamed: #"Style1.png"];
}
if(indexPath.row == totalRows - 2){
// Categories
text = #"Style2";
iv.image = [UIImage imageNamed: #"Style2.png"];
defaultStyle = NO;
if ( self.style2 == nil) {
defaultCell.selectionStyle = UITableViewCellSelectionStyleNone;
defaultCell.textLabel.textColor = [UIColor grayColor];
} else {
defaultCell.selectionStyle = UITableViewCellSelectionStyleBlue;
defaultCell.textLabel.textColor = [UIColor blackColor];
}
}
else if (indexPath.row == totalRows - 1){
// Search
text = #"Style3";
iv.image = [UIImage imageNamed: #"Style3.png"];
}
}
else if (computedSection == 1)
{
MyCustomTableViewCell *cell = [mTableView dequeueReusableCellWithIdentifier:kCustomTableCellIdentifier];
if ( cell == nil ) {
cell = [CustomTableViewCell loadFromNibNamed:#"CustomTableViewCell"];
}
cell.titleLabel = #"custom cell";
cell.openImage =[UIImage imageNamed: #"custom.png"]
return cell;
}
else if (indexPath.section == totalSections - 1)
{
if (indexPath.row == 0)
{
text = #"Account";
defaultStyle = NO;
if (self.hasAccount == nil)
{
defaultCell.selectionStyle = UITableViewCellSelectionStyleNone;
defaultCell.textLabel.textColor = [UIColor grayColor];
}
else
{
defaultCell.selectionStyle = UITableViewCellSelectionStyleBlue;
defaultCell.textLabel.textColor = [UIColor whiteColor];
}
iv.image = [UIImage imageNamed: #"Account.png"];
}
}
defaultCell.textLabel.text = text;
return defaultCell;
}
I have copied your code into a new project, and made some tests. It has never used more than 10 Mb memory on a real device, so there can be 2 cases.
Calling a method which leaks. At this point you call only one method in cellForRow because: BOOL defaultStyle = YES; This method is:
int totalSections = [self numberOfSectionsInTableView:tableView];
Could you post also this method please?
UITableViewCell subclass leaks while reusing. This isn't an option now since you are using UITableViewCell.
Small note #1: remove the autorelease if the project is in ARC.
Small note #2: Use
static NSString *CellIdentifier = #"Cell";
UITableViewCell *defaultCell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
instead of:
static NSString *CellIdentifier = #"Cell";
UITableViewCell *defaultCell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (defaultCell == nil) {
defaultCell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
}
else{
NSLog(#"reusing the cell");
}
and you don't need to check and alloc new cells manually.
Thanks all for your help, the following solution worked for me
1.
-(void) viewDidLoad
{
[super viewDidLoad];
[self.tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:kDefaultCellIdentifier];
[self.tableView registerNib:[UINib nibWithNibName:#"CustomTableViewCell" bundle:nil] forCellReuseIdentifier:kCustomTableCellIdentifier];
}
as told by #Péter Kovács
2.
Used indexPath method for both cells
static NSString *CellIdentifier = kDefaultCellIdentifier;
UITableViewCell *defaultCell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
static NSString *CellIdentifier = kCustomTableViewCellIdentifer;
UITableViewCell *defaultCell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
3.
Main problem I was using the defaultCell globally, it dequeued the defaultCell also for the CustomTableViewCell, so in the case CustomTableViewCell block was triggered it dequeued two cells. Dequeue ing respective cell based on their rows solved the problem.
I think you are not reusing the cells used in your table view
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:#"myCell"];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:#"myCell"];
} else {
// reset your cell values here
}
Use this inside your -(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
I have a UITableView with this code below:
- (UITableViewCell *)tableView:(UITableView *)tableViews cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"MyCell";
TableViewCell *cell = [tableViews dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
if (cell == nil) {
cell = [[TableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];
}
if (indexPath.row == 0){
cell.image.image = [UIImage imageNamed:#"male80.png"];
cell.text.attributedPlaceholder = [[NSAttributedString alloc] initWithString:#"Phone" attributes:#{NSForegroundColorAttributeName: [UIColor whiteColor]}];
}
if(indexPath.row == 1) {
cell.image.image = [UIImage imageNamed:#"male80.png"];
cell.text.attributedPlaceholder = [[NSAttributedString alloc] initWithString:#"Mobile Phone" attributes:#{NSForegroundColorAttributeName: [UIColor whiteColor]}];
}
if (indexPath.row == 2) {
cell.image.image = [UIImage imageNamed:#"gift41.png"];
cell.text.attributedPlaceholder = [[NSAttributedString alloc] initWithString:#"E-mail" attributes:#{NSForegroundColorAttributeName: [UIColor whiteColor]}];
}
if (indexPath.row == 3) {
cell.image.image = [UIImage imageNamed:#"gender.png"];
cell.text.attributedPlaceholder = [[NSAttributedString alloc] initWithString:#"address" attributes:#{NSForegroundColorAttributeName: [UIColor whiteColor]}];
}
if (indexPath.row == 4) {
cell.image.image = [UIImage imageNamed:#"gender.png"];
cell.text.attributedPlaceholder = [[NSAttributedString alloc] initWithString:#"country" attributes:#{NSForegroundColorAttributeName: [UIColor whiteColor]}];
}
if (indexPath.row == 5) {
cell.image.image = [UIImage imageNamed:#"gender.png"];
cell.text.attributedPlaceholder = [[NSAttributedString alloc] initWithString:#"city" attributes:#{NSForegroundColorAttributeName: [UIColor whiteColor]}];
}
if (indexPath.row == 6) {
cell.image.hidden = YES;
cell.text.hidden = YES;
UITextView *view = [[UITextView alloc] initWithFrame:CGRectMake(15, 10, cell.frame.size.width - 18, cell.frame.size.height - 15)];
view.text = #"Text Example";
view.textColor = [UIColor whiteColor];
view.backgroundColor = [UIColor clearColor];
view.tag = 13;
view.layer.borderWidth = 0.5f;
view.layer.cornerRadius = 4;
view.layer.borderColor = [[UIColor grayColor] CGColor];
[cell addSubview:view];
}
return cell;
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
if (indexPath.row != 6) {
return 65;
}else{
return 204;
}
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
return 7;
}
This code is very simple, the problem with him is:
When I run my app first time, I can see my fields organized in the manner prescribed (Like I put in cellforRowAtIndexPath)
When I scroll down (put my fields out of window) and scroll to up again I see a problem!
The problem is that my table, create a UITextview in rows like 0, 1 and 2. But why this is happening? in my code I made it clear! That only the row 6 will be created a UITextView!
How can I solve this problem?
Two issues:
1) your cell is recycled, this means when the cell gets to row 6 your UITextView is added to the cell and then when scrolling back the UITextView is still there
2) and you should add the subviews of cell in its contentView and not the cell itself.
Solution :
Use tow kind of UITableViewCell, one specific for row 6 and another one for the other rows. Register the cells and then dequeue them for the appropriate indexPath.
I think your problem is loading your NIB file into the table view.
Look into Assertion failure in dequeueReusableCellWithIdentifier:forIndexPath:
I tried to recreate your problem by creating a UITableViewCell using Interface Builder called ItemCell and it works. Make sure you have this in your viewDidLoad
[super viewDidLoad];
//Load the NIB file
UINib *nib = [UINib nibWithNibName:#"ItemCell"
bundle:nil];
//Register this NIB, which contains the cell
[self.tableView registerNib:nib
forCellReuseIdentifier:#"ItemCell"];
Then replace
static NSString *CellIdentifier = #"MyCell";
TableViewCell *cell = [tableViews dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
if (cell == nil) {
cell = [[TableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];
}
with
ItemCell *cell = [tableView dequeueReusableCellWithIdentifier:#"ItemCell"
forIndexPath:indexPath];
if (cell == nil) {
cell = [[ItemCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:#"ItemCell"];
}
You might want to also look into putting your images in a data structure to go with a MVC design pattern.
Give table Row 6 a different identifier than the others
static NSString *CellIdentifier = #"MyCell";
if (indexPath.row == 6){
CellIdentifier = #"MyCell6";
}
Even then you shouldn't be re allocing the textField everytime cellForRowAtIndexPath is called. That's the point of reusing the cell! But that's another conversation so look up how to properly recycle cells
Put this code in viewDidLoad and make you view a global variable.
UITextView *view = [[UITextView alloc] initWithFrame:CGRectMake(15, 10, cell.frame.size.width - 18, cell.frame.size.height - 15)];
view.text = #"Text Example";
view.textColor = [UIColor whiteColor];
view.backgroundColor = [UIColor clearColor];
view.tag = 13;
view.layer.borderWidth = 0.5f;
view.layer.cornerRadius = 4;
view.layer.borderColor = [[UIColor grayColor] CGColor];
And now create the tableView like this:
- (UITableViewCell *)tableView:(UITableView *)tableViews cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"MyCell";
TableViewCell *cell = [tableViews dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
if (cell == nil) {
cell = [[TableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];
}
view.hidden = YES;
if (indexPath.row == 0){
cell.image.image = [UIImage imageNamed:#"male80.png"];
cell.text.attributedPlaceholder = [[NSAttributedString alloc] initWithString:#"Phone" attributes:#{NSForegroundColorAttributeName: [UIColor whiteColor]}];
}
if(indexPath.row == 1) {
cell.image.image = [UIImage imageNamed:#"male80.png"];
cell.text.attributedPlaceholder = [[NSAttributedString alloc] initWithString:#"Mobile Phone" attributes:#{NSForegroundColorAttributeName: [UIColor whiteColor]}];
}
if (indexPath.row == 2) {
cell.image.image = [UIImage imageNamed:#"gift41.png"];
cell.text.attributedPlaceholder = [[NSAttributedString alloc] initWithString:#"E-mail" attributes:#{NSForegroundColorAttributeName: [UIColor whiteColor]}];
}
if (indexPath.row == 3) {
cell.image.image = [UIImage imageNamed:#"gender.png"];
cell.text.attributedPlaceholder = [[NSAttributedString alloc] initWithString:#"address" attributes:#{NSForegroundColorAttributeName: [UIColor whiteColor]}];
}
if (indexPath.row == 4) {
cell.image.image = [UIImage imageNamed:#"gender.png"];
cell.text.attributedPlaceholder = [[NSAttributedString alloc] initWithString:#"country" attributes:#{NSForegroundColorAttributeName: [UIColor whiteColor]}];
}
if (indexPath.row == 5) {
cell.image.image = [UIImage imageNamed:#"gender.png"];
cell.text.attributedPlaceholder = [[NSAttributedString alloc] initWithString:#"city" attributes:#{NSForegroundColorAttributeName: [UIColor whiteColor]}];
}
if (indexPath.row == 6) {
cell.image.hidden = YES;
cell.text.hidden = YES;
view.hidden = NO;
[cell addSubview:view];
}
return cell;
}
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];}
}
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
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.