2010-08-30 45 views
0

我面臨一個奇怪的問題。在表格視圖中,我有3個部分,其中第1部分只有標籤和文本字段。第二和第三部分在表格的一些單元格上包含文本字段和公開指示符。第二部分具有10個單元格,然後3個包含公開指示符和其他具有文本字段的其他部分。披露指標從iPhone滾動後從其位置轉移到iPhone

我的問題是,當我滾動視圖上下波動,說3-4次,披露指標單元格從那裏位置移動,並放置在該部分的任何位置。如果我繼續向前滾動,則披露指標將再次放置在那裏。如果我刪除這些披露指標並且只將文本字段全部運行良好。

我使用開關的情況下也試圖在的cellForRowAtIndexPath(如在下面的鏈接中提到),但沒有幫助我... :( http://www.iphonedevsdk.com/forum/iphone-sdk-development/13370-uitableview-cells-uilabel-problem.html

我做了很多谷歌,但沒有能夠找到一個解決方案這一點。

請幫我在這...

問候, VM

+0

你能不能發佈您的代碼? – rano 2010-08-30 07:48:36

+0

我認爲這是您的cellForRowAtIndexPath中的一個問題。所以請張貼該代碼。 – audience 2010-08-30 08:38:27

+0

嗨Rano和觀衆。你有沒有在我發佈的代碼中發現任何問題。 ( – 2010-09-01 04:24:48

回答

0

請找到供您參考的代碼。

第一部分包含6個單元,第二部分包含12和第三部分相同。在第2節和第3節中,單元格4,8,9包含公開指示符,其他單元格僅包含文本字段。

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { 

UITableViewCell *cell; 
NSInteger row = [indexPath row];NSLog(@"Row->%d",row); 
NSInteger section = [indexPath section];NSLog(@"section->%d",section); 
static NSString *CellIdentifier = @"Cell"; 
static NSString *BillingInfoCellIdentifier = @"BillingCell"; 
static NSString *DIBillingInfoCellIdentifier = @"DIBillingCell"; 
static NSString *BCellIdentifier = @"BCell"; 

switch (indexPath.section) { 
    case 0: 

cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 
if (cell == nil) { 
    cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:CellIdentifier] autorelease]; 
    UILabel *cellLabel = [[UILabel alloc] initWithFrame:CGRectMake(10, 10, 200, 25)]; 
    cellLabel.tag = 56; 
    cellLabel.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight; 
    [cell.contentView addSubview:cellLabel]; 
    [cellLabel release]; 

     switch (indexPath.row) { 
      case 0: 
      case 1: 
      case 2: 
      case 3: 
      case 4: 
      case 5: 
       textField = [[UITextField alloc] initWithFrame: CGRectMake(105, 12, 200, 25)]; 
       textField.clearsOnBeginEditing = NO; 
       textField.placeholder = [[tempArray objectAtIndex:section] objectAtIndex:row]; 
       [textField setDelegate:self]; 
       [textField addTarget:self action:@selector(textFieldDone:) forControlEvents:UIControlEventEditingDidEndOnExit]; 
       textField.tag = 57; 
       [cell.contentView addSubview:textField];NSLog(@"in adding text fields"); 
       break; 
     } 
     break; 
    case 1: 
     cell = [tableView dequeueReusableCellWithIdentifier:BillingInfoCellIdentifier]; 
     if (cell == nil) { 

      switch (indexPath.row) { 
       case 0: 
       case 1: 
       case 2: 
       case 3: 
       case 5: 
       case 6: 
       case 7: 
       case 10: 
       case 11: 
        cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:BillingInfoCellIdentifier] autorelease]; 
        UILabel *cellLabel = [[UILabel alloc] initWithFrame:CGRectMake(10, 10, 200, 25)]; 
        cellLabel.tag = 56; 
        cellLabel.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight; 
        [cell.contentView addSubview:cellLabel]; 
        [cellLabel release]; 
        cell.accessoryType = UITableViewCellAccessoryNone; 
        textField = [[UITextField alloc] initWithFrame: CGRectMake(105, 12, 200, 25)]; 
        textField.clearsOnBeginEditing = NO; 
        textField.placeholder = [[tempArray objectAtIndex:section] objectAtIndex:row]; 
        [textField setDelegate:self]; 
        [textField addTarget:self action:@selector(textFieldDone:) forControlEvents:UIControlEventEditingDidEndOnExit]; 
        textField.tag = 57; 
        [cell.contentView addSubview:textField];NSLog(@"in adding text fields"); 
        break; 
       case 4: 
       case 8: 
       case 9: 
        cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:BillingInfoCellIdentifier] autorelease]; 
        cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator; 
        cellLabel = [[UILabel alloc] initWithFrame:CGRectMake(10, 10, 200, 25)]; 
        cellLabel.tag = 56; 
        cellLabel.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight; 
        [cell.contentView addSubview:cellLabel]; 
        [cellLabel release]; 
        break; 
      } 
      break; 

     } 
     break; 
    case 2: 
     cell = [tableView dequeueReusableCellWithIdentifier:BCellIdentifier]; 
     if (cell == nil) { 
      cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:BCellIdentifier] autorelease]; 
      UILabel *cellLabel = [[UILabel alloc] initWithFrame:CGRectMake(10, 10, 200, 25)]; 
      cellLabel.tag = 56; 
      cellLabel.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight; 
      [cell.contentView addSubview:cellLabel]; 
      [cellLabel release]; 
     } 
    break; 
} 

} 
UILabel *cellLabel = (UILabel *)[cell.contentView viewWithTag:56]; 
UITextField *TF = (UITextField *)[cell.contentView viewWithTag:57]; 

switch (indexPath.section) { 
    case 0: 
     switch (indexPath.row) { 
      case 0: 
       cellLabel.text = @"First Name:"; 
       TF. placeholder = [[tempArray objectAtIndex:section] objectAtIndex:row]; 
       break; 
      case 1: 
       cellLabel.text = @"Last Name";//TF.text = @"Last Name:"; 
       TF.placeholder = [[tempArray objectAtIndex:section] objectAtIndex:row]; 
       NSLog(@"TF.placeholder 1->%@",[[tempArray objectAtIndex:section] objectAtIndex:row]); 
       break; 
      case 2: 
       cellLabel.text = @"eMail";//TF.text = @"email Name:"; 
       TF.placeholder = [[tempArray objectAtIndex:section] objectAtIndex:row]; 
       NSLog(@"TF.placeholder 2->%@",[[tempArray objectAtIndex:section] objectAtIndex:row]); 
       break; 
      case 3: 
       cellLabel.text = @"Password";//TF.text = @"pass Name:"; 
       TF.placeholder = [[tempArray objectAtIndex:section] objectAtIndex:row]; 
       NSLog(@"TF.placeholder 3->%@",[[tempArray objectAtIndex:section] objectAtIndex:row]); 
       break; 
      case 4: 
       cellLabel.text = @"Confirm";//TF.text = @"conform Name:"; 
       TF.placeholder = [[tempArray objectAtIndex:section] objectAtIndex:row]; 
       NSLog(@"TF.placeholder 4->%@",[[tempArray objectAtIndex:section] objectAtIndex:row]); 
       break; 
      case 5: 
       cellLabel.text = @"Phone";//TF.text = @"phone Name:"; 
       TF.placeholder = [[tempArray objectAtIndex:section] objectAtIndex:row]; 
       NSLog(@"TF.placeholder 5->%@",[[tempArray objectAtIndex:section] objectAtIndex:row]); 
       break; 
     } 
     break; 
    case 1: 
     switch(indexPath.row) { 
      case 0: 
       cellLabel.text = @"First Name"; 
       TF.placeholder = [[tempArray objectAtIndex:section] objectAtIndex:row]; 
       break; 
      case 1: 
       cellLabel.text = @"Last Name"; 
       TF.placeholder = [[tempArray objectAtIndex:section] objectAtIndex:row]; 
       break; 
      case 2: 
       cellLabel.text = @"Phone"; 
       TF.placeholder = [[tempArray objectAtIndex:section] objectAtIndex:row]; 
       break; 
      case 3: 
       cellLabel.text = @"Company"; 
       TF.placeholder = [[tempArray objectAtIndex:section] objectAtIndex:row]; 
       break; 
      case 5: 
       cellLabel.text = @"Address 1"; 
       TF.placeholder = [[tempArray objectAtIndex:section] objectAtIndex:row]; 
       break; 
      case 6: 
       cellLabel.text = @"Address 2"; 
       TF.placeholder = [[tempArray objectAtIndex:section] objectAtIndex:row]; 
       break; 
      case 7: 
       cellLabel.text = @"City"; 
       TF.placeholder = [[tempArray objectAtIndex:section] objectAtIndex:row]; 
       break; 
      case 10: 
       cellLabel.text = @"Suit"; 
       TF.placeholder = [[tempArray objectAtIndex:section] objectAtIndex:row]; 
       break; 
      case 11: 
       cellLabel.text = @"Zip"; 
       TF.placeholder = [[tempArray objectAtIndex:section] objectAtIndex:row]; 
       break; 
      default: 
       cellLabel.text = @"2 else "; 
       //TF.placeholder = [[tempArray objectAtIndex:section] objectAtIndex:row]; 
       break; 
     } 
     break; 
    case 2: 
     switch (indexPath.row) { 
      case 0: 
       cellLabel.text = @"Label 2"; 
       break; 
      case 1: 
       cellLabel.text = @"Label 2"; 
       break; 
      default: 
       cellLabel.text = @"3. else Label "; 
       break; 
     } 
     break; 
    default: 
     break; 
} 
return cell; 

}

+0

任何人在上面的代碼中發現任何錯誤?我無法解決這個問題.. :( – 2010-08-31 04:33:17

0

嘗試做所有的初始化你分配的小區後,沒有空調,如果它是零了。

含義:

if (cell == nil) { 
    cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:CellIdentifier] autorelease]; 
} 

現在小區的初始化的其餘部分(它的子視圖和更多)