2012-11-16 50 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