Reputation: 183
Intent: I have a simple UITableView that I set up programmatically (no IB), and a segmented control which will replace the table's array and refresh the view.
Problem: When I press either of the segmented control buttons, the new data set output to the log is correct, but the tableView is not refreshed.
If I scroll the table, the cells get replaced with the correct data, but I want the cells to refresh instantly (without scrolling). When I initially load the table, my NSLog from tableView:numberOfRowsInSection: is written, however it never appears again upon pressing the button. Am I not setting up the delegate correctly?
ViewController.h
#import <UIKit/UIKit.h>
@interface ViewController : UIViewController <UITableViewDelegate, UITableViewDataSource>
{
NSMutableArray *tableObjects;
}
@property (strong, nonatomic) UITableView *tableView;
@end
ViewController.m
#import "ViewController.h"
@interface ViewController ()
@end
@implementation ViewController
- (void)viewDidLoad
{
[super viewDidLoad];
tableObjects = [[NSMutableArray alloc] initWithObjects:@"1", @"2", @"3", nil];
//Create toggle frame
UILabel *toggleFrame = [[UILabel alloc] init];
toggleFrame.frame = CGRectMake(0, 0, 320, 40);
[self.view addSubview:toggleFrame];
//Create the segmented control
NSArray *itemArray = [NSArray arrayWithObjects: @"Numbers", @"Letters", nil];
//Set font size
UIFont *font = [UIFont boldSystemFontOfSize:14.0f];
NSDictionary *attributes = [NSDictionary dictionaryWithObject:font
forKey:UITextAttributeFont];
UISegmentedControl *segmentedControl = [[UISegmentedControl alloc] initWithItems:itemArray];
[segmentedControl setTitleTextAttributes:attributes
forState:UIControlStateNormal];
segmentedControl.frame = CGRectMake(70, 5, 180, 30);
segmentedControl.segmentedControlStyle = UISegmentedControlStylePlain;
segmentedControl.selectedSegmentIndex = 0;
[segmentedControl addTarget:self
action:@selector(showList:)
forControlEvents:UIControlEventValueChanged];
[self.view addSubview:segmentedControl];
//Create frame with tableview in it
CGRect tableframe = CGRectMake(0,40,320,400);
//Instantiate the TableView
UITableView *tableView = [[UITableView alloc] initWithFrame:tableframe style:UITableViewStylePlain];
tableView.delegate = self;
tableView.dataSource = self;
[self.view addSubview:tableView];
}
-(void) showList:(id)sender{
UISegmentedControl *segmentedControl = (UISegmentedControl *)sender;
if (segmentedControl.selectedSegmentIndex == 0) {
tableObjects = [[NSMutableArray alloc] initWithObjects:@"1", @"2", @"3", nil];
NSLog(@"%@",tableObjects);
[self.tableView reloadData];
} else {
tableObjects = [[NSMutableArray alloc] initWithObjects:@"Alpha", @"Bravo", @"Charlie", nil];
NSLog(@"%@",tableObjects);
[self.tableView reloadData];
}
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
NSLog(@"Number of Rows Called");
return 3;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Cell"];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:@"Cell"];
}
cell.textLabel.text = [tableObjects objectAtIndex:indexPath.row];
return cell;
}
@end
Upvotes: 0
Views: 457
Reputation: 1524
When you instantiate your table view you are doing:
UITableView *tableView = [[UITableView alloc] initWithFrame:tableframe style:UITableViewStylePlain];
You should do:
self.tableView = [[UITableView alloc] initWithFrame:tableframe style:UITableViewStylePlain];
Upvotes: 2