无法使用SegmentedControl刷新UITableView数据

时间:2013-10-29 14:49:22

标签: ios iphone uitableview tableview uisegmentedcontrol

意图:我有一个简单的UITableView,我以编程方式设置(没有IB),以及一个分段控件,它将替换表的数组并刷新视图。

问题:当我按下任一分段控制按钮时,输出到日志的新数据集是正确的,但不会刷新tableView。

如果我滚动表格,单元格会被正确的数据替换,但我希望单元格立即刷新(不滚动)。当我最初加载表时,我写了来自tableView:numberOfRowsInSection:的NSLog,但是按下按钮后它再也不会出现了。我没有正确设置代表吗?

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

1 个答案:

答案 0 :(得分:2)

当您实例化表视图时:

UITableView *tableView = [[UITableView alloc] initWithFrame:tableframe style:UITableViewStylePlain];

你应该这样做:

self.tableView = [[UITableView alloc] initWithFrame:tableframe style:UITableViewStylePlain];