Advertise Mobile SDKs Books Events Forum News Social Networking Support Us
Follow @iphonedevsdk on Twitter

Interface 2, Advanced iOS
Mockup & Code Gen
($9.99)

Make your own iPhone apps
and run them live!
(free)

Pic Frame Dynamo: Photo Editing
($0.99)

Abiliator
($1.99)

Want your application or service advertised on iPhone Dev SDK?

Go Back   iPhone Dev SDK Forum > iPhone SDK Development Forums > iPhone SDK Development

Reply
 
LinkBack Thread Tools Display Modes
Old 03-19-2011, 05:02 PM   #1 (permalink)
Registered Member
 
Join Date: Mar 2011
Posts: 5
whoDatNinja is on a distinguished road
Exclamation HELP!.UITableView not reloading data on reloadData() or calling cellForRowAtIndexPath

I am having a serious issue with UITableView reloadData method. I have a UIViewController class, WiGiMainViewController that has a UITableView and NSMuttableArray in it. I am currently issuing network calls in the AppDelegate, and posting notifications to the WiGiMainViewController once the data has been downloaded. In my selector method for the notification, reloadWigiList, I am passing an NSArray containing the the recently downloaded items. I then initialize the WiGiMainViewController's NSMuttableArray with the passed in array and proceed to call reloadData() on my UITableView object. I can see from nslog statements that the numberOfRowsInSection is fired on reload but not the cellForRowAtIndexPath, therefore causing the UI NOT to reload the UITableView with the newly downloaded items. I have verified that the reloadData method is being called on the main thread and that the datasource delegate are set in IB and programatically in the viewDidLoad method of WiGiMainViewController. Any ideas why my UITableView, wigiLists isn't reloading the data, in particular, not calling the cellForRowAtIndexPath method?

EDIT: When the view first loads numberOfRowsInSection is 0. But this should be correct, as at this time the itemlist count is equal to 0, and won't be filled until the items are downloaded. However afterwards, when the data is downloaded the itemList count is equal 6 and upon calling reloadData, the numberOfRowsInSection returns a count of 6. I have noticed that if i put 1 or a number greater than 0 for numberofRows I can see the first item shown on the tableview after I scroll.

EDIT: I am PRETTY sure the delegate and datasource are set correctly in both IB and viewDidLoad(). When reloadData is issued, numberOfRowsInSection is called, but cellForRowAtIndexPath isn't.
Code:
@interface WiGiMainViewController : UIViewController<FBRequestDelegate,UITableViewDelegate, UITableViewDataSource> {

     //setup UI
     UITableView *wigiLists;
     
     WiGiAppDelegate *myAppDelegate;
     NSMutableArray *itemsList;

}
@property (nonatomic, retain) WiGiAppDelegate *myAppDelegate;
@property (nonatomic, retain) IBOutlet UITableView *wigiLists;
@property (nonatomic, retain) NSMutableArray *itemsList;

-(void) reloadWigiList: (NSNotification*) notification;
-(NSInteger) numberOfSectionsInTableView:(UITableView *)tableView;
-(NSInteger) tableView: (UITableView*) tableView numberOfRowsInSection:(NSInteger) section;
-(UITableViewCell *) tableView: (UITableView*)tableView cellForRowAtIndexPath: (NSIndexPath *)indexPath;

@end


@implementation WiGiMainViewController

@synthesize headerLabel = _headerLabel, userNameLabel = _userNameLabel, facebookPicture = _facebookPicture,
myAppDelegate, wigiLists, itemsList;

