2013-05-15 67 views
2

這是我的代碼,第0部分顯示標題,但不是文本框或佔位符,這是怎麼回事?第1部分很好!Uitableview單元格不是全部工作?

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
UITableViewCell *cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:nil]; 

// Make cell unselectable and set font. 
cell.selectionStyle = UITableViewCellSelectionStyleNone; 
cell.textLabel.font = [UIFont fontWithName:@"ArialMT" size:12]; 

if (indexPath.section == 0) { 

    UITextField* tf = nil; 
    switch (indexPath.row) { 
     case 0: { 
      cell.textLabel.text = @"Name" ; 
      tf = nameFieldTextField = [self makeTextField:self.name placeholder:@"John Appleseed"]; 
      [cell addSubview:nameFieldTextField]; 
      break ; 
     } 
     case 1: { 
      cell.textLabel.text = @"Address" ; 
      tf = addressFieldTextField = [self makeTextField:self.address placeholder:@"Street Address"]; 
      [cell addSubview:addressFieldTextField]; 
      break ; 
     } 
     case 2: { 
      cell.textLabel.text = @"Email" ; 
      tf = emailFieldTextField = [self makeTextField:self.email placeholder:@"[email protected]"]; 
      [cell addSubview:emailFieldTextField]; 
      break ; 
     } 
     case 3: { 
      cell.textLabel.text = @"Phone" ; 
      tf = phoneFieldTextField = [self makeTextField:self.phone placeholder:@"XXX-XXX-XXXX"]; 
      [cell addSubview:phoneFieldTextField]; 
      break ; 
     } 

    } 

} else if (indexPath.section == 1) { 

    UITextField* tf = nil; 
    switch (indexPath.row) { 
     case 0: { 
      cell.textLabel.text = @"Company" ; 
      tf = workNameTextField = [self makeTextField:self.workName placeholder:@"Company Name"]; 
      [cell addSubview:workNameTextField]; 
      break ; 
     } 
     case 1: { 
      cell.textLabel.text = @"Address" ; 
      tf = workAddressTextField = [self makeTextField:self.workAddress placeholder:@"Work Address"]; 
      [cell addSubview:workAddressTextField]; 
      break ; 
     } 
     case 2: { 
      cell.textLabel.text = @"Phone" ; 
      tf = workPhoneTextField = [self makeTextField:self.workPhone placeholder:@"xxx-xxx-xxxx"]; 
      [cell addSubview:workPhoneTextField]; 
      break ; 
     } 
     case 3: { 
      cell.textLabel.text = @"Title" ; 
      tf = workTitleTextField = [self makeTextField:self.workTitle placeholder:@"Position"]; 
      [cell addSubview:workTitleTextField]; 
      break ; 
     } 
     case 4: { 
      cell.textLabel.text = @"Manager" ; 
      tf = workManagerTextField = [self makeTextField:self.workManager placeholder:@"Mr. Boss"]; 
      [cell addSubview:workManagerTextField]; 
      break ; 
     } 
     case 5: { 
      cell.textLabel.text = @"Manager Phone" ; 
      tf = workManagerPhoneTextField = [self makeTextField:self.workManagerphone placeholder:@"XXX-XXX-XXXX"]; 
      [cell addSubview:workManagerPhoneTextField]; 
      break ; 
     } 
     case 6: { 
      cell.textLabel.text = @"Annual Salary" ; 
      tf = workManagerPhoneTextField = [self makeTextField:self.workManagerphone placeholder:@"$50,000"]; 
      [cell addSubview:workManagerPhoneTextField]; 
      break ; 
     } 

    } 
    // Textfield dimensions 
    tf.frame = CGRectMake(120, 12, 170, 30); 

    // Workaround to dismiss keyboard when Done/Return is tapped 
    [tf addTarget:self action:@selector(textFieldFinished:) forControlEvents:UIControlEventEditingDidEndOnExit]; 

} 

return cell; 
} 
+2

是否正確,即'tf.frame = CGRectMake(120,12,170,30);'部分僅適用於第1部分? – geo

+0

我試過在第0部分加入它,並沒有什麼區別。我使用動態單元格的故事板。 – Jason

+0

檢查你的(nameFieldTextField,addressFieldTextField,emailFieldTextField,phoneFieldTextField)的隱藏屬性是YES?並且不要忘記像第1部分中那樣添加文本框的框架大小。 –

回答

3

您設定框架屬性僅對部分1,而不是部分0只設置

// Textfield dimensions 
    tf.frame = CGRectMake(120, 12, 170, 30); 

    // Workaround to dismiss keyboard when Done/Return is tapped 
    [tf addTarget:self action:@selector(textFieldFinished:) forControlEvents:UIControlEventEditingDidEndOnExit]; 

