-1

I'm noob in Obj-c programming. I'm making an iPad application composed of a ViewController that shows me 2 text boxes , a button in the upper side , just to make a sum.

In the same Viewcontroller, i have a TableView and a Button. This Tableview initially shows me an element, but i'd want to refresh this list just clicking the button.

The function linked at the button to refresh the list ( touch down event ) is called " listaPDF " .

Viewcontroller.h

#import <UIKit/UIKit.h>

@interface VPViewController : UIViewController <UITableViewDelegate, UITableViewDataSource> {
    IBOutlet UITextField *txtPrimoAddendo ;
    IBOutlet UITextField *txtSecondoAddendo ;
    IBOutlet UILabel *lblTotale ;
    IBOutlet UITableView *tabella;
    NSMutableArray *lista;
   }

@property (nonatomic, retain) IBOutlet UITextField *txtPrimoAddendo;
@property (nonatomic, retain) IBOutlet UITextField *txtSecondoAddendo;
@property (nonatomic, retain) IBOutlet UILabel *lblTotale;
@property (nonatomic, retain) IBOutlet UITableView *tabella;
@property (nonatomic, retain) NSMutableArray *lista;

-(IBAction)somma ;
-(IBAction)listaPDF;

@end

Viewcontroller.m

    #import "VPViewController.h"

    @interface VPViewController ()

    @end

    @implementation VPViewController

    -(void)somma {

    int x = [[txtPrimoAddendo text] intValue];
    int y = [[txtSecondoAddendo text] intValue];
    int somma = x + y ;

    NSString *totale = [NSString stringWithFormat:@"La somma fa : %d", somma];

    [lblTotale setText:totale];
    [lblTotale setHidden:FALSE];


    }
    - (void)listaPDF {

    int contatore = 1;

    NSArray *dirFiles = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:[[NSBundle mainBundle] resourcePath] error: nil];
    for (int i = 0; i < dirFiles.count ; i++) {

        NSString *files = dirFiles[i];
        int indice = files.length - 4 ;
        NSString *extension = [files substringFromIndex:indice];

        if([extension  isEqual: @".pdf"]) {

            NSLog(@"********* File PDF : %@", files);
            [lista insertObject:files atIndex:contatore];
            contatore++;
        }
    }
    [tabella reloadData];

    }

    - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
    {
        return [lista count];
    }
    - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
    {
    static NSString *simpleTableIdentifier = @"CellID";

    UITableViewCell *cell = [tabella dequeueReusableCellWithIdentifier:simpleTableIdentifier];

    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:simpleTableIdentifier];
    }
    cell.imageView.image = [UIImage imageNamed:@"pdf.png"];
    cell.textLabel.text = [lista objectAtIndex:indexPath.row];
    return cell;
    }


    - (void)viewDidLoad
    {
        tabella.delegate = self;
        tabella.dataSource = self;

        tabella = [[ UITableView alloc] init];
        lista = [[NSMutableArray alloc] init];

        [lista insertObject:@"FIRST ELEMENT" atIndex:0];


    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    }

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

-(BOOL)textFieldShouldReturn:(UITextField *)textField{
    [textField resignFirstResponder];
    return YES;
}

@synthesize lblTotale;
@synthesize txtPrimoAddendo;
@synthesize txtSecondoAddendo;
@synthesize tabella;
@synthesize lista;


@end

Thanks

user2854276
  • 21
  • 1
  • 5

2 Answers2

1

I have tried your code as it is.

Why you initializing UITableView object in viewDidLoad. It doesn't need to be allocated if you are adding on Xib file. If you creating UITableView Programmatically then its required. Just comment/remove that initializing line and tableView's delegate methods will be called.

And In 'ListaPDF' function no items is being added to array 'lista', that's why no new entry was visible in UITableView.

Shahab Qureshi
  • 952
  • 1
  • 7
  • 19
0

NSMutableArray indices begin at 0 - not 1

int contatore = 1;

...

[lista insertObject:files atIndex:contatore];
contatore++;

Use addObject to add an object to a NSMutableArray

[lista addObject:files];

Remove all of your instance variables, because this:

 @property (nonatomic, retain) NSMutableArray *lista;

creates an instance variable called _lista - not lista

You have quite a bit of code backwards:

tabella.delegate = self;
tabella.dataSource = self;

tabella = [[ UITableView alloc] init];
lista = [[NSMutableArray alloc] init];

You're trying to set the delegate and datasource before the table is allocated.

It should be:

tabella = [[ UITableView alloc] init];
lista = [[NSMutableArray alloc] init];

tabella.delegate = self;
tabella.dataSource = self;

However the UITableView has been added inside Interface Builder, right?

That means it's already being created for you and you shouldn't be allocating the table here.

Also: inside Interface Builder you can rightclick+drag (or ctrl+leftclick+drag) to connect your UITableView to yourUIViewController as its delegate and datasource

Phil Wilson
  • 888
  • 6
  • 8