- (void)viewDidLoad {
     NSLog(@"In viewDidLoad");
     [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(reloadWigiList:) name:@"wigiItemUpdate" object:nil];

     
     // get appdelicate
     self.myAppDelegate = (WiGiAppDelegate*) [[UIApplication sharedApplication] delegate];     
     self.itemsList = [[NSMutableArray alloc] init];
     //setup tableview
      self.wigiLists = [[UITableView alloc] initWithFrame:CGRectZero style:UITableViewStylePlain];
          self.wigiLists.delegate = self;
     self.wigiLists.dataSource = self;
     //set up view
     [self.headerLabel setText:self.myAppDelegate.HEADER_TEXT];
     //check if user is logged in
     if (self.myAppDelegate.isLoggedIn) {
          //user is logged in
          NSLog(@"HERE");
          //get facebook information to populate view
          [self retrieveFacebookInfoForUser];
          //[self.myAppDelegate retrieveWigiItems];
     }else {
          //user is not logged in
          NSLog(@"user not logged in");
          //show login modal
     }
     //[self.wigiLists reloadData];
     [super viewDidLoad];
}

-(void) reloadWigiList: (NSNotification *) notification {
     if ([NSThread isMainThread]) {
          NSLog(@"main thread");
     }else{
          NSLog(@"MEHTOD NOT CALLED ON MAIN THREAD!");
     }
     NSLog(@"notification recieved:%@", notification.userInfo);
     
     NSLog(@"in reloadwigilists:%@", wigiLists );
     NSLog(@"list size:%@", self.itemsList);
     NSLog(@"delegate:%@",self.wigiLists.delegate);
     NSLog(@"datasource:%@",self.wigiLists.dataSource);
     //populate previously empty itemsList
     [self.itemsList setArray:notification.userInfo];
     NSLog(@"list size:%@", self.itemsList);
     [self.wigiLists reloadData];
     
}

/////////////////////////////////////
// UITableViewDelegate protocols
-(NSInteger) numberOfSectionsInTableView:(UITableView *)tableView {
     //NSLog(@"numberofsections: %@", [self.itemsList count]);
     return 1;
}

-(NSInteger) tableView: (UITableView *)tableView numberOfRowsInSection:(NSInteger) section {
     NSLog(@"7t87giuiu%@",self.itemsList);
     NSLog(@"numberofrows: %i", [self.itemsList count]);


          return [self.itemsList count];

     //return 6;

}

Last edited by whoDatNinja; 03-19-2011 at 07:01 PM. Reason: updating information
whoDatNinja is offline   Reply With Quote
Old 03-19-2011, 05:31 PM   #2 (permalink)
Registered Member
 
Join Date: Mar 2011
Posts: 26
L14M333 is on a distinguished road
Default

try using

Code:
[self.tableView reloadData]
L14M333 is offline   Reply With Quote
Old 03-19-2011, 05:46 PM   #3 (permalink)
Registered Member
 
Join Date: Mar 2011
Posts: 5
whoDatNinja is on a distinguished road
Default

Quote:
Originally Posted by L14M333 View Post
try using

Code:
[self.tableView reloadData]
Thanks for the response. My tableView is named wigiLists and I have done:
Code:
 [self.wigiLists reloadData]
but that doesn't help. I don't think I can do self.tableView as I did not declare a variable named tableView.
whoDatNinja is offline   Reply With Quote
Old 03-19-2011, 05:54 PM   #4 (permalink)
Reading the Documentation
 
baja_yu's Avatar
 
Join Date: Sep 2010
Location: 45.255019,19.844908
Posts: 5,414
baja_yu has a spectacular aura about
Default

Are the DataSource and Delegate for that tableview set to the class that implements the cellForRowAtIndexPath and other methods?
baja_yu is offline   Reply With Quote
Old 03-19-2011, 06:02 PM   #5 (permalink)
Registered Member
 
Join Date: Mar 2011
Posts: 26
L14M333 is on a distinguished road
Default

Quote:
Originally Posted by whoDatNinja View Post
Thanks for the response. My tableView is named wigiLists and I have done:
Code:
 [self.wigiLists reloadData]
but that doesn't help. I don't think I can do self.tableView as I did not declare a variable named tableView.

I know, but if you delegated it, It should respond to it... I think i had the same issue once and it fixed it...

I may be wrong though because im fairly new... worth a shot tho




