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;
}
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 liketextfieldtheTableCellyou need tocreate(allocate)that first and then you can add any object over thattableCelland finally return thattableCellAs You would see inside theCellForRowAtIndexPathmethod.I think you clear now.
For The UISwitch you were adding it over the
self.viewmeanscontroller's view.see your code[self.view addSubview:]showing that switch over the mainView(conrtoller’ view) rather Shwoing over thetableViewCell.and for the TextField it was not working because of this linecell = (UITableViewCell *)[field0 superview];it’s totally wrong.Just for your kind Information see below code.
}
I would suggest you should start from the basic of view Hierarchy and UITableView.
Thanks