2012-05-08 49 views
0

我創建了自定義UITableViewCell,其中UITextFieldUILabelsUIButtons。當我輸入一個值到UITextField並向下滾動時,它會消失並顯示在另一個隨機單元格中。我的UIButton發生同樣的問題,當我按下它時,狀態正在改變,但在向下滾動後,它消失。滾動後自定義UITableViewCells的UITextField混合

這裏是我的cellForRowAtIndexPath:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    static NSString *CellIdentifier = @"produktCell"; 
    //NSString *CellIdentifier = [NSString stringWithFormat:@"produktCell %d",indexPath.row]; 

    ProduktTableViewCell *cell =(ProduktTableViewCell *) [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 

    if (cell == nil) { 
      NSLog(@"wird neu erstellt"); 
     cell = [[ProduktTableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier]; 
    } 



    NSManagedObject *managedObject = [self.fetchedResultsController objectAtIndexPath:indexPath]; 

    cell.produktnameLabel.text = [managedObject valueForKey:@"produktname"]; 



    if ([[managedObject valueForKey:@"vondatum"] length] > 1){ 
     cell.vonDatumLabel.text = [managedObject valueForKey:@"vondatum"]; 
     cell.bisDatumLabel.text = [managedObject valueForKey:@"bisdatum"]; 

    }else { 
     cell.vonDatumLabel.text = dateString; 
     cell.bisDatumLabel.text = dateString1; 
    } 


     cell.datumButton.tag = indexPath.row; 
    cell.warenkorbButton.tag = indexPath.row; 
    cell.menge.tag = indexPath.row + 437812; 
    cell.mengeLabel.text = [managedObject valueForKey:@"mengelabel"]; 
    cell.produktnameLabel.tag = indexPath.row +22333; 
    cell.mengeLabel.tag =indexPath.row; 


    NSNumberFormatter *formatter = [[NSNumberFormatter alloc] init]; 
    NSLocale *german = [[NSLocale alloc] initWithLocaleIdentifier:@"de_DE"]; 
    [formatter setLocale:german]; 
    [formatter setNumberStyle:NSNumberFormatterCurrencyStyle]; 
    [formatter setMinimumFractionDigits:2]; 
    [formatter setMaximumFractionDigits:2]; 


    NSNumber *firstNumber = [managedObject valueForKey:@"preis"]; 

    cell.preisLabel.text = [formatter stringFromNumber:firstNumber]; 



    return cell; 
} 

編輯:我固定我的問題與文本框。我只是刪除方法:

/*- (void) scrollViewWillBeginDragging:(UIScrollView *)scrollView 
{ 


    [[self.tableView superview] endEditing:YES]; 


}*/ 

但我的按鈕仍然有問題。

回答

0

我最好的猜測是,你正在重複使用的細胞正在接收與你剛剛交互過的細胞的相同行爲。你可以快速嘗試一下,看看我的意思。評論此:

編輯:

ProduktTableViewCell *cell =(ProduktTableViewCell *) [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 

    if (cell == nil) { 
      NSLog(@"wird neu erstellt"); 
     cell = [[ProduktTableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier]; 
    } 

將這個來代替:

ProduktTableViewCell *cell = [[ProduktTableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier]; 

這樣,你的新細胞將不會得到相同的 「狀態」 爲以前的小區。接下來你需要做的是知道你正在處理哪個細胞。除此之外,您仍然需要重置新單元格的狀態。 要完成,請取消註釋我所說的評論(因爲您希望您的單元格可以重複使用)

+0

當我評論此行時,它具有相同的行爲。桌面視圖仍然混合起來。 –

+0

檢查我的編輯.. – Peres

+0

我試過了,但我無法進入(cell == nil)。 tableview是空的,NSLog沒有被調用。 –