Trouble with TableView ->DetailView Cell not being ReUsable.

Discussion in 'iPhone/iPad Programming' started by jjgraz, Apr 21, 2009.

  1. macrumors regular

    jjgraz

    Joined:
    Feb 13, 2009
    #1
    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:mad:"BltDetailView" bundle:[NSBundle mainBundle]];

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

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


    Thank you.
     
  2. Moderator

    dejo

    Staff Member

    Joined:
    Sep 2, 2004
    Location:
    The Centennial State
    #2
    I'd suggest first enclosing your code in [ CODE ] tags rather than bolding it. That's just hard to look at.
     
  3. thread starter macrumors regular

    jjgraz

    Joined:
    Feb 13, 2009
    #3
    Code:
    - (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];
    }
    
     
  4. Moderator

    dejo

    Staff Member

    Joined:
    Sep 2, 2004
    Location:
    The Centennial State
    #4
    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.
     
  5. thread starter macrumors regular

    jjgraz

    Joined:
    Feb 13, 2009
    #5
    It does. the following code is in bltDetailViewController.m

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

    dejo

    Staff Member

    Joined:
    Sep 2, 2004
    Location:
    The Centennial State
    #6
    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?
     
  7. thread starter macrumors regular

    jjgraz

    Joined:
    Feb 13, 2009
    #7
    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....)
     
  8. thread starter macrumors regular

    jjgraz

    Joined:
    Feb 13, 2009
    #8
    bltDetailViewController.h

    Code:
    
    #import <UIKit/UIKit.h>
    
    @class Blt;
    
    @interface BltDetailViewController : UIViewController {
    
    	IBOutlet UITableView *tableView;
    	
    	Blt *aBlt;
    }
    
    @property (nonatomic, retain) Blt *aBlt;
    
    @end
    
    
    bltDetailViewController.m

    Code:
    #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.
     
  9. thread starter macrumors regular

    jjgraz

    Joined:
    Feb 13, 2009
    #9
    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.
     
  10. Moderator

    dejo

    Staff Member

    Joined:
    Sep 2, 2004
    Location:
    The Centennial State
    #10
    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
     

Share This Page