无法使用分段控件刷新 UITableView 数据



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

问题:当我按下任何一个分段控制按钮时,输出到日志的新数据集是正确的,但表视图没有刷新。

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

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

当您实例化表视图时,您正在执行以下操作:

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

你应该做:

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

最新更新