在 UIViewController 中填充 UITableView 并使用 UISegmentedControl 更改数据源
Populating a UITableView within a UIViewController and changing data source with UISegmentedControl
我正在尝试填充一个 UITableView
,我把它放在 UIViewController
的下面 UISegmentedControl
,如图所示:
按下任何段后,我希望 UITableView
重新填充一组不同的数据。这是我的相关代码:
@interface OTValuesViewController ()
@property (weak, nonatomic) IBOutlet UITableView *tableView;
@property (weak, nonatomic) IBOutlet UISegmentedControl *segmentControl;
@end
@implementation OTValuesViewController
- (IBAction)segmentValueChanged:(id)sender {
UISegmentedControl *segment = self.segmentControl;
UINavigationItem *navItem = self.navigationItem;
UIBarButtonItem *addButton = navItem.rightBarButtonItem;
switch (segment.selectedSegmentIndex) {
case 0:
addButton.action = @selector(addNewVision:);
[self.tableView reloadData];
break;
case 1:
addButton.action = @selector(addNewPurpose:);
[self.tableView reloadData];
break;
case 2:
addButton.action = @selector(addNewPlan:);
[self.tableView reloadData];
break;
default:
[self.tableView reloadData];
break;
}
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UISegmentedControl *segment = self.segmentControl;
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"OTValuesTableViewCell" forIndexPath:indexPath];
NSArray *visions = [[OTVisionStore sharedStore] allVisions];
NSArray *purposes = [[OTPurposeStore sharedStore] allPurposes];
NSArray *plans = [[OTPlanStore sharedStore]allPlans];
OTVision *vision = [visions objectAtIndex:indexPath.row];
OTVision *purpose = [purposes objectAtIndex:indexPath.row];
OTVision *plan = [plans objectAtIndex:indexPath.row];
NSString *textLabel = [vision description];
if (segment.selectedSegmentIndex==0) {
textLabel = [vision description];
cell.textLabel.text = textLabel;
}
else if (segment.selectedSegmentIndex==1) {
textLabel = [purpose description];
cell.textLabel.text = textLabel;
}
else{
textLabel = [plan description];
cell.textLabel.text = textLabel;
}
return cell;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
UISegmentedControl *segment = self.segmentControl;
if(segment.selectedSegmentIndex==0) {
return [[[OTVisionStore sharedStore]allVisions]count];
}
else if (segment.selectedSegmentIndex==1) {
return [[[OTPurposeStore sharedStore]allPurposes]count];
}
else{
return [[[OTPlanStore sharedStore]allPlans]count];
}
}
- (void)viewDidLoad
{
[super viewDidLoad];
self.tableView.dataSource = self;
self.tableView.delegate = self;
self.title=@"Mission Statement";
UINavigationItem *navItem = self.navigationItem;
UIBarButtonItem *rbbi = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAdd
target:self
action:@selector(addNewVision:)];
navItem.rightBarButtonItem = rbbi;
}
- (void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];
[self.tableView reloadData];
}
我的问题是:当我构建和 运行 项目时,一切都很好——单元格填充在我的默认选项卡(视觉)中。当我单击 UISegmentedControl
按钮时,出现错误:
Terminating app due to uncaught exception 'NSRangeException', reason: '*** -[__NSArrayM objectAtIndex:]: index 1 beyond bounds [0 .. 0]'
如果我删除:
self.tableView.dataSource = self;
self.tableView.delegate = self;
从我的 viewDidLoad
来看,这个错误没有发生(尽管 UITableView
显然没有被填充。)这让我相信错误是由于我填充单元格引起的基于 UISegmentedController's
选定的细分,但是,我不知道具体是什么原因造成的。
如有任何帮助,我们将不胜感激!
问题出在您的 cellForRowAtIndexPath
方法中。您尝试对所有数组使用当前索引路径,而不仅仅是适用的数组。尝试这样的事情:
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UISegmentedControl *segment = self.segmentControl;
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"OTValuesTableViewCell" forIndexPath:indexPath];
if (segment.selectedSegmentIndex==0) {
NSArray *visions = [[OTVisionStore sharedStore] allVisions];
OTVision *vision = [visions objectAtIndex:indexPath.row];
NSString *textLabel = [vision description];
cell.textLabel.text = textLabel;
}
else if (segment.selectedSegmentIndex==1) {
NSArray *purposes = [[OTPurposeStore sharedStore] allPurposes];
OTVision *purpose = [purposes objectAtIndex:indexPath.row];
NSString *textLabel = [purpose description];
cell.textLabel.text = textLabel;
}
else{
NSArray *plans = [[OTPlanStore sharedStore]allPlans];
OTVision *plan = [plans objectAtIndex:indexPath.row];
NSString *textLabel = [plan description];
cell.textLabel.text = textLabel;
}
return cell;
}
我正在尝试填充一个 UITableView
,我把它放在 UIViewController
的下面 UISegmentedControl
,如图所示:
按下任何段后,我希望 UITableView
重新填充一组不同的数据。这是我的相关代码:
@interface OTValuesViewController ()
@property (weak, nonatomic) IBOutlet UITableView *tableView;
@property (weak, nonatomic) IBOutlet UISegmentedControl *segmentControl;
@end
@implementation OTValuesViewController
- (IBAction)segmentValueChanged:(id)sender {
UISegmentedControl *segment = self.segmentControl;
UINavigationItem *navItem = self.navigationItem;
UIBarButtonItem *addButton = navItem.rightBarButtonItem;
switch (segment.selectedSegmentIndex) {
case 0:
addButton.action = @selector(addNewVision:);
[self.tableView reloadData];
break;
case 1:
addButton.action = @selector(addNewPurpose:);
[self.tableView reloadData];
break;
case 2:
addButton.action = @selector(addNewPlan:);
[self.tableView reloadData];
break;
default:
[self.tableView reloadData];
break;
}
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UISegmentedControl *segment = self.segmentControl;
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"OTValuesTableViewCell" forIndexPath:indexPath];
NSArray *visions = [[OTVisionStore sharedStore] allVisions];
NSArray *purposes = [[OTPurposeStore sharedStore] allPurposes];
NSArray *plans = [[OTPlanStore sharedStore]allPlans];
OTVision *vision = [visions objectAtIndex:indexPath.row];
OTVision *purpose = [purposes objectAtIndex:indexPath.row];
OTVision *plan = [plans objectAtIndex:indexPath.row];
NSString *textLabel = [vision description];
if (segment.selectedSegmentIndex==0) {
textLabel = [vision description];
cell.textLabel.text = textLabel;
}
else if (segment.selectedSegmentIndex==1) {
textLabel = [purpose description];
cell.textLabel.text = textLabel;
}
else{
textLabel = [plan description];
cell.textLabel.text = textLabel;
}
return cell;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
UISegmentedControl *segment = self.segmentControl;
if(segment.selectedSegmentIndex==0) {
return [[[OTVisionStore sharedStore]allVisions]count];
}
else if (segment.selectedSegmentIndex==1) {
return [[[OTPurposeStore sharedStore]allPurposes]count];
}
else{
return [[[OTPlanStore sharedStore]allPlans]count];
}
}
- (void)viewDidLoad
{
[super viewDidLoad];
self.tableView.dataSource = self;
self.tableView.delegate = self;
self.title=@"Mission Statement";
UINavigationItem *navItem = self.navigationItem;
UIBarButtonItem *rbbi = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAdd
target:self
action:@selector(addNewVision:)];
navItem.rightBarButtonItem = rbbi;
}
- (void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];
[self.tableView reloadData];
}
我的问题是:当我构建和 运行 项目时,一切都很好——单元格填充在我的默认选项卡(视觉)中。当我单击 UISegmentedControl
按钮时,出现错误:
Terminating app due to uncaught exception 'NSRangeException', reason: '*** -[__NSArrayM objectAtIndex:]: index 1 beyond bounds [0 .. 0]'
如果我删除:
self.tableView.dataSource = self;
self.tableView.delegate = self;
从我的 viewDidLoad
来看,这个错误没有发生(尽管 UITableView
显然没有被填充。)这让我相信错误是由于我填充单元格引起的基于 UISegmentedController's
选定的细分,但是,我不知道具体是什么原因造成的。
如有任何帮助,我们将不胜感激!
问题出在您的 cellForRowAtIndexPath
方法中。您尝试对所有数组使用当前索引路径,而不仅仅是适用的数组。尝试这样的事情:
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UISegmentedControl *segment = self.segmentControl;
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"OTValuesTableViewCell" forIndexPath:indexPath];
if (segment.selectedSegmentIndex==0) {
NSArray *visions = [[OTVisionStore sharedStore] allVisions];
OTVision *vision = [visions objectAtIndex:indexPath.row];
NSString *textLabel = [vision description];
cell.textLabel.text = textLabel;
}
else if (segment.selectedSegmentIndex==1) {
NSArray *purposes = [[OTPurposeStore sharedStore] allPurposes];
OTVision *purpose = [purposes objectAtIndex:indexPath.row];
NSString *textLabel = [purpose description];
cell.textLabel.text = textLabel;
}
else{
NSArray *plans = [[OTPlanStore sharedStore]allPlans];
OTVision *plan = [plans objectAtIndex:indexPath.row];
NSString *textLabel = [plan description];
cell.textLabel.text = textLabel;
}
return cell;
}