我想在 tableView 中添加文本字段。因此,我将它们手动插入到 Storyboard 中的 tableView 中,并修改了它们各自的属性。但是,当我运行该程序时,我看不到文本字段。我用开关重复了相同的过程。但它仍然没有运行。所以我认为尽管这些对象存在于 Storyboard中,但它们并未被显示。
Q1。知道为什么会出现这种情况吗?
为了克服这个问题,我以编程方式插入了这些对象。对于 UISwitch,我在 initWithStyle:style reuseIdentifier:reuseIdentifier
之后的 cellForRowAtindexPath
中添加了以下代码。
UISwitch *newSwitch = [[UISwitch alloc] initWithFrame:CGRectMake(25, 55, 77, 27)];
[newSwitch addTarget: self action: @selector(pictureSwitchAction:) forControlEvents:UIControlEventValueChanged];
[self.view addSubview:newSwitch];
这很好用。 pictureSwitchAction
方法按预期调用。
但是,当我尝试对 UITextField 执行类似操作时,应用程序崩溃了。 (仅供引用... field0
被声明为 property
。)这是我的代码:
field0 = [[UITextField alloc] initWithFrame:CGRectMake(0, 50, 320, 44)]; // (1)
cell = (UITableViewCell *)[field0 superview]; // (2)
[self.view addSubview:field0]; // (3)
(1)和(2)(注释(3))使应用程序崩溃。错误:*** 由于未捕获的异常“NSInternalInconsistencyException”而终止应用程序,原因:“UITableView 数据源必须从 tableView:cellForRowAtIndexPath: 返回一个单元格:”
我的理解是,必须先返回单元格,然后才能为其分配一些值。但是(1)和(3)(评论(2)),没有产生任何结果。我不知道为什么这适用于开关而不适用于 UITextField。
对问题 1 有何回应以及如何以编程方式插入文本字段?
谢谢!
编辑 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;
}
最佳答案
在这里,您应该像 As 一样更改代码
答案 1:- 如果您想通过简单地添加其他 View 来自定义单元格,则应该将它们添加到内容 View 中。这样,当单元格进入和退出编辑模式时,它们就会处于适当的位置。
Answer-2 崩溃原因:- 因为您没有返回 UITableViewCell.As 崩溃日志 ItSelf 解释相同 *** 由于未捕获的异常“NSInternalInconsistencyException”而终止应用程序,原因:“UITableView dataSource 必须从 tableView 返回一个单元格:cellForRowAtIndexPath:'
答案3 - 对于这个我的理解是,在我可以为其分配一些值之前,单元格必须先返回
。对于这一行,我想说你完全错了,简单地想想。 即使您没有那个篮子,您也可以将芒果放入篮子中吗?意味着您将需要它
。因此,每当您要添加诸如 textfield
之类的 TableCell
之类的内容时,这里都存在相同的概念,您需要先创建(分配)
,然后再添加可以在该 tableCell
上添加任何对象,并最终返回该 tableCell
,正如您在 CellForRowAtIndexPath
方法中看到的那样。
我想你现在清楚了。
对于 UISwitch,您将其添加到 self.view
上意味着 Controller 的 View
。请参阅您的代码[self.view addSubview:]
显示切换到 mainView(conrtoller' view) 而不是 Shwoing 切换到 tableViewCell
。对于 TextField,由于这一行 cell = (UITableViewCell *)[field0 superview]; ,它不起作用
这是完全错误的。
仅供您引用,请参阅下面的代码。
- (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;
我建议你应该从 View 层次结构和 UITableView 的基础开始。
谢谢
关于iphone - 将 UITextField 插入 TableView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13423047/