Liam
L14M333 is offline   Reply With Quote
Old 03-19-2011, 06:57 PM   #6 (permalink)
Registered Member
 
Join Date: Mar 2011
Posts: 5
whoDatNinja is on a distinguished road
Default

Quote:
Originally Posted by baja_yu View Post
Are the DataSource and Delegate for that tableview set to the class that implements the cellForRowAtIndexPath and other methods?
Yes, I print them out in the nslog and they are both set to the WiGiMainViewController class.
whoDatNinja is offline   Reply With Quote
Old 03-19-2011, 07:32 PM   #7 (permalink)
Reading the Documentation
 
baja_yu's Avatar
 
Join Date: Sep 2010
Location: 45.255019,19.844908
Posts: 5,414
baja_yu has a spectacular aura about
Default

Wait a second. Where are the implementations of those methods? All I see is the declarations in the header file, which by the way you don't need to do, that's what conforming to a protocol is for. But I, for example, don't see the cellForRowAtIndexPath implemented anywhere.
baja_yu is offline   Reply With Quote
Old 03-19-2011, 07:41 PM   #8 (permalink)
Registered Member
 
Join Date: Mar 2011
Posts: 5
whoDatNinja is on a distinguished road
Default

Quote:
Originally Posted by baja_yu View Post
Wait a second. Where are the implementations of those methods? All I see is the declarations in the header file, which by the way you don't need to do, that's what conforming to a protocol is for. But I, for example, don't see the cellForRowAtIndexPath implemented anywhere.

I ommited it for brevity. Here it is though, its just being called when the app first starts but then not again.
Code:
-(UITableViewCell *) tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

	NSLog(@"trying to return cell");
	static NSString *CellIdentifier = @"Cell";
	UITableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
	if (cell==nil) {
		cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:CellIdentifier] autorelease];
	}
	
	//grab the item
	if ([self.itemsList count] >0) {
	NSDictionary *currentItem =(NSDictionary *) [self.itemsList objectAtIndex:indexPath.row];
	NSLog(@"%i",indexPath.row);
	//setup the cell
	cell.text = [currentItem objectForKey:@"item_comments"];
	NSURL *url = [NSURL URLWithString:[currentItem objectForKey:@"item_image_url"]];
	NSData *img = [[NSData alloc] initWithContentsOfURL:url];
	cell.imageView.image = [UIImage imageWithData:img]; 
		[img release];
	}

	
	return cell;
	
}
whoDatNinja is offline   Reply With Quote
Old 03-19-2011, 08:58 PM   #9 (permalink)
Registered Member
 
Join Date: Mar 2011
Posts: 5
whoDatNinja is on a distinguished road
Default

Wow! after 3 days of banging my head against this problem, it was something ridiculously simple. In my ViewDidLoad method of WiGiMainViewController, i was initializing my tableview:

self.wigiLists = [[UITableView alloc] initWithFrame:CGRectZero style:UITableViewStylePlain];

Because I had linked the tableView I created in IB to my instance wigiLists, alloc'ing and initializing in the ViewDidLoad overwrote my link to the tableView created in IB and currently being displayed. Getting rid of this line fixed everything.
whoDatNinja is offline   Reply With Quote
Reply

Bookmarks

Tags
cellforrowatindexpath, uitableview

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



» Advertisements
» Online Users: 356
11 members and 345 guests
7twenty7, condor304, Creativ, Domele, dreamdash3, laureix68, LEARN2MAKE, michelle, mistergreen2011, Sami Gh, tinamm64
Most users ever online was 1,387, 04-10-2012 at 04:21 AM.
» Stats
Members: 175,661
Threads: 94,119
Posts: 402,896
Top Poster: BrianSlick (7,990)
Welcome to our newest member, tinamm64
Powered by vBadvanced CMPS v3.1.0

All times are GMT -5. The time now is 01:30 AM.
Powered by vBulletin® Version 3.8.0
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.3.0