将TextField内容保存在自定义动态tableView中

时间:2012-03-21 14:28:18

标签: iphone xcode core-data xcode4.3 ios5.1

我有一个带有动态tableviewcontroller的自定义TableViewCell screenshot of my app

产品保存在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]);
    }
}

1 个答案:

答案 0 :(得分: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

希望这有帮助!