【问题标题】:Cannot Refresh UITableView Data With SegmentedControl无法使用 SegmentedControl 刷新 UITableView 数据
【发布时间】:2013-11-08 19:17:47
【问题描述】:

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

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

如果我滚动表格,单元格会被正确的数据替换,但我希望单元格立即刷新(不滚动)。当我最初加载表时,我的 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

【问题讨论】:

    标签: ios iphone uitableview tableview uisegmentedcontrol


    【解决方案1】:

    当你实例化你正在做的表格视图时:

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

    你应该这样做:

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

    【讨论】:

    • 非常感谢@Heliem。这解决了问题,但它让我不那么自信了!为什么将此表初始化为 self.tableView 而不是指向 UITableView 实例的指针是正确的?是不是因为.h文件中的属性声明?
    • 只是因为在 showList 方法中您调用 [self.tableView reloadData] 来重新加载数据。但是你没有为 self.tableView 分配任何东西。除非你让 self.tableView = [[UITableView alloc(...)] 那个变量总是 nil
    • 嗯。好的。那么我会多读一些关于自我的书。非常感谢您的帮助。
    猜你喜欢
    • 2012-01-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多