我正在尝试填充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
重新填充一组不同的数据。这是我的相关代码:
self.tableView.dataSource = self;
self.tableView.delegate = self;
我的问题是:当我构建并运行项目时,一切都很好 - 单元格填充在我的默认选项卡(视觉)中。当我点击viewDidLoad
按钮时,出现错误:
由于未捕获的异常'NSRangeException'而终止应用程序,原因:'*** - [__ NSArrayM objectAtIndex:]:索引1超出边界[0 .. 0]'
如果我删除:
UITableView
来自我的UISegmentedController's
,此错误不会发生(但{{1}}显然没有填充。)这让我相信错误来自于我填充基于的单元格但是,{{1}}选定的细分受众群,我不知道具体是什么造成的。
非常感谢任何帮助!
答案 0 :(得分:1)
问题出在您的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;
}