【发布时间】:2015-08-19 00:12:43
【问题描述】:
我正在尝试填充一个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 按钮时,我收到错误消息:
由于未捕获的异常“NSRangeException”而终止应用程序,原因:“*** -[__NSArrayM objectAtIndex:]: index 1 beyond bounds [0 .. 0]”
如果我删除:
self.tableView.dataSource = self;
self.tableView.delegate = self;
从我的viewDidLoad 来看,不会发生此错误(尽管 UITableView 显然没有被填充。)这使我相信该错误是由于我根据 UISegmentedController's 填充单元格而产生的选定的段,但是,我不知道具体是什么原因造成的。
任何帮助将不胜感激!
【问题讨论】:
标签: ios objective-c uitableview uisegmentedcontrol