我試圖填充UITableView
我把UIViewController
裏面,一個UISegmentedControl
下,這是在畫面描繪的:填充一個UIViewController內的UITableView和改變數據來源與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;
[email protected]"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
選定細分的細胞,但是,我不知道是什麼造成的。
任何幫助將不勝感激!
這解決了我的問題完美!非常感謝 - 我真的在撓撓我的頭腦。 – ecatalano