2013-09-01 1 views
2

셀 및 텍스트 필드가있는 UITableView를 구현하려고하지만 셀이 겹치기 때문에 텍스트 필드가 한 섹션에서 다른 섹션으로 건너 뜁니다.테이블보기에서 겹치는 셀

누군가 내 코드에서 내가 수행 한 실수를 확인할 수 있습니까?

listForItemsinfo =[[NSArray alloc]initWithObjects:@"Name",@"Price", nil]; 
listForBrandYear =[[NSArray alloc] initWithObjects:@"Year",@"Alfa",@"aston",@"audi",@"bentley",@"BMW",@"Cadillac",@"Chevrolet",@"Ferrari",@"Ford",@"Jaguar",@"Jeep",@"Kia",@"Landrover",@"Mazda",@"Mercedes",@"Mitsubishi",@"Nissan",@"Porshe",@"Subaru", nil]; 



listForkeywords =[[NSArray alloc] initWithObjects:@"KeyWord1",@"Keyword2", nil]; 
listForcarType =[[NSArray alloc] initWithObjects:@"Sedan",@"Convertible",@"Coupe",@"Heatchback", nil]; 
listForItemsCategory =[[NSArray alloc] initWithObjects:@"cat1",@"cat2",@"cat3",@"cat4",@"cat5",@"cat6", nil]; 

sections = [[NSArray alloc]initWithObjects:@"Item's info",@"Brand, Year",@"Keywords",@"Car Type",@"Category", nil]; 
listOfsections =[[NSArray alloc]initWithObjects:listForItemsinfo,listForBrandYear,listForkeywords,listForcarType,listForItemsCategory, nil]; 



- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
static NSString *CellIdentifier = @"Cell"; 
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 
if (cell == nil) { 
    cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier]; 


    if (indexPath.section == 0) { 

      // Add a UITextField 
     UITextField *textField = [[UITextField alloc] init]; 
     // Set a unique tag on each text field 
     textField.tag = 1+ indexPath.row; 
     // Add general UITextAttributes if necessary 
     textField.enablesReturnKeyAutomatically = YES; 
     textField.autocorrectionType = UITextAutocorrectionTypeNo; 
     textField.autocapitalizationType = UITextAutocapitalizationTypeNone; 
     [cell.contentView addSubview:textField]; 


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

     UITextField *textField = [[UITextField alloc] init]; 
     // Set a unique tag on each text field 
     textField.tag = 2+ indexPath.row; 
     // Add general UITextAttributes if necessary 
     textField.enablesReturnKeyAutomatically = YES; 
     textField.autocorrectionType = UITextAutocorrectionTypeNo; 
     textField.autocapitalizationType = UITextAutocapitalizationTypeNone; 
     [cell.contentView addSubview:textField]; 
     } 
    }else if (indexPath.section == 2){ 
     if (indexPath.row==0 | indexPath.row ==1) { 

      UITextField *textField = [[UITextField alloc] init]; 
      // Set a unique tag on each text field 
      textField.tag = 3+ indexPath.row; 
      // Add general UITextAttributes if necessary 
      textField.enablesReturnKeyAutomatically = YES; 
      textField.autocorrectionType = UITextAutocorrectionTypeNo; 
      textField.autocapitalizationType = UITextAutocapitalizationTypeNone; 
      [cell.contentView addSubview:textField]; 
     }else{ 

     } 
    } 
} 
//[email protected]"wqewq"; 
//cell.textLabel.text = [[listOfsections objectAtIndex:indexPath.section] objectAtIndex:indexPath.row]; 

[self configureCell:cell atIndexPath:indexPath]; 



return cell; 
} 

