Note that there are some explanatory texts on larger screens.

plurals
  1. POInserting UITextField into a TableView
    primarykey
    data
    text
    <p>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. </p> <p>Q1. Any idea why this is the case?</p> <p>In order to overcome this problem, I inserted these objects programmatically. For UISwitch, I added the following code in <code>cellForRowAtindexPath</code> right after <code>initWithStyle:style reuseIdentifier:reuseIdentifier</code>. </p> <pre><code>UISwitch *newSwitch = [[UISwitch alloc] initWithFrame:CGRectMake(25, 55, 77, 27)]; [newSwitch addTarget: self action: @selector(pictureSwitchAction:) forControlEvents:UIControlEventValueChanged]; [self.view addSubview:newSwitch]; </code></pre> <p>This works fine. <code>pictureSwitchAction</code> method is called as expected.</p> <p>However, when I try to do similar thing with UITextField, the app crashes. (FYI... <code>field0</code> is a declared as <code>property</code>.) Here's my code:</p> <pre><code>field0 = [[UITextField alloc] initWithFrame:CGRectMake(0, 50, 320, 44)]; // (1) cell = (UITableViewCell *)[field0 superview]; // (2) [self.view addSubview:field0]; // (3) </code></pre> <p>(1) and (2) (commented (3)) crash the app. Error: <code>*** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'UITableView dataSource must return a cell from tableView:cellForRowAtIndexPath:'</code></p> <p>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.</p> <p>Any responses to Q1 and how to insert textField programmatically?</p> <p>Thanks!</p> <p><strong>Edit 1</strong></p> <pre><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) { 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; } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload