Note that there are some explanatory texts on larger screens.

plurals
  1. POAdding values in a table view cell
    text
    copied!<p>I have two view controllers. The CardWallet View Controller is my table view. Then the AddCard View Controller is where I input values for a new instance of an object named Card. So far, I am adding those Card instances in an array named myWallet which is in my CardWallet View Controller using a delegate and it works. </p> <p>What I want is, after clicking the button in my AddCard View Controller, a new table cell will appear in my Card Wallet View, with the name depending on the recently added instance of Card. Below is my code, kindly check why is it that when I'm finished adding a new instance of Card, nothing appears in my table. I've done some research and went through some tutorials, this one is good, <a href="http://kurrytran.blogspot.com/2011/10/ios-5-storyboard-and.html" rel="nofollow">http://kurrytran.blogspot.com/2011/10/ios-5-storyboard-and.html</a>, it helped me a lot regarding table view controllers. However, the tutorial doesn't cater my main concern for it's table's values only come from an array with static values.</p> <p>Thanks!</p> <p>CardWalletViewController.h</p> <pre><code>#import &lt;UIKit/UIKit.h&gt; @interface CardWalletViewController : UIViewController &lt;UITableViewDelegate, UITableViewDataSource&gt; { } @property (nonatomic, strong) NSMutableArray *myWallet; -(void) printArrayContents; @end </code></pre> <p>CardWalletViewController.m</p> <pre><code>#import "CardWalletViewController.h" #import "AddCardViewController.h" #import "Card.h" @interface CardWalletViewController () &lt;AddCardDelegate&gt; @end @implementation CardWalletViewController @synthesize myWallet = _myWallet; - (NSMutableArray *) myWallet { if (_myWallet == nil) _myWallet = [[NSMutableArray alloc] init]; return _myWallet; } - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender { if ([segue.identifier isEqualToString:@"showAddCardVC"]) { AddCardViewController *addCardVC = (AddCardViewController *)segue.destinationViewController; addCardVC.delegate = self; } } - (void)printArrayContents { // I want to show the name of each instance of card for ( int i = 0; i &lt; self.myWallet.count; i++) { Card *cardDummy = [self.myWallet objectAtIndex:i]; NSLog(@"Element %i is %@", i,cardDummy.name ); } } - (void)addCardViewController:(AddCardViewController *)sender didCreateCard:(Card *)newCard { // insert a new card to the array [self.myWallet addObject:newCard]; [self printArrayContents]; } - (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view, typically from a nib. } - (void)viewDidUnload { // Release any retained subviews of the main view. } - (void)viewWillAppear:(BOOL)animated { } - (void)viewWillDisappear:(BOOL)animated { } - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { //this method will return the number of rows to be shown return self.myWallet.count; } - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { NSString *CellIdentifier = @"Cell"; UITableViewCell *cell = (UITableViewCell *) [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; if (cell == nil) { cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier]; } // Configure the cell... //---------- CELL BACKGROUND IMAGE ----------------------------- UIImageView *imageView = [[UIImageView alloc] initWithFrame:cell.frame]; UIImage *image = [UIImage imageNamed:@"LightGrey.png"]; imageView.image = image; cell.backgroundView = imageView; [[cell textLabel] setBackgroundColor:[UIColor clearColor]]; [[cell detailTextLabel] setBackgroundColor:[UIColor clearColor]]; //this will show the name of the card instances stored in the array // for ( int i = 0; i &lt; self.myWallet.count; i++) { Card *cardDummy = [self.myWallet objectAtIndex:i]; cell.textLabel.text = cardDummy.name; } //Arrow cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator; return cell; } - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation { return (interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown); } @end </code></pre> <p>AddCardViewController.h</p> <pre><code>#import &lt;UIKit/UIKit.h&gt; #import "Card.h" @class AddCardViewController; @protocol AddCardDelegate &lt;NSObject&gt; - (void)addCardViewController:(AddCardViewController *)sender didCreateCard:(Card *) newCard; @end @interface AddCardViewController : UIViewController &lt;UITextFieldDelegate&gt; @property (strong, nonatomic) IBOutlet UITextField *cardNameTextField; @property (strong, nonatomic) IBOutlet UITextField *pinTextField; @property (strong, nonatomic) IBOutlet UITextField *pointsTextField; @property (nonatomic, strong) id &lt;AddCardDelegate&gt; delegate; @end </code></pre> <p>AddCardViewController.m</p> <pre><code>#import "AddCardViewController.h" #import "Card.h" #import "CardWalletViewController.h" @interface AddCardViewController () @end @implementation AddCardViewController @synthesize cardNameTextField = _cardNameTextField; @synthesize pinTextField = _pinTextField; @synthesize pointsTextField = _pointsTextField; @synthesize delegate = _delegate; - (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil { self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]; if (self) { // Custom initialization } return self; } - (void) viewWillAppear:(BOOL)animated { [super viewWillAppear:animated]; [self.cardNameTextField becomeFirstResponder]; } - (void) viewWillDisappear:(BOOL)animated { } - (BOOL) textFieldShouldReturn:(UITextField *)textField{ if ([textField.text length]) { [self.cardNameTextField resignFirstResponder]; [self.pinTextField resignFirstResponder]; [self.pointsTextField resignFirstResponder]; return YES; } else { return NO; } } - (void)viewDidLoad { self.cardNameTextField.delegate = self; self.pinTextField.delegate = self; self.pointsTextField.delegate = self; [super viewDidLoad]; // Do any additional setup after loading the view, typically from a nib. } - (void)viewDidUnload { [self setCardNameTextField:nil]; [self setPinTextField:nil]; [self setPointsTextField:nil]; [super viewDidUnload]; // Release any retained subviews of the main view. } - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation { return (interfaceOrientation == UIInterfaceOrientationPortrait); } - (IBAction)addCard:(id)sender { Card *myNewCard = [[Card alloc] init]; myNewCard.name = self.cardNameTextField.text; myNewCard.pin = self.pinTextField.text; myNewCard.points = [self.pointsTextField.text intValue]; // to check if the text fields were filled up by the user if ([self.cardNameTextField.text length] &amp;&amp; [self.pinTextField.text length] &amp;&amp; [self.pointsTextField.text length]) { [[self presentingViewController] dismissModalViewControllerAnimated:YES]; NSLog(@"name saved %@", myNewCard.name); NSLog(@"pin saved %@", myNewCard.pin); NSLog(@"points saved %i", myNewCard.points); [self.delegate addCardViewController:self didCreateCard:myNewCard]; // to check if there is a delegate if (self.delegate){ NSLog(@"delegate is not nil"); } } } @end </code></pre> <p>Card.h</p> <pre><code>#import &lt;Foundation/Foundation.h&gt; @interface Card : NSObject @property (nonatomic, strong) NSString *name; @property (nonatomic, strong) NSString *pin; @property (nonatomic) int points; @end </code></pre> <p>Card.m</p> <pre><code>#import "Card.h" @implementation Card @synthesize name = _name; @synthesize pin = _pin; @synthesize points = _points; @end </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload