gpt4 book ai didi

ios - 更改未分段控件时不会加载 Tableview 数据

转载 作者:塔克拉玛干 更新时间:2023-11-02 21:14:15 25 4
gpt4 key购买 nike

//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 - TableView 数据源

- (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 - TableView 委托(delegate)

// 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

最佳答案

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

关于ios - 更改未分段控件时不会加载 Tableview 数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31332526/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com