Reputation: 1123
I've got a custom TableViewCell with a dynamic tableviewcontroller screenshot of my app
the products are saved in CoreData and i can fetch them (Name,price etc.) but i don't know how to implement the amount of the products. its a textfield and i want to save the textfield when i click on the basket button.
here is my 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
my 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;
}
my basket button:
- (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]);
}
}
Upvotes: 0
Views: 1366
Reputation: 1972
Here's what I understood. You want to get the value entered on the text field. (Right?) In ProduktTableViewCell.h, create an outlet for your UITableView element in the xib file--So, something like:
@property (retain, nonatomic) IBOutlet UITableView *myTableView;
Please take care of the property attributes. The code above is just an example. Then, on ProduktTableViewCell.m synthesize myTableView
and declare a global variable:
static NSIndexPath *myIndexPath;
Next, on - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
save the indexPath:
myIndexPath = indexPath;
Also, assign a unique value to cell.menge.tag
:
cell.menge.tag = indexPath.row + 437812;
Now, in - (IBAction)warenkorbButton:(id)sender
extract the cell containing the text field and eventually getting the text field:
UITableViewCell *cell = [myTableView cellForRowAtIndexPath:myIndexPath];
UITextField *textField = (UITextField *)[cell.contentView viewWithTag:indexPath.row + 437812];
Finally, the text is in textField.text
.
Hope this helps!
Upvotes: 2