2012-11-16 53 views
0

我想在tableView中使用textField。所以我手动将它们插入到故事板的tableView中并修改它们各自的属性。但是,当我运行程序时,我看不到文本字段。我用一个开关重复了同样的过程。但它仍然没有运行。所以我认为,尽管它们出现在故事​​板中,但它们并没有显示出来。将UITextField插入到TableView中

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))崩溃的应用程序。错误:*** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'UITableView dataSource must return a cell from tableView:cellForRowAtIndexPath:'

我的理解是,单元必须先返回,然后才能为其分配一些值。但是(1)和(3)(评论(2)),不会产生任何结果。我不知道为什么这可以用于交换机而不是UITextField。

对Q1的任何反应以及如何以编程方式插入textField?

谢谢!

编辑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; 
} 
+0

我怀疑你的问题是你如何把它放在一起。发布你的整个'tableView:cellForRowAtIndexPath:'方法,因为这应该阐明为什么你会得到异常。 – kamprath

+0

当然..请检查我的编辑在2分钟.. –

回答

1

这里你应该像你的代码更改为

答1: - 如果您想通过简单地增加额外的观点,你应该定制细胞将它们添加到内容视图中,所以当单元格进入和退出编辑模式时它们将被适当定位。

回答-2崩溃原因: - 因为你没有返回UITableViewCell.As崩溃日志本身说明相同*** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'UITableView dataSource must return a cell from tableView:cellForRowAtIndexPath:'

回答3 - 对于这My understanding is that the cell has to be return first before I can allocate some value to it。这里对于此行中我会说你是完全错只是想。 can you put mango inside the basket even if you don't have that basket.means you will need that。所以相同的概念是在这里,无论何时您要添加textfieldTableCell您需要create(allocate)第一,然后您可以添加任何对象通过该tableCell并最后返回tableCell正如你会看到在CellForRowAtIndexPath方法内。 我想你现在明白了。

为UISwitch你将它添加在self.view意味着controller's view。看到你的代码[self.view addSubview:]显示,在tableViewCell。而且因为它是不是因为这条线的工作文本字段切换的MAINVIEW(conrtoller”视图),而Shwoing 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; 

}

我建议你应该从视图层次和UITableView的基本启动。

谢谢

+0

非常感谢@ ios - Deveoper!代码起作用。 –

+0

我明白你刚刚加了'thisTextField.delegate = self;'。它为什么如此重要? –

+0

@Rut这会将一些方法配置为使用UItextField的'delegate'方法所需的'textfield'.THAT。您应该看到'UITextFieldDelegate'协议。 – Kamarshad