2013-04-27 3 views
0

기본적으로 모든 textField에 잘못된 데이터가 표시됩니다 (모든 것이 섞여 있습니다). 나는 UITableView에서 재사용 가능한 셀의 굉장한 최적화 때문에 색인 경로가 끊임없이 변하기 때문에 이것이 문제를 해결하는 방법을 모르겠다는 것을 알고있다.iOS 6 UITableViewCell 안의 UITableViewCell 섹션이 스크롤 할 때 잘못된 데이터를받습니다.

동일한 셀에 UILabel이있는 경우 모든 레이블보기에 특정 태그를 추가하여이 문제를 해결 했으므로 tableview는 어떤 데이터가 뷰에서 반환 될지를 알고 있지만 어떤 이유로이 텍스트 필드보기에는 불가능합니다 . 어떤 도움에 감사드립니다

#define DESCRIPTION_TAG 10 
#define TEXTFIELD_TAG 11 
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{ 

static NSString *CellIdentifier = @"Description Cell"; 
// Intatiating my own TableViewCell 
CustomLocationCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath]; 
if (!cell){ 
    cell = [[CustomLocationCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier]; 
} 

cell.rowText = (UILabel *) [cell viewWithTag:DESCRIPTION_TAG]; // returning view with unique tag 
cell.rowTextField = (UITextField *)[cell viewWithTag:TEXTFIELD_TAG]; // NOT WORKING!!! Data get mix up 
cell.rowTextField.delegate = cell; // sending the delegate to custom class cell 
cell.delegate = self; // make connection with my @protocol 

switch (indexPath.section) { 
    case 0: 
     cell.rowText.text = [self.descriptions objectAtIndex:indexPath.row]; 
     cell.rowTextField.placeholder = [self.descriptionsPlaceholder objectAtIndex:indexPath.row]; 
     break; 
    case 1: 
     cell.rowText.text = [self.rooms objectAtIndex:indexPath.row]; 
     cell.rowTextField.placeholder = [self.contentPlaceholder objectAtIndex:indexPath.row]; 
     break; 

    default: 
     break; 
} 
return cell; 

}

:

여기 내 코드입니다. 여기

는 내 데이터 소스의 업데이트입니다 : 내 UILabel의 아웃렛을해야합니까 내 CustomTableViewCell에 과의 UITextField

// UITableView DataSource 

- (NSArray *)sections 
{ 
    if (!_sections){ 
     _sections = [[NSArray alloc] initWithObjects:@"First Description", @"Second Descriptions", nil]; 
    } 
    return _sections; 
} 

- (NSMutableArray *)rooms 
{ 
    if (!_rooms){ 
     _rooms = [[NSMutableArray alloc] init]; 
    } 
    return _rooms; 
} 

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView 
{ 
    return self.sections.count; 
} 

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section 
{ 
    switch (section) { 
     case 0: 
      return [self.descriptions count]; 
      break; 
     case 1: 
      return [self.rooms count]; 
      break; 
     default: 
      break; 
    } 
    return 0; 
} 
+0

표시되는 텍스트를 변경하지 마십시오. –

+0

[cell.contentView viewWithTag : TEXTFIELD_TAG]을 (를) 사용해 볼 수 있습니까? – Anupdas

+0

응답 해 주셔서 감사합니다! @ A-Live, 사용자가 텍스트를 쓰고 테이블을 스크롤 할 때이 새로운 텍스트가 섞여서 문제가되지 않는다고 생각합니다. – Pompeyo

답변

1

당신이 달성하기 위해 노력하고 정확히 모르겠지만, 여기 있어요 자리 표시 자 텍스트가있는 레이블 및 텍스트 필드와 함께 작동하는 예제입니다. cellForRowAtIndexPath에서 텍스트와 자리 표시자를 모두 nil로 설정하여 셀을 다시 사용할 때 잘못된 텍스트가 표시되지 않도록합니다. 또한 텍스트 필드를 수행 할 때 배열에 해당 인덱스에 자리 표시 자 텍스트가 있는지 확인하고, 그렇다면 자리 표시자를 재설정하고 텍스트가 아닌 경우 텍스트를 다시 설정합니다.

텍스트 필드 대리자 메서드가 제대로 작동하려면 각 텍스트 파일에 대해 (indexPath.row + 1000 * indexPath)와 동일한 값으로 설정된 고유 태그 (레이블 및 텍스트 필드에 대한 태그 하나가 아닌)가 필요합니다. 섹션).

#import "TableController.h" 
#import "CustomLocationCell.h" 

@interface TableController() 
@property (strong,nonatomic) NSArray *descriptions; 
@property (strong,nonatomic) NSMutableArray *descriptionsPlaceholder; 
@property (strong,nonatomic) NSArray *rooms; 
@property (strong,nonatomic) NSMutableArray *contentPlaceholder; 
@end 

@implementation TableController 


- (void)viewDidLoad { 
    [super viewDidLoad]; 
    self.descriptions = @[@"One",@"Two",@"Three",@"Four",@"Five",@"Six",@"Seven",@"Eight"]; 
    self.descriptionsPlaceholder = [@[@"Placeholder1",@"Placeholder2",@"Placeholder3",@"Placeholder4",@"Placeholder5",@"Placeholder6",@"Placeholder7",@"Placeholder8"] mutableCopy]; 
    self.rooms = @[@"Room1", @"Room2", @"Room3", @"Room4",@"Room5", @"Room6", @"Room7", @"Room8"]; 
    self.contentPlaceholder = [@[@"ContentPH1",@"ContentPH2",@"ContentPH3",@"ContentPH4",@"ContentPH5",@"ContentPH6",@"ContentPH7",@"ContentPH8"] mutableCopy]; 
    [self.tableView reloadData]; 
} 

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView { 
    return 2; 
} 

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { 
    return self.descriptions.count; 
} 

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

    static NSString *CellIdentifier = @"Description Cell"; 

    CustomLocationCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath]; 
    cell.rowTextField.delegate = self; 
    cell.rowTextField.tag = indexPath.row + 1000*indexPath.section; 
    cell.rowTextField.placeholder = nil; 
    cell.rowTextField.text = nil; 

    switch (indexPath.section) { 
     case 0: 
      cell.rowText.text = [self.descriptions objectAtIndex:indexPath.row]; 
      if ([self.descriptionsPlaceholder[indexPath.row] hasPrefix:@"Placeholder"]) { 
       cell.rowTextField.placeholder = [self.descriptionsPlaceholder objectAtIndex:indexPath.row]; 
      }else{ 
       cell.rowTextField.text = [self.descriptionsPlaceholder objectAtIndex:indexPath.row]; 
      } 
      break; 
     case 1: 
      cell.rowText.text = [self.rooms objectAtIndex:indexPath.row]; 
      if ([self.contentPlaceholder[indexPath.row] hasPrefix:@"ContentPH"]) { 
       cell.rowTextField.placeholder = [self.contentPlaceholder objectAtIndex:indexPath.row]; 
      }else{ 
       cell.rowTextField.text = [self.contentPlaceholder objectAtIndex:indexPath.row]; 
      } 
      break; 
     default: 
      break; 
    } 
    return cell; 
} 


-(void)textFieldDidEndEditing:(UITextField *)textField { 
    if (textField.tag >= 1000 && ![textField.text isEqualToString: @""]) { 
     [self.contentPlaceholder replaceObjectAtIndex:textField.tag - 1000 withObject:textField.text]; 
    }else if (textField.tag < 1000 && ![textField.text isEqualToString: @""]) { 
     [self.descriptionsPlaceholder replaceObjectAtIndex:textField.tag withObject:textField.text]; 
    } 
} 


-(BOOL)textFieldShouldReturn:(UITextField *)textField { 
    [textField resignFirstResponder]; 
    return YES; 
} 
+0

대단히 감사합니다. @rdelmar, 잘 작동합니다. 나쁜 설명에 죄송합니다. – Pompeyo

관련 문제