2017-01-11 23 views
0

SegmentedControlを使用して複数のCustomCellに小さな問題があります。私は(Admin、Engineer、Doctor、Employee)のような4つのsegmentedcontrolsを持つフレームワークHMSegmentedControlをインポートしました。今問題は、(AdminCellList、EngineerCellList、DoctorCellList、EmployeeListCell)のような複数のカスタムセルを使用する方法です。 Admin SegmentedControlをクリックすると、AdminCellListなどが読み込まれます。以下は私が試みたものです。 TIA。UISegmentedControlで複数のCustomCellを使用する方法Objective C

1.Reloadテーブル、ユーザがセグメントを変更するたび:

MYViewController.h 

@interface MYViewController : UIViewController 
{ 
NSUInteger currentScreen; 
} 

MYViewController.m 

self.AdminView.hidden = NO; 
    self.EngineerView.hidden = YES; 
    self.DoctorView.hidden = YES; 
    self.EmployeeView.hidden = YES; 


    currentScreen=0; 
    [self.scrollView addSubview:self.AdminView]; 

self.edgesForExtendedLayout = UIRectEdgeNone; 


    CGFloat viewWidth = CGRectGetWidth(self.view.frame); 

    HMSegmentedControl *segmentedControl = [[HMSegmentedControl alloc] initWithSectionTitles:@[@"Admin", @"Engineer", @"Doctor", @"Employee"]]; 
    segmentedControl.frame = CGRectMake(0, 0, viewWidth, 45); 
    segmentedControl.selectionStyle = HMSegmentedControlSelectionStyleFullWidthStripe; 

    segmentedControl.backgroundColor = [UIColor colorWithRed:170/255.0 green:170/255.0 blue:170/255.0 alpha:1.0]; 

    segmentedControl.selectionIndicatorColor = [UIColor blackColor]; 
    segmentedControl.selectionIndicatorHeight = 2.0f; 
    segmentedControl.verticalDividerEnabled = YES; 
    segmentedControl.verticalDividerColor = [UIColor blackColor]; 
    segmentedControl.verticalDividerWidth = 1.0f; 
    segmentedControl.borderColor = [UIColor blackColor]; 


    [segmentedControl setTitleFormatter:^NSAttributedString *(HMSegmentedControl *segmentedControl, NSString *title, NSUInteger index, BOOL selected) { 
     NSAttributedString *attString = [[NSAttributedString alloc] initWithString:title attributes:@{NSForegroundColorAttributeName : [UIColor whiteColor]}]; 
     return attString; 
    }]; 

    [segmentedControl addTarget:self action:@selector(segmentedControlChangedValue:) forControlEvents:UIControlEventValueChanged]; 
    [self.view addSubview:segmentedControl]; 
} 


- (void)segmentedControlChangedValue:(UISegmentedControl *)segment 
{ 

    if(segment.selectedSegmentIndex == 0) { 
     currentScreen=0; 

     //Loading Service 
    } 
    else if(segment.selectedSegmentIndex == 1) { 
     currentScreen=1; 

     //Loading Service 
    } 
    else if(segment.selectedSegmentIndex == 2) { 
     currentScreen=2; 

     //Loading Service 
    } 
    else if(segment.selectedSegmentIndex == 3) { 
     currentScreen=3; 

     //Loading Service 
    } 

} 


- (void)uisegmentedControlChangedValue:(UISegmentedControl *)segmentedControl { 
    NSLog(@"Selected index %ld", (long)segmentedControl.selectedSegmentIndex); 

} 


#pragma mark - 
#pragma mark Table view data source 

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView { 
    // Return the number of sections. 
    return 1; 
} 
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { 
return 10; 
} 
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { 

    static NSString *CellIdentifier = @"Cell"; 

    AdminCellList *cell = (AdminCellList *) [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 

    if (cell == nil) { 

     NSArray *topLevelObjects = [[NSBundle mainBundle] loadNibNamed:@"AdminCellList" owner:self options:nil]; 

     for (id currentObject in topLevelObjects){ 
      if ([currentObject isKindOfClass:[UITableViewCell class]]){ 
       cell = (AdminCellList *) currentObject; 
       cell.selectionStyle=UITableViewCellSelectionStyleNone; 

} 
+0

あなたの 'cellForRowAtIndexPath'コードは不完全です。 –

+0

複数のカスタムセルを取得する方法を理解できません。だから私は1つのカスタムセルを書いた – Abhimanyu

+0

あなたは以下のコードを試しましたか? –

答えて

0

すべてがちょうどいくつかの追加を必要とする、よさそうです。

currentScreenに従ってcellForRowAtIndexPathをチェックし、対応するセルをカスタムクラスでロードします。

- (void)segmentedControlChangedValue:(UISegmentedControl *)segment { 

    if(segment.selectedSegmentIndex == 0) { 
     currentScreen=0; 
    } 
    else if(segment.selectedSegmentIndex == 1) { 
     currentScreen=1; 
    } 
    ....... more code 

    [myTableView reloadData]; 
    } 

    - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { 

     static NSString *CellIdentifier = @"Cell"; 

     if (currentScreen == 0) { 
      AdminCellList *cell = (AdminCellList *) [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 

      if (cell == nil) { 

       NSArray *topLevelObjects = [[NSBundle mainBundle] loadNibNamed:@"AdminCellList" owner:self options:nil]; 

       for (id currentObject in topLevelObjects){ 
        if ([currentObject isKindOfClass:[UITableViewCell class]]){ 
         cell = (AdminCellList *) currentObject; 
         cell.selectionStyle=UITableViewCellSelectionStyleNone; 

        } 
       } 
      } 
     } else if (currentScreen == 1) { 
      //Same code for cell as AdminCellList 
      EngineerCellList *cell = (EngineerCellList *) [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 

      if (cell == nil) { 

       NSArray *topLevelObjects = [[NSBundle mainBundle] loadNibNamed:@"EngineerCellList" owner:self options:nil]; 

       for (id currentObject in topLevelObjects){ 
        if ([currentObject isKindOfClass:[UITableViewCell class]]){ 
         cell = (EngineerCellList *) currentObject; 
         cell.selectionStyle=UITableViewCellSelectionStyleNone; 

        } 
       } 
      } 
     } else if (currentScreen == 2) { 
      //Similar custom cell code here as above 
     } else { 
      //Similar custom cell code here as above 
     } 
} 
関連する問題