2

I am trying to refresh an UITableView every time I navigate the the view that contains this Table.

I Have a ViewController and a custom UITableViewController that manages to set the Table Correctly when the application starts, using an NSMutableArray contained inside the controller.

When I navigate to the page containing the table, the ViewController calls a function that gets the data from a server with an HTTP request and parse it in an NSMutableArray.

Now here is my problem. I manage to send this array to my UITableViewController, but when I want to refresh my tableView, nothing happens.

I tried to use [myTable reloadData], but it doesn't calls the numberOfRowsInSection, or cellForRowAtIndexPath functions. I saw that people with the same problem solved it using [self.myTable ReloadData], but I get an error :

accessing unknown getter/setter method

I am pretty new to objective-C, and this error is still a bit mysterious to me as I get it a bit randomly.

Anyway, there is a high probability that I made a mess with the declaration of the UITableViewController (where am I supposed to declare it?) and with the Interface Builder links, so this can be a clue to find the solution.

Any one have an idea?

Thank you very much!

EDIT : Here is my tableview controller class:

#import "MyCell.h"

@class Mycell;

@interface MyTableController : UITableViewController {

    IBOutlet MyCell * myCell;
    IBOutlet UITableView * myTable;
    NSMutableArray *data;

}

@property (nonatomic, retain) IBOutlet UITableView * myTable;
- (void) EditTable : (NSMutableArray*) param;


@end

And now the .m:

@implementation MyTableController
@synthesize myTable;

- (void) viewDidLoad {

[super viewDidLoad];
myTable = [[UITableView alloc] init];   
data = [[NSMutableArray alloc] init];
}


- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {

return 1;

}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {

return [data count];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

static NSString *CellIdentifier = @"MyCell";

MyCell *cell = (MyCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];  >

if (cell == nil) {

    NSArray *topLevelObjects = [[NSBundle mainBundle] loadNibNamed:@"MyCell" owner:self options:nil];

    for (id currentObject in topLevelObjects){

        if ([currentObject isKindOfClass:[UITableViewCell class]]){

        cell =  (MyCell *) currentObject;

            }
        }

    }


    NSString *datastring = [listenom objectAtIndex:indexPath.row];
    [cell setCell: datastring ];
    return cell;
}


- (void) EditTable : (NSMutableArray*) param{   

//This function is called by the ViewController when the user goes to the page containing the view

    data = param; //The param array contains the data from the HTTP request

    [self.tableView reloadData];

    [self.myTable reloadData]; //I tried both, but only the first one actually calls the previous functions

}
Robin
  • 10,011
  • 5
  • 49
  • 75
Mathieu C.
  • 453
  • 1
  • 4
  • 10
  • Welcome to SO! I fixed the formatting of the code in your post so it's easier for people to work with. In future, the ">" is good to highlight a single line as code, but to do a block, you want to select it and hit the "{}" button at the top of the form. – Dan Ray Mar 11 '11 at 15:58
  • Thank you ! I was a bit embarassed when I saw my own message, I will definitely try to write my code more properly next time. – Mathieu C. Mar 11 '11 at 16:12
  • @Mathiew - No worries. You should have seen MY first post! – Dan Ray Mar 11 '11 at 17:29
  • most probably it happens because return [data count]; returns 0.... – Mihir Mehta Mar 12 '11 at 10:46
  • I added a NSLog to check the value of [data count], and it returns the size of the array I want to display in the Table. If I add some items in my array in the ViewDidLoad function, cellForRowAtIndexPath is called when the application starts, but not when I reload the data, even if fhe value of [data count] is updated. Did i forgot anything to set my array as the table database? – Mathieu C. Mar 12 '11 at 14:28
  • How did you connect myCell and myTable in IB? – Sefran2 Apr 09 '11 at 18:30

3 Answers3

2

You have a number of problems in this code sample. I'll point out a few of them here but I highly recommend reading the relevant Apple documentation at:

http://developer.apple.com/library/ios/#featuredarticles/ViewControllerPGforiPhoneOS/Introduction/Introduction.html

and

http://developer.apple.com/library/ios/#documentation/userexperience/conceptual/TableView_iPhone/AboutTableViewsiPhone/AboutTableViewsiPhone.html

Some issues in your code:

  1. Since the class MyTableController is a subclass of UITableViewController you don't need the attribute and property for myTableView. The tableView property is defined and initialized as part of UITableViewController's implementation with its dataSource and delegate set to the UITableViewController instance. This is why [self.tableView reloadData] is calling your delegate and dataSource protocol methods.

  2. You are also using interface builder so if you did want to create your own subviews you should either do that within IB and set the outlet there or do it in your code which means creating the subview(s) in viewDidLoad and then adding them to your view with [view addSubview:mySubView].

  3. A better way to set the data for your table would be to create a property for your data attribute and call setData from the view controller that has initialized the MyTableController instance. You would use the setData: method to do this. You can call [self.tableView reloadData] in setData. You don't need to explicitly reload the table when the view is loaded as this is done automatically. A more minor point, if you stay with EditTable I would rename it to be more descriptive and to use camel case (e.g. setDataForTable`) to be consistent with iOS conventions.

  4. You don't show any init/alloc for the listenom attribute referenced in tableView:cellForRowAtIndexPath:. Did you mean to use data instead?

  5. Is your MyTableController.m file the complete version? If so, you are missing viewDidUnload and dealloc methods. Both of which are required. viewDidUnload should release any objects allocated in viewDidLoad and dealloc should release anything retained by the controller (including objects released in viewDidUnload.

XJones
  • 21,959
  • 10
  • 67
  • 82
0

As you are using tableViewController you should be able to use self.tableView instead to reload the data like this

[self.tableView reloadData];
Robin
  • 10,011
  • 5
  • 49
  • 75
0

you need to synthesize first then you can use self.myTable

do on the top

@synthesize myTable

and then

[self.myTable reloadData];
Mihir Mehta
  • 13,743
  • 3
  • 64
  • 88
  • first thank you for responding. The reload now goes through numberofsections and numberOfRowsInSection, but still never call cellForRowAtIndexPath. I checked and NumberOfRowsInSections returns the size of my Array. I updated my post to add some code to help, but I actually think the problem is I didnt set the database correctly. thank you again in advance! – Mathieu C. Mar 11 '11 at 15:40