0

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

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

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

视图控制器.h

#import <UIKit/UIKit.h>

@interface ViewController : UIViewController <UITableViewDelegate, UITableViewDataSource>
{
    NSMutableArray *tableObjects;
}

@property (strong, nonatomic) UITableView *tableView;

@end

视图控制器.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
4

1 回答 1

2

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

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

你应该做:

self.tableView = [[UITableView alloc] initWithFrame:tableframe style:UITableViewStylePlain];
于 2013-10-29T14:53:00.067 回答