部分的else if(indexPath.section == 1)括號外(和之前的第一,如果還宣佈TF變量)或複製/粘貼在第一個開關之後:P

0

你應該這樣

if (indexPath.section == 0) { 

    UITextField* tf = nil; 
    switch (indexPath.row) { 
     case 0: { 
      cell.textLabel.text = @"Name" ; 
      tf = nameFieldTextField = [self makeTextField:self.name placeholder:@"John Appleseed"]; 
      [cell.contentview addSubview:nameFieldTextField]; 
      break ; 
     } 
     case 1: { 
      cell.textLabel.text = @"Address" ; 
      tf = addressFieldTextField = [self makeTextField:self.address placeholder:@"Street Address"]; 
      [cell.contentview addSubview:addressFieldTextField]; 
      break ; 
     } 
     case 2: { 
      cell.textLabel.text = @"Email" ; 
      tf = emailFieldTextField = [self makeTextField:self.email placeholder:@"[email protected]"]; 
      [cell.contentview addSubview:emailFieldTextField]; 
      break ; 
     } 
     case 3: { 
      cell.textLabel.text = @"Phone" ; 
      tf = phoneFieldTextField = [self makeTextField:self.phone placeholder:@"XXX-XXX-XXXX"]; 
      [cell.contentview addSubview:phoneFieldTextField]; 
      break ; 
     } 

    } 

} else if (indexPath.section == 1) { 

    UITextField* tf = nil; 
    switch (indexPath.row) { 
     case 0: { 
      cell.textLabel.text = @"Company" ; 
      tf = workNameTextField = [self makeTextField:self.workName placeholder:@"Company Name"]; 
      [cell.contentview addSubview:workNameTextField]; 
      break ; 
     } 
     case 1: { 
      cell.textLabel.text = @"Address" ; 
      tf = workAddressTextField = [self makeTextField:self.workAddress placeholder:@"Work Address"]; 
      [cell.contentview addSubview:workAddressTextField]; 
      break ; 
     } 
     case 2: { 
      cell.textLabel.text = @"Phone" ; 
      tf = workPhoneTextField = [self makeTextField:self.workPhone placeholder:@"xxx-xxx-xxxx"]; 
      [cell.contentview addSubview:workPhoneTextField]; 
      break ; 
     } 
     case 3: { 
      cell.textLabel.text = @"Title" ; 
      tf = workTitleTextField = [self makeTextField:self.workTitle placeholder:@"Position"]; 
      [cell.contentview addSubview:workTitleTextField]; 
      break ; 
     } 
     case 4: { 
      cell.textLabel.text = @"Manager" ; 
      tf = workManagerTextField = [self makeTextField:self.workManager placeholder:@"Mr. Boss"]; 
      [cell.contentview addSubview:workManagerTextField]; 
      break ; 
     } 
     case 5: { 
      cell.textLabel.text = @"Manager Phone" ; 
      tf = workManagerPhoneTextField = [self makeTextField:self.workManagerphone placeholder:@"XXX-XXX-XXXX"]; 
      [cell.contentview addSubview:workManagerPhoneTextField]; 
      break ; 
     } 
     case 6: { 
      cell.textLabel.text = @"Annual Salary" ; 
      tf = workManagerPhoneTextField = [self makeTextField:self.workManagerphone placeholder:@"$50,000"]; 
      [cell.contentview addSubview:workManagerPhoneTextField]; 
      break ; 
     } 

    } 
    // Textfield dimensions 
    tf.frame = CGRectMake(120, 12, 170, 30); 

    // Workaround to dismiss keyboard when Done/Return is tapped 
    [tf addTarget:self action:@selector(textFieldFinished:) forControlEvents:UIControlEventEditingDidEndOnExit]; 

} 
+0

同樣的問題。部分0不添加帶佔位符的uitextfields。 – Jason

0

請改變你的分配代碼的內容視圖中添加文本框斷細胞 並更換

UITableViewCell *cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:nil]; 

有了這個代碼:

static NSString *CellIdentifier = @"Cell"; 
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 
if (cell == nil) 
{ 
    cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease]; 
} 
+0

沒有區別。同樣的問題。 – Jason

+0

我不想投票給你,但是當設置這樣的單元格時,如果你添加了你建議的代碼,它會在滾動時混淆單元格佔位符。 – Jason

0

allocinit您的tf並將其添加到細胞的子視圖。我認爲你的tf是零,這就是爲什麼它沒有添加到細胞的子視圖。

希望這會有所幫助。

+0

我沒有遵循,第1部分是罰款和編碼相同的方式..? – Jason

+0

請提供uitableview的整個代碼來檢查 – iEinstein

相關問題