PDA

View Full Version : Trouble with TableView ->DetailView Cell not being ReUsable.




jjgraz
Apr 21, 2009, 10:28 AM
I have an application which uses a UITabBar with 5 tabs. The UITabBar has within a navigation controller for one of the tabbar items controlling a TableView for that particular view....The TableView then has a disclosure Button which moves to the detailView of selected Cell.
So : TabBarController ->5tabbarButtons one of which is NavigationController -> TableView -> DetailView.

The app gets xml data from my database/webservice and loads it all into each reusable cell to display an accurate history of events for a particular user.

The Problem is: detailView is only showing the first set of details for selected Cell:
In other words, Everything appears fine on the tableView(there are a few labels which display some basic details on each different history event for the user, However, when going on step further into detail view....The Cells in detailView display all the correct detail information for only the first cell selected. So if someone selects entry1 to see details, it displays correctly...but if they go back and then select entry2 after, entry 2 detailvew displays the details of entry1. The detailView Cells are not refreshing and allowing itself to be reused. I have looked my code over and over and can't find the solution. Please let me know if you can help. I very much appreciate it. I have included a bit of code below. Thank you.

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

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return [appDelegate.blts count];
}

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

static NSString *CellIdentifier = @"Cell";

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:CellIdentifier] autorelease];

*******Some more Code*******
}
return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
// Navigation logic -- create and push a new view controller

if(bltController == nil)
bltController = [[BltDetailViewController alloc] initWithNibName:@"BltDetailView" bundle:[NSBundle mainBundle]];

*******Some more Code*******

[self.navigationController pushViewController:bltController animated:YES];
}


Thank you.



dejo
Apr 21, 2009, 10:47 AM
I'd suggest first enclosing your code in [ CODE ] tags rather than bolding it. That's just hard to look at.

jjgraz
Apr 21, 2009, 11:56 AM
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return [appDelegate.blts count];
}

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

static NSString *CellIdentifier = @"Cell";

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:CellIdentifier] autorelease];

*******Some more Code*******
}
return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
// Navigation logic -- create and push a new view controller

if(bltController == nil)
bltController = [[BltDetailViewController alloc] initWithNibName:@"BltDetailView" bundle:[NSBundle mainBundle]];

*******Some more Code*******

[self.navigationController pushViewController:bltController animated:YES];
}

dejo
Apr 21, 2009, 01:03 PM
Does BltDetailViewController have any code in viewWillAppear or so that updates the data of that detail view? I.E. you need to refresh the data displayed in the detail view when based on which row is selected.

jjgraz
Apr 21, 2009, 01:22 PM
It does. the following code is in bltDetailViewController.m



- (void)viewWillAppear:(BOOL)animated {
[super viewWillAppear:animated];

[tableView reloadData];
}

dejo
Apr 21, 2009, 01:30 PM
Okay. That'll reload the tableView. What about the detail View? What is BltDetailView? Is it a subclass of UIView, UITableView, what? What kind of elements does it contain?

jjgraz
Apr 21, 2009, 02:15 PM
bltDetailView is actually another UITableView, and a subclass of UIView in the bltViewController.xib. It contains a bunch of NSStrings which are used to display history items in the bltDetailViews Indexed Table. history items such as(name, date, ect....)

jjgraz
Apr 21, 2009, 02:23 PM
bltDetailViewController.h



#import <UIKit/UIKit.h>

@class Blt;

@interface BltDetailViewController : UIViewController {

IBOutlet UITableView *tableView;

Blt *aBlt;
}

@property (nonatomic, retain) Blt *aBlt;

@end



bltDetailViewController.m


#import "BltDetailViewController.h"
#import "Blt.h"

@implementation BltDetailViewController

@synthesize aBlt;

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
if (self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]) {
}
return self;
}

// Implement viewDidLoad to do additional setup after loading the view.
- (void)viewDidLoad {
[super viewDidLoad];

self.title = @"Blt Detail";
}

- (void)viewWillAppear:(BOOL)animated {
[super viewWillAppear:animated];
[tableView reloadData];
}


- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
// Return YES for supported orientations
return (interfaceOrientation == UIInterfaceOrientationPortrait);
}


- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning]; // Releases the view if it doesn't have a superview
// Release anything that's not essential, such as cached data
}

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


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

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

static NSString *CellIdentifier = @"Cell";

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:CellIdentifier] autorelease];

}

switch(indexPath.section)

{
case 0:
cell.text = aBlt.firstname;
break;
case 1:
*****ect..******

return cell;
}

- (NSString *)tableView:(UITableView *)tblView titleForHeaderInSection:(NSInteger)section {

NSString *sectionName = nil;

switch(section)
{
case 0:
sectionName = [NSString stringWithString:@"Opp"];
break;
case 1:
*****ect..******

}

return sectionName;
}

- (void)dealloc {

[aBlt release];
[tableView release];
[super dealloc];
}


@end


hope this helps.

jjgraz
Apr 21, 2009, 06:37 PM
I got it. I do appreciate your thoughts Dejo. I actually had a silly if statement in there that was stopping the reload process. Thanks.

dejo
Apr 21, 2009, 07:17 PM
I got it. I do appreciate your thoughts Dejo. I actually had a silly if statement in there that was stopping the reload process. Thanks.
You're welcome. Sometimes it helps to post the real code so that we can better troubleshoot the issue. If you've never been stumped by code that doesn't work or you can't understand why until someone else looks at it, you haven't lived as a true programmer yet. :D