0

I created a custom UITableViewCell with a UITextField, UILabels and UIButtons. When I enter a value to a UITextField and scroll down, it disappears and shows in another random cell. The same problem happens with my UIButton, when I press it, the state is changing but after I scroll down, it disappears.

Here is my 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;
}

EDIT: i fixed my problem with the textfield. i just delete the method:

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


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


}*/

but there is still a problem with my button.

4

1 回答 1

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];

这样,您的新单元格将不会获得与前一个单元格相同的“状态”。接下来你需要做的是知道你在哪个单元格上工作。除此之外,您仍然需要重置新单元格的状态。最后,取消注释我告诉您要评论的内容(因为您希望您的单元格可重复使用)

于 2012-05-08T08:33:18.237 回答