当未分段控件更改时,Tableview数据不会加载

时间:2015-07-10 04:35:11

标签: ios uitableview uisegmentedcontrol

// tableview没有改变。我已经好几天了。看起来很简单。谢谢你的帮助

#import "StopsTableViewController.h"

static NSString *MyIdentifier = @"RouteListing";

@interface StopsTableViewController ()

@property (strong, nonatomic) NSArray *TESTARRAY;

@end


@implementation StopsTableViewController

- (instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];

    if (self) {
        //Set the tab bar item's title
        self.title = @"Stops";

    }        

    self.stopList = [[API sharedAPI] fetchStopListing];

    self.TESTARRAY = @[@"Josh", @"Kyle", @"Nate"];

    return self;
}

- (void)viewDidLoad {
    [super viewDidLoad];

    [self.tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:MyIdentifier];

    // Adds Segmented Control
    [self segmentedView];    

}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void) segmentedView {
    NSArray *segmentedMenu = [NSArray arrayWithObjects:@"All", @"Near Me", nil];
    UISegmentedControl *segmentedControl = [[UISegmentedControl alloc] initWithItems:segmentedMenu];
    [segmentedControl addTarget:self
                         action:@selector(valueChanged:)
               forControlEvents:UIControlEventValueChanged];

    segmentedControl.selectedSegmentIndex = 0;
    self.navigationItem.titleView = segmentedControl;

}

pragma mark - 表视图数据源

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    // Return the number of sections.
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    if ([self.segmentedControl selectedSegmentIndex] == 0) {
        return  [self.stopList count];

    }
    else {
        return [self.TESTARRAY count];

    }
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

    UITableViewCell *cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:MyIdentifier];
    cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;

    if (_segmentedControl.selectedSegmentIndex == 0) {

            cell.textLabel.text = [[self.stopList objectAtIndex:indexPath.row] objectForKey:@"stoptitle"];
            cell.detailTextLabel.text = [NSString stringWithFormat:@"Stop %@", [[self.stopList objectAtIndex:indexPath.row] objectForKey:@"stopnumber"]];
    }

    else {
        cell.textLabel.text = self.TESTARRAY[indexPath.row];

    }

    return cell;
}

pragma mark - 表视图委托

// In a xib-based application, navigation from a table can be handled in -tableView:didSelectRowAtIndexPath:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    // Navigation logic may go here, for example:
    // Create the next view controller.
    StopInfoViewController *detailViewController = [[StopInfoViewController alloc] initWithNibName:@"StopInfoViewController" bundle:nil];

    // Pass the selected object to the new view controller.

    detailViewController.stopInfo = [[self.stopList objectAtIndex:indexPath.row] objectForKey:@"stopnumber"];

    detailViewController.stopName = [[self.stopList objectAtIndex:indexPath.row] objectForKey:@"stoptitle"];


    // Push the view controller.
    [self.navigationController pushViewController:detailViewController animated:YES];
}


//Function for segmentedView
-(void) valueChanged:(UISegmentedControl *)sender {

    [self.tableView reloadData];
    NSLog(@"I'm getting called segment number is: %ld", (long)sender.selectedSegmentIndex);

}

@end

3 个答案:

答案 0 :(得分:1)

在调用表重载方法之前检查数据源数组,并确保该数组包含与您选择的段相对应的新值。

答案 1 :(得分:0)

您是否设置了tableview委托?

尝试将其放入viewDidLoad功能。

[tableView setDelegate:self];
[tableView setDataSource:self];

如果您想要更全面的解释,请点击此处:

how to set a tableview delegate

该帖子还解释了如何确保您的类订阅UITableViewDelegate和UITableViewDataSource协议

希望有所帮助。

答案 2 :(得分:0)

在使用表格视图时,我总是遇到的一件事是使用界面构建器将表格与界面连接起来。因为听起来你正在加载数据,所以当视图首次加载时。但是当你在没有连接表的情况下调用[self.tableView reloadData]时,什么都不会发生。

我忘了那个超级简单的步骤。希望这有帮助