0

我有一个自定义 TableViewCell 和 我的应用程序的动态 tableviewcontroller 屏幕截图

产品保存在 CoreData 中,我可以获取它们(名称、价格等),但我不知道如何实现产品的数量。它是一个文本字段,当我单击购物篮按钮时,我想保存文本字段。

这是我的 TableViewCell.h:

 #import <UIKit/UIKit.h>

 @interface ProduktTableViewCell : UITableViewCell <UITextFieldDelegate>

 @property (nonatomic, weak) IBOutlet UILabel *produktnameLabel;
 @property (nonatomic, weak) IBOutlet UILabel *preisLabel;
 @property (nonatomic, weak) IBOutlet UILabel *vonDatumLabel;
 @property (nonatomic, weak) IBOutlet UITextField *menge;
 @property (nonatomic, weak) IBOutlet UILabel *bisDatumLabel;
 @property (strong, nonatomic) IBOutlet UIButton *datumButton;
 @property (strong, nonatomic) IBOutlet UIButton *warenkorbButton;
 @end

TableViewCell.m:

 #import "ProduktTableViewCell.h"

 @implementation ProduktTableViewCell
 @synthesize produktnameLabel;
 @synthesize preisLabel;
 @synthesize vonDatumLabel;
 @synthesize bisDatumLabel;
 @synthesize datumButton;
 @synthesize menge;
 @synthesize warenkorbButton;


 @end

我的 ProductViewController.m:

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

    ProduktTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"produktCell"];

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

    cell.produktnameLabel.text = [managedObject valueForKey:@"produktname"]; 
    cell.vonDatumLabel.text = [managedObject valueForKey:@"vondatum"];
    cell.bisDatumLabel.text = [managedObject valueForKey:@"bisdatum"]; 
    cell.datumButton.tag = indexPath.row;
    cell.warenkorbButton.tag = indexPath.row;
    cell.menge.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;
}

我的购物篮按钮:

- (IBAction)warenkorbButton:(id)sender {
    NSManagedObjectContext *context = [app managedObjectContext]; 
    UIButton *button = sender;
    NSInteger rowInIndexPath =button.tag;
    NSIndexPath *indexPath = [NSIndexPath indexPathForRow:rowInIndexPath inSection:0];
    Warenkorb *warenkorb = [NSEntityDescription insertNewObjectForEntityForName:@"Warenkorb" inManagedObjectContext:context];

   Produkt *produkt = [self.fetchedResultsController objectAtIndexPath:indexPath];

    warenkorb.produktname  =produkt.produktname ;
    warenkorb.vondatum = produkt.vondatum;
    warenkorb.bisdatum = produkt.bisdatum;
    warenkorb.preis =produkt.preis;

    NSError *error;
    if (![context save:&error]) 
    {
        NSLog(@"Fehler beim hinzufügen : %@", [error localizedDescription]);
    }
}
4

1 回答 1

2

这就是我的理解。您想要获取在文本字段中输入的值。(对吗?)在 ProduktTableViewCell.h 中,为 xib 文件中的 UITableView 元素创建一个出口——因此,类似于:

@property (retain, nonatomic) IBOutlet UITableView *myTableView;

请注意属性属性。上面的代码只是一个例子。然后,在 ProduktTableViewCell.m 上合成myTableView并声明一个全局变量:

static NSIndexPath *myIndexPath;

接下来,- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath保存 indexPath:

myIndexPath = indexPath;

此外,为 分配一个唯一值cell.menge.tag

cell.menge.tag = indexPath.row + 437812;

现在,- (IBAction)warenkorbButton:(id)sender提取包含文本字段的单元格并最终获得文本字段:

UITableViewCell *cell = [myTableView cellForRowAtIndexPath:myIndexPath];
UITextField *textField = (UITextField *)[cell.contentView viewWithTag:indexPath.row + 437812];

最后,正文在textField.text.


希望这可以帮助!

于 2012-03-21T19:00:43.720 回答