Pramesh
Pramesh

Reputation: 1244

Populate UITableView from button pressed

I am trying to populate UITableView from NSMutableArray. I have UITextField and a button on a ViewController. When I type any text in the UITextField and click button, I can see the text being added to the array with NSLog. I set breakpoints on the data source method of UITableView but it does not even hit those breakpoints when I click the button.

#import <UIKit/UIKit.h>

@interface ViewController : UIViewController<UITableViewDataSource, UITableViewDelegate>
{
    NSMutableArray *arrBarcode;
    IBOutlet UITextField *txtInsert;
}
@property IBOutlet UITableView *myTableView;
-(IBAction)btnPressed:(id)sender;
@end



#import "ViewController.h"

@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad
{
    [super viewDidLoad];
    self.myTableView.delegate = self;
    self.myTableView.dataSource = self;
    arrBarcode = [[NSMutableArray alloc]init];
}

-(IBAction)btnPressed:(id)sender{
    [arrBarcode addObject:txtInsert.text];
    NSLog(@"array count is : %i", [arrBarcode count]);
    [self.myTableView reloadData];
 }

 -(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
     [txtInsert resignFirstResponder];
 }

 - (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

-(NSInteger)numberOfSectionsInTableView: (UITableView *)tableView{
    return 1;
 }

 -(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    if ([arrBarcode count] == 0){
        return 0;
   }
   else{
      NSLog(@"Number of Rows : %i", [arrBarcode count]);
      return [arrBarcode count];
   }
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:    (NSIndexPath *)indexPath{
    static NSString *CellIdentifier = @"Cell Identifier";
    [tableView registerClass:[UITableViewCell class]   forCellReuseIdentifier:CellIdentifier];
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
    if (cell == nil){
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault  reuseIdentifier:CellIdentifier];
}
    NSString *barcode = [arrBarcode objectAtIndex:[indexPath row]];
    [cell.textLabel setText:barcode];
    return cell;
}

@end

When I initialize a NSMutable Array with some data in the viewDidLoad method, the UITableView is populating fine but not with dynamic array. I am newbie in Objective C, Can someone point me to right direction?

Upvotes: 0

Views: 814

Answers (2)

Jzon
Jzon

Reputation: 89

I'm not sure but the following line looks strange to me:

@synthesize myTableView = myTableView_;

This is telling the compiler to make a getter and setter for the property myTableView and backing it with an iVar named myTableView_. But in your case you have already defined an iVar named myTableView_.

Try connecting the UITableView as a property instead. A property will be backed by an instance variable with the form _yourProperty and have getter and setter generated automatically so @synthesize isn't really needed in this case.

Upvotes: 1

Mundi
Mundi

Reputation: 80265

Code looks OK (even if not very efficient). You have to check if the button is indeed connected to the action. In storyboard or Interface Builder, select the button and check the rightmost inspector on the right. See if the action is correctly connected.

Maybe you want to get rid of the touchesBegan call and call resignFirstResponder when the button is pressed.

For numberOfRowsInSection I think this is enough:

return arrBarcode.count;

Upvotes: 1

Related Questions