2013-04-27 64 views
0

所以基本上,我通過滾動我的TableView(每件事都混在一起)在每個textField上得到錯誤的數據。我知道,這是因爲UITableView中可重用單元的優化優化,所以indexpath不斷變化,但我仍然不知道如何解決這個問題。iOS 6 UITextField裏面的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; 

}

任何幫助表示讚賞。

這裏是我的數據源的更新: 在我CustomTableViewCell我有網點爲我的UILabel,同時也是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

設置佔位符不改變顯示的文本。 – 2013-04-27 15:22:58

+0

你可以試一下[cell.contentView viewWithTag:TEXTFIELD_TAG]; – Anupdas 2013-04-27 15:23:44

+0

感謝您的迴應! @ A-Live,我認爲這不是問題,因爲當用戶編寫一些文本並滾動表格時,這個新文本會混淆。 – Pompeyo 2013-04-27 16:30:37

回答

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 2013-04-28 11:19:52

相關問題