2014-01-26 3 views
1

세 개의 테이블보기 셀이있는 테이블보기가 있습니다. 셀을 구성하는 동안 각 셀에 UITextField를 추가하고 모든 텍스트 필드에 자리 표시 자 값을 설정합니다. 테이블 뷰는 최종 결과를로드 은 다음과 같습니다UITextField 자리 표시 자 텍스트 오버레이 문제

Table view after load

난 데 문제는, 내가 "화면 꺼짐"세포 중 하나를 스크롤 할 때 그들이 자리 표시 자 텍스트를 얻을 다시 때이다 여기처럼 어둡고 어두운 :

Darker placeholder text

그때 프로그래밍 방식으로 마지막 셀에 자리 표시 자 값을 변경하여에 이름을 입력하여 UITextField에 문자열 값을 변경하거나 할 때, 그 속성의 이전 값 체류하고 새로운 가치 다음과 같이 셀에 중첩됩니다 : 여기

enter image description here는 그 세포 구성에 대한 책임이있는 방법이 있습니다 : 제거 같은

당신이 볼 수 있듯이, 내가 시도
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    static NSString *CellIdentifier = @"Cell"; 
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath]; 

    // Configure the cell... 
    [self configureCell:cell forIndexPath:indexPath]; 

    return cell; 
} 

- (void)configureCell:(UITableViewCell *)cell forIndexPath:(NSIndexPath *)indexPath 
{ 
    for (UIView *subview in cell.contentView.subviews) { 
     [subview removeFromSuperview]; 
    } 

    cell.backgroundColor = [UIColor whiteColor]; 
    cell.autoresizesSubviews = YES; 
    cell.clearsContextBeforeDrawing = YES; 
    CGRect textFieldFrame = cell.bounds; 
    textFieldFrame.origin.x += 10; 
    textFieldFrame.size.width -= 10; 

    UITextField *textField = [[UITextField alloc] initWithFrame:textFieldFrame]; 
    textField.adjustsFontSizeToFitWidth = YES; 
    textField.textColor = [UIColor lightGrayColor]; 
    textField.enabled = YES; 
    textField.userInteractionEnabled = NO; 
    textField.autoresizingMask = UIViewAutoresizingFlexibleWidth; 
    textField.clearsContextBeforeDrawing = YES; 
    textField.clearsOnBeginEditing = YES; 

    if (indexPath.section == ATTAddNewTimerSectionName) { 
     textField.placeholder = @"Name"; 
     textField.userInteractionEnabled = YES; 
     textField.delegate = self; 
     textField.returnKeyType = UIReturnKeyDone; 
     textField.clearButtonMode = UITextFieldViewModeWhileEditing; 
    } else if (indexPath.section == ATTAddNewTimerSectionDate) { 
     textField.placeholder = @"Date/Time"; 
     cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator; 
    } else if (indexPath.section == ATTAddNewTimerSectionCalendar) { 
     if (self.userCalendarEvent == nil) { 
      textField.placeholder = @"See list of calendar events"; 
     } else { 
      textField.placeholder = nil; 
      textField.placeholder = self.userCalendarEvent.title; 
     } 
     cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator; 
    } 
    [cell addSubview:textField]; 
} 

여러 가지 모든 하위 뷰를 셀에 추가하거나 셀 및 UITextView에서 [UIView setClearsContextBeforeDrawing : YES]를 설정하고 자리 표시 자 값을 nil로 설정하여 성공하지 못하게합니다.

모든 포인터가 많이 감사하겠습니다!

답변

2

많은 사람들이 범하는 고전적인 오류를 만들고 있습니다. 표가 스크롤 될 때 셀을 다시 사용합니다.

작성된대로 코드는 셀을 사용할 때마다 새 텍스트 필드를 만들고 유지합니다. 따라서 셀에 여러 텍스트 필드가있는 결과를 볼 수 있습니다.

하나의 텍스트 필드 만 셀에 추가하려고합니다. 텍스트 필드가 이미 없으면 추가하기 만하면 코드를 업데이트하십시오.

코드에 논리 문제가있는 것 같습니다. 텍스트 필드를 셀에 직접 추가하지만 셀의 contentView에서 텍스트 필드를 제거하려고합니다.

변경이 :

[cell addSubview:textField]; 

에 :

[cell.contentView addSubview:textField]; 
+0

예, 그게 문제입니다. 자리 표시 자와 텍스트가 겹치는 경우 동일한 위치에> = 2 UITextField가 있기 때문에 가능합니다 – onmyway133

관련 문제