- (void)configureCell:(UITableViewCell *)theCell atIndexPath:(NSIndexPath *)indexPath { 

if (indexPath.section == 0) { 

    // Get the text field using the tag 
    UITextField *textField = (UITextField *)[theCell.contentView viewWithTag:1+indexPath.row]; 
    // Position the text field within the cell bounds 
    CGRect cellBounds = theCell.bounds; 
    CGFloat textFieldBorder = 10.f; 
    // Don't align the field exactly in the vertical middle, as the text 
    // is not actually in the middle of the field. 
    CGRect aRect = CGRectMake(textFieldBorder, 9.f, CGRectGetWidth(cellBounds)-(2*textFieldBorder), 31.f); 

    textField.frame = aRect; 

    theCell.textLabel.text = NULL; 


    // Configure UITextAttributes for each field 
    if(indexPath.section == 0 & indexPath.row == 0) { 
     textField.placeholder = @"Name"; 
     textField.returnKeyType = UIReturnKeyNext; 
     textField.autocapitalizationType = UITextAutocapitalizationTypeWords; 
    } else if(indexPath.section == 0 & indexPath.row == 1) { 
     textField.placeholder = @"Price"; 
     textField.returnKeyType = UIReturnKeyNext; 
     textField.keyboardType = UIKeyboardTypeNumberPad; 
    }else if (indexPath.section ==0 & indexPath.row >1){ 
     [textField removeFromSuperview]; 
    } 
}else if (indexPath.section == 1) { 

    // Get the text field using the tag 
    UITextField *textField = (UITextField *)[theCell.contentView viewWithTag:2+indexPath.row]; 
    // Position the text field within the cell bounds 
    CGRect cellBounds = theCell.bounds; 
    CGFloat textFieldBorder = 10.f; 
    // Don't align the field exactly in the vertical middle, as the text 
    // is not actually in the middle of the field. 
    CGRect aRect = CGRectMake(textFieldBorder, 9.f, CGRectGetWidth(cellBounds)-(2*textFieldBorder), 31.f); 

    textField.frame = aRect; 

    // Configure UITextAttributes for each field 
    if(indexPath.section == 1 & indexPath.row == 0) { 
     textField.placeholder = @"Year"; 
     textField.returnKeyType =UIReturnKeyDone; 
     textField.autocapitalizationType = UIKeyboardTypeNumberPad; 
     theCell.textLabel.text = NULL; 
    }else if(indexPath.section == 1 & indexPath.row != 0){ 
     [textField removeFromSuperview]; 
     theCell.textLabel.text = [[listOfsections objectAtIndex:indexPath.section] objectAtIndex:indexPath.row]; 

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

    // Get the text field using the tag 
    UITextField *textField = (UITextField *)[theCell.contentView viewWithTag:3+indexPath.row]; 
    // Position the text field within the cell bounds 
    CGRect cellBounds = theCell.bounds; 
    CGFloat textFieldBorder = 10.f; 
    // Don't align the field exactly in the vertical middle, as the text 
    // is not actually in the middle of the field. 
    CGRect aRect = CGRectMake(textFieldBorder, 9.f, CGRectGetWidth(cellBounds)-(2*textFieldBorder), 31.f); 

    textField.frame = aRect; 

    // Configure UITextAttributes for each field 
    if(indexPath.section == 2 & indexPath.row == 0) { 
     textField.placeholder = @"Keyword1"; 
     textField.returnKeyType =UIReturnKeyDone; 
     textField.autocapitalizationType = UIKeyboardTypeNumberPad; 
    }else if(indexPath.section == 2 & indexPath.row == 1) { 
     textField.placeholder = @"Keyword2"; 
     textField.returnKeyType =UIReturnKeyDone; 
     textField.autocapitalizationType = UIKeyboardTypeNumberPad; 
    }else{} 
} 



} 

고정! 사람은 세포가 중복하지 않습니다 같은 문제 (작업 코드)

static NSString *CellIdentifier = @"Cell"; 
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 
    if (cell == nil) { 
     cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier]; 
    } 
    // Add a UITextField 
    UITextField *textField = [[UITextField alloc] init]; 
    // Set a unique tag on each text field 
    textField.tag = 1 + indexPath.row; 
    textField.delegate=self; 
    // Add general UITextAttributes if necessary 
    textField.enablesReturnKeyAutomatically = YES; 
    textField.autocorrectionType = UITextAutocorrectionTypeNo; 
    textField.autocapitalizationType = UITextAutocapitalizationTypeNone; 
    // Position the text field within the cell bounds 
    CGRect cellBounds = cell.bounds; 
    CGFloat textFieldBorder = 10.f; 
    // Don't align the field exactly in the vertical middle, as the text 
    // is not actually in the middle of the field. 
    CGRect aRect = CGRectMake(textFieldBorder, 9.f, CGRectGetWidth(cellBounds)-(2*textFieldBorder), 31.f); 

    textField.frame = aRect; 

    textField.placeholder = @"Search for an item"; 
    textField.returnKeyType = UIReturnKeyDefault; 
    textField.autocapitalizationType = UITextAutocapitalizationTypeWords; 

     if ((indexPath.section == 0) & (indexPath.row==0)) { 

      [cell.contentView addSubview:textField]; 
      cell.textLabel.text=NULL; 
     }else{ 
      [textField removeFromSuperview]; 
      cell.textLabel.text = [[sections objectAtIndex:indexPath.section] objectAtIndex:indexPath.row]; 
     } 
+0

아래 링크를 확인하시기 바랍니다, 희망, 그것은 :-) 당신을 위해 일하겠습니다 http://stackoverflow.com/a/23734829/1999368 –

답변

0

테이블이 있지만 세포 경우 재사용 얻을 않습니다. 문제는 초기화 할 때 셀을 구성하는 것뿐입니다. 사실 새 셀을 만들거나 이전 셀을 만들 때 -dequeueReusableCellWithIdentifier:에서 해당 구성을 수행해야합니다.

아마도 테이블보기에서 모든 셀을 저장한다고 생각했을 것입니까? 재사용이 가능한 세포는 한 번에 표시 할 수있는만큼 많은 세포가 필요하다는 것을 의미합니다. 보기를 스크롤하면 위쪽 또는 아래쪽으로 스크롤되는 셀이 새 내용의 새 셀로 재활용됩니다. 새로운 콘텐츠를 설정하지 않으므로 이전 콘텐츠가 대신 표시됩니다. 코드의 나머지 부분이 항상 실행되도록

, 당신의 if의 첫 번째 줄 이후에 대괄호를 추가

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 
if (cell == nil) { 
    cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier]; 
} 

if (indexPath.section == 0) { 
+0

감사합니다. – George

관련 문제