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

  • Home
  • SEARCH
  • 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 8842003
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T10:51:27+00:00 2026-06-14T10:51:27+00:00

I wanted to have textFields in a tableView. So I inserted them manually in

  • 0

I wanted to have textFields in a tableView. So I inserted them manually in the tableView in the storyboard and modified their individual properties. However, I could not see the text fields when I ran the program. I repeated the same procedure with a switch. But it still did not run. So I figured that the objects are not being shown despite the fact that they are present in the storyboard.

Q1. Any idea why this is the case?

In order to overcome this problem, I inserted these objects programmatically. For UISwitch, I added the following code in cellForRowAtindexPath right after initWithStyle:style reuseIdentifier:reuseIdentifier.

UISwitch *newSwitch = [[UISwitch alloc] initWithFrame:CGRectMake(25, 55, 77, 27)];
[newSwitch addTarget: self action: @selector(pictureSwitchAction:) forControlEvents:UIControlEventValueChanged];
[self.view addSubview:newSwitch]; 

This works fine. pictureSwitchAction method is called as expected.

However, when I try to do similar thing with UITextField, the app crashes. (FYI… field0 is a declared as property.) Here’s my code:

field0 = [[UITextField alloc] initWithFrame:CGRectMake(0, 50, 320, 44)];   // (1)
cell = (UITableViewCell *)[field0 superview];                              // (2)
[self.view addSubview:field0];                                             // (3)

(1) and (2) (commented (3)) crash the app. Error: *** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'UITableView dataSource must return a cell from tableView:cellForRowAtIndexPath:'

My understanding is that the cell has to be return first before I can allocate some value to it. But (1) and (3) (commented (2)), yield no result. I don’t know why this works for a switch and not UITextField.

Any responses to Q1 and how to insert textField programmatically?

Thanks!

Edit 1

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"Cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

    if (cell == nil)
    {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];
    }

    cell.selectionStyle = UITableViewCellSelectionStyleNone; // VERY IMPORTANT

    if(indexPath.section == 2)
    {
        if (indexPath.row == 0)
        {
            field0 = [[UITextField alloc] initWithFrame:CGRectMake(0, 50, 320, 44)];
            cell = (UITableViewCell *)[field0 superview];
           // [self.view addSubview:field0];
        }

        if (indexPath.row == 1)
        {
            field1 = [[UITextField alloc] initWithFrame:CGRectMake(0, 50, 300, 43)];
            cell = (UITableViewCell *)[field1 superview];            
            // [self.view addSubview:field1];
        }

       if (indexPath.row == 2)
       {
            field2 = [[UITextField alloc] initWithFrame:CGRectMake(0, 50, 300, 43)];
            cell = (UITableViewCell *)[field2 superview];
            // [self.view addSubview:field2];
        }
    }

    return cell;
}
  • 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-14T10:51:28+00:00Added an answer on June 14, 2026 at 10:51 am

    Here You should make the Change in Your Code like As

    Answer-1:- If you want to customize cells by simply adding additional views, you should add them to the content view .so they will be positioned appropriately as the cell transitions into and out of editing mode.

    Answer-2 Crash Reason:- Because you were not returning UITableViewCell.As Crash Log ItSelf Explain The same *** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'UITableView dataSource must return a cell from tableView:cellForRowAtIndexPath:'

    Answer 3 -For this My understanding is that the cell has to be return first before I can allocate some value to it.Here For this Line i would say you are totally wrong simply think. can you put mango inside the basket even if you don't have that basket.means you will need that . so the same concept is here whenever you are going to add something like textfield the TableCell you need to create(allocate) that first and then you can add any object over that tableCell and finally return that tableCellAs You would see inside the CellForRowAtIndexPath method.
    I think you clear now.

    For The UISwitch you were adding it over the self.view means controller's view.see your code [self.view addSubview:] showing that switch over the mainView(conrtoller’ view) rather Shwoing over the tableViewCell .and for the TextField it was not working because of this line cell = (UITableViewCell *)[field0 superview];it’s totally wrong.

    Just for your kind Information see below code.

    - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
    {
    static NSString *CellIdentifier = @"Cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    
    if (cell == nil)
    {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];
    }
    
    cell.selectionStyle = UITableViewCellSelectionStyleNone; // VERY IMPORTANT
    
    if(indexPath.section == 2)
    {
    
        if (indexPath.row == 0)
        {
            UITextField* thisTextField =[[UITextField alloc] initWithFrame:CGRectMake(originX, originY, width ,hieght)];//pass the co-ordinate.
            [thisTextField setBackgroundColor:[UIColor clearColor]];
            [thisTextField setTag:indexPath.row];//here by setting this you can access further it         
            thisTextField.delegate= self;
            [[cell contentView] addSubview:thisTextField];
            // If you want to customize cells by simply adding additional views, 
            // you should add them to the content view .
            //  so they will be positioned appropriately as the cell transitions into and out of editing mode.
        }
    
    return cell;
    

    }

    I would suggest you should start from the basic of view Hierarchy and UITableView.

    Thanks

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

Sidebar

Related Questions

Let's say I wanted to have an application that could easily switch the DB
I have wanted to try GAE since launch, but coming from ASP .NET and
I wanted to have a list of lambdas that act as sort of a
So I wanted to have something like in this image . Basically you have
Say I wanted to have one variable in a class always be in some
If I wanted to have a collection that described the (recursive) contents of a
hi I wanted to have some keys and values filtered out when compared with
Good day I wanted to have a general URL to access the Desktop of
I currently have this and it works fine but I wanted to have a
I am programming in Ruby and have wanted to learn about matrices but I

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.