GFlam
GFlam

Reputation: 1109

iOS reload UITableView using UISegmentedControl

Hi I'm trying to reload my table view based off of two different arrays. Which array should be loaded is determined by a segment control in the navigation bar. Currently it only will load the first array and nothing happens when the segment control is pressed. Below is my code any help as to why this isn't working is greatly appreciated. I've also checked that my IBAction segmenter is connected in the nib.

MessageViewController.h

#import <UIKit/UIKit.h>

@interface MessageViewController : UIViewController<UITableViewDelegate> {
    IBOutlet UISegmentedControl *segmentControl;
    IBOutlet UINavigationBar *navBar;
    IBOutlet UITableView *tableView;
}

@property (retain, nonatomic) IBOutlet UISegmentedControl *segmentControl;
@property (retain, nonatomic) IBOutlet UITableView *tableView;
@property (nonatomic, retain) NSMutableArray *inbox;
@property (nonatomic, retain) NSMutableArray *sent;

@end

MessageViewController.m

#import "MessageViewController.h"

@interface MessageViewController () <UITableViewDelegate>

@end

@implementation MessageViewController
@synthesize segmentControl;
@synthesize inbox;
@synthesize sent;

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        self.tabBarItem.title = NSLocalizedString(@"Messages", @"Messages");
        self.tabBarItem.image = [UIImage imageNamed:@"mail_2_icon&32"];
    }
    return self;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.tableView.delegate = self;
    self.inbox = [NSMutableArray arrayWithObjects:@"testing", @"test", @"another", nil];
    self.sent = [NSMutableArray arrayWithObjects:@"test", @"another", @"testing", nil];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}

#pragma mark Table view methods

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


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    if(segmentControl.selectedSegmentIndex == 0){
        return [inbox count];
    }else if(segmentControl.selectedSegmentIndex == 1){
        return [sent count];
    }else{
        return [inbox count];
    }
}

- (UITableViewCell *)tableView:(UITableView *)aTableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    static NSString *CellIdentifier = @"Cell";
    UITableViewCell *cell = [aTableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
    }
    if(segmentControl.selectedSegmentIndex == 0){
        NSString *cellValue = [inbox objectAtIndex:indexPath.row];
        cell.textLabel.text = cellValue;
    }else if(segmentControl.selectedSegmentIndex == 1){
        NSString *cellValue = [sent objectAtIndex:indexPath.row];
        cell.textLabel.text = cellValue;
    }else{
        NSString *cellValue = [inbox objectAtIndex:indexPath.row];
        cell.textLabel.text = cellValue;
    }
    return cell;
}

-(IBAction)segmenter{
    [tableView reloadData];
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {

}

- (void)dealloc {
    [inbox release];
    [sent release];
    [segmentControl release];
    [segmentControl release];
    [super dealloc];
}

- (void)viewDidUnload {
    [self setSegmentControl:nil];
    [segmentControl release];
    segmentControl = nil;
    [super viewDidUnload];
}

@end

Upvotes: 2

Views: 2936

Answers (3)

GFlam
GFlam

Reputation: 1109

Not sure why it wasn't working in the end all I did was delete the three classes and redo everything with the code above something must have just got borked along the way but it's working now and I'm a happy camper. Didn't need the delegate stuff either since that's all done in the nib so my original code worked fine.

Upvotes: 1

8vius
8vius

Reputation: 5836

The only problem I see is that you're using a view controller with a table view in it, but you're not setting up the delegate for it in your viewDidLoad, and you're not implementing the delegate for your view controller.

@interface MessageViewController () <UITableViewDelegate, UITableViewDataSource>

@end

In viewDidLoad:

self.tableView.delegate = self;
self.tableView.dataSource = self;

Also make sure you have everything hooked up properly in IB, both your segmented control and your table view.

EDIT: I made my own test as per what you're trying to do, Here's my own implementation file

Upvotes: 0

Bajaj
Bajaj

Reputation: 869

set the delegate and datasource methods and take breakpoint to check the data . your code is right .

tableview.delegate=self;
 tableView.datasource=self;

Upvotes: 0

Related Questions