2015-08-18 43 views
0

我試圖填充UITableView我把UIViewController裏面,一個UISegmentedControl下,這是在畫面描繪的:填充一個UIViewController內的UITableView和改變數據來源與UISegmentedControl

enter image description here

在按任意一個分段,我想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選定細分的細胞,但是,我不知道是什麼造成的。

任何幫助將不勝感激!

回答

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; 
} 
+0

這解決了我的問題完美!非常感謝 - 我真的在撓撓我的頭腦。 – ecatalano