Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8133525
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T09:37:57+00:00 2026-06-06T09:37:57+00:00

I’ve got 3 table cells and somehow it displays the two last contents in

  • 0

I’ve got 3 table cells and somehow it displays the two last contents in the last cell. I did something wrong somewhere in my code, but I don’t know where since I just follow a tutorial and try to do the same like in the tutorial, but instead of 2 cells I want 3 editable cells.

enter image description here

the full code:

#import "LocationAddViewController.h"
#import "Location.h"

@interface LocationAddViewController ()
- (void)prepareCell:(UITableViewCell *)cell forIndexPath:(NSIndexPath *)indexPath;
- (UIBarButtonItem *)newCancelButton;
- (UIBarButtonItem *)newSaveButton;
- (UITextField *)newTextField;
@end

@implementation LocationAddViewController

@synthesize location;
@synthesize titleField;
@synthesize authorField;
@synthesize atextField;
@synthesize delegate;


- (void)dealloc {
    [location release];
    [titleField release];
    [authorField release];
    [atextField release];
    [super dealloc];
}


- (id)initWithLocation:(Location *)aLocation andDelegate:(id)aDelegate {
    if (self = [super initWithStyle:UITableViewStyleGrouped]) {
        self.location = aLocation;
        self.delegate = aDelegate;
    }
    return self;
}

- (void)viewDidLoad {
    [super viewDidLoad];

    self.tableView.allowsSelection = NO;

    titleField = [self newTextField];
    titleField.keyboardType = UIKeyboardTypeASCIICapable;
    [titleField becomeFirstResponder];

    authorField = [self newTextField];
    authorField.keyboardType = UIKeyboardTypeASCIICapable;

    atextField = [self newTextField];
    atextField.keyboardType = UIKeyboardTypeASCIICapable;

    if (location.onelocationId) {
        titleField.text = location.title;
        authorField.text = location.author;
        atextField.text = location.text;
    } else {
        titleField.placeholder = @"Title";
        authorField.placeholder = @"Author";
        atextField.placeholder = @"Text";
    }

    UIBarButtonItem *cancelButton = [self newCancelButton];
    self.navigationItem.leftBarButtonItem = cancelButton;
    [cancelButton release];

    UIBarButtonItem *saveButton = [self newSaveButton];
    self.navigationItem.rightBarButtonItem = saveButton;
    saveButton.enabled = NO;
    [saveButton release];        
} 

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];

    if (location.onelocationId) {
        self.title = @"Edit Location";
    } else {
        self.title = @"Add Location";
    }
}


-(IBAction)cancel {
    [self.navigationController popViewControllerAnimated:YES];
}

-(IBAction)save {
    location.title = titleField.text;
    location.author = authorField.text;
    location.text = atextField.text;

    [self.delegate didChangeLocation:location];
    [self.navigationController popViewControllerAnimated:YES];
}



- (NSInteger)tableView:(UITableView *)tableView 
 numberOfRowsInSection:(NSInteger)section {
    return 3;
}

- (UITableViewCell *)tableView:(UITableView *)aTableView 
         cellForRowAtIndexPath:(NSIndexPath *)indexPath {

    UITableViewCell *cell = 
    [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault 
                            reuseIdentifier:nil] autorelease];

    [self prepareCell:cell forIndexPath:indexPath];

    return cell;
}



- (void)prepareCell:(UITableViewCell *)cell forIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.row == 0)  {
        [cell.contentView addSubview:titleField];   
    } else { 
        [cell.contentView addSubview:authorField];
        [cell.contentView addSubview:atextField];
    }
}


- (BOOL)textFieldShouldReturn:(UITextField *)textField { 
    [textField resignFirstResponder];
    if (textField == titleField) {
        [authorField becomeFirstResponder];
    }
    if (titleField == authorField) {
        [self save];
    }
    return YES;
} 

- (IBAction)textFieldChanged:(id)sender {
    BOOL enableSaveButton = 
    ([self.titleField.text length] > 0) && ([self.authorField.text length] > 0) && ([self.atextField.text length] > 0);
    [self.navigationItem.rightBarButtonItem setEnabled:enableSaveButton];
}

- (UIBarButtonItem *)newCancelButton {
    return [[UIBarButtonItem alloc] 
            initWithTitle:@"Cancel" 
            //auch im Original gelb
            style:UIBarButtonSystemItemCancel 
            target:self 
            action:@selector(cancel)];
}

- (UIBarButtonItem *)newSaveButton {
    return [[UIBarButtonItem alloc]
            initWithTitle:@"Save" 
            //auch im Original gelb
            style:UIBarButtonSystemItemSave
            target:self 
            action:@selector(save)];
}

- (UITextField *)newTextField {
    UITextField *textField = 
    [[UITextField alloc] initWithFrame:CGRectMake(10, 10, 285, 25)];
    textField.font = [UIFont systemFontOfSize:16];
    textField.delegate = self;
    textField.returnKeyType = UIReturnKeyDone;
    textField.clearButtonMode = UITextFieldViewModeWhileEditing;
    [textField addTarget:self 
                  action:@selector(textFieldChanged:) 
        forControlEvents:UIControlEventEditingChanged];
    return textField;
}  

@end

I suppose the problem is here:

    - (void)prepareCell:(UITableViewCell *)cell forIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.row == 0)  {
        [cell.contentView addSubview:titleField];   
    } else { 
        [cell.contentView addSubview:authorField];
        [cell.contentView addSubview:atextField];
    }
}

I’m not too much into the whole programming, but I guess I have to write 3 if-clauses? (Something like if (...) elsif (...) else (...)) Does anybody know it better than me?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-06T09:38:00+00:00Added an answer on June 6, 2026 at 9:38 am

    In your prepareCell:forIndexPath, you are adding both subviews into the last cell. You can use an elseif just as your described so your method looks something like this

    if (indexPath.row == 0)  {
        [cell.contentView addSubview:titleField];   
    } else if (indexPath.row == 1) { 
        [cell.contentView addSubview:authorField];
    } else {
        [cell.contentView addSubview:atextField];
    }
    

    You can add any amount of else ifs after the if.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

i got an object with contents of html markup in it, for example: string
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I've got a string that has curly quotes in it. I'd like to replace
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I have this code to decode numeric html entities to the UTF8 equivalent character.
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
I have this code: - (void)parser:(NSXMLParser *)parser foundCDATA:(NSData *)CDATABlock { NSString *someString = [[NSString
I ran into a problem. Wrote the following code snippet: teksti = teksti.Trim() teksti
I have just tried to save a simple *.rtf file with some websites and
I want to count how many characters a certain string has in PHP, but

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.