2011-05-24 5 views
7

J'aimerais faire 2 segments, quelque chose comme çaComment utiliser UISegmentedControl avec UITableView

enter image description here

le segment deparature affiche la deparature voler dans un tableView et le segment de retour de la mouche de retour. Est-ce que somene peut m'expliquer comment je devrais faire ça? Dois-je faire 2 tableView ou juste un? merci

Répondre

17

Vous pouvez utiliser un UITableView à cette fin et recharger les données de table sur segmentcontrolindexchange method.Look A Code

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView { 
    // Return the number of sections. 
    return 1; 
} 

-(NSInteger)tableView:(UITableView *)table numberOfRowsInSection:(NSInteger)section 
{ if(segment.selectedSegmentIndex==0) 
{ 
    return [List count]; 
} 
    else 
     if (segment.selectedSegmentIndex==1) { 
      return[List1 count]; 

     } 

    return 0; 
} 

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

    static NSString *CellIdentifier = @"Cell"; 

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 
      cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease]; 



    lbl1 = [[UILabel alloc]initWithFrame:CGRectMake(200, 10, 100, 20) ]; 

    // Configure the cell... 
lbl =[[UILabel alloc]initWithFrame:CGRectMake(100, 10, 100, 20) ]; 
    if(segment.selectedSegmentIndex==0) 
    { 
    cell.textLabel.text=[List objectAtIndex:indexPath.row]; 

     lbl.text = [List3 objectAtIndex:indexPath.row]; 
     [cell.contentView addSubview:lbl]; 

     lbl1.text = [List objectAtIndex:indexPath.row]; 
     [cell.contentView addSubview:lbl1]; 
    } 
    else if(segment.selectedSegmentIndex==1) { 
     cell.textLabel.text=[List1 objectAtIndex:indexPath.row]; 

     lbl.text = [List objectAtIndex:indexPath.row]; 
     [cell.contentView addSubview:lbl]; 
    } 


    return cell; 
} 




-(IBAction) segmentedControlIndexChanged 
{ 
    switch (self.segment.selectedSegmentIndex) { 
     case 0: 
      i=0; 
      [table reloadData]; 
      break; 
     case 1: 
      i=1; 

      [table reloadData]; 
     default: 
      break; 
    } 



} 
+0

C'est de quoi je parle. merci beaucoup – user567

3

Vous pouvez le faire de toute façon ... un UITableView vous obligerait à changer la source de données et lorsque le contrôle de segmentation change. alternativement et de préférence vous auriez 2 UITableView avec leurs propres contrôleurs et simple basculer la visibilité d'entre eux avec le contrôle de segmentation ..

+0

Merci .Je suis désolé, mais je suis tellement nouveau dans ce domain.I devrait créer un .m deparaturetableviewcontroller.h et .xib et comebacktableviewcontroller.h .m et .xib? parce que vous dites "avec leurs propres contrôleurs" – user567

+0

Vous n'avez pas besoin de séparer les xib pour la vue, il suffit de connecter les UITableView à leurs contrôleurs respectés, (Delegate et Datasource) –

0

Une autre approche qui suivrait les lignes directrices de conception d'Apple serait de remplacer le contrôle de segmentation avec un contrôle de barre d'outils, Vous pouvez ensuite utiliser la construction dans UIToolBarController pour gérer la pile de l'interface utilisateur et avoir des vues distinctes pour chaque état.

+0

Merci, je surco la méthode la plus simple. une date et quand il cloche sur le retour, je dois vérifier la date qu'il a choisi et dans la deuxième vue de table je dois montrer les prix pour ce jour de déparat ... keydate ":" 2011052820110530 "s'il a choisi 20110528 je dois montrer le prix pour cette date ... j'espère que tu comprends ... Alors, quelle méthode me proposez-vous? le plus facile? UItoolbar ou UIsegment? – user567

+0

J'utiliserais la construction dans UITabBarController, cela vous donnerait les visuels dont vous avez besoin (barre d'outils en bas de l'écran) et vous permettriez de concevoir le retournement des vues dans Interface Builder. –

+0

Voir http://developer.apple.com/fr/library/ios/#documentation/uikit/reference/UITabBarController_Class/Reference/Reference.html –

0
My case was also same (UISegment Control and UITableView with two prototype cells. 


    class DashBoardViewController: UIViewController,UITableViewDataSource { 

     @IBOutlet weak var dashBoardSegment: UISegmentedControl! 
     @IBOutlet weak var dashBoardTableView: UITableView! 


     //TableView Variables 
     var CellIdentifier: String = "dashBoardTableReportsCellID" 
     var cell:UITableViewCell? 
    var rowCount:Int? 


     // MARK: - Table view data source 
     func numberOfSectionsInTableView(tableView: UITableView) -> Int { 
      return 1 
     } 
     func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int { 

      switch CellIdentifier 
      { 
      case "dashBoardTableCellID": 
       rowCount = 2 

      case "dashBoardTableReportsCellID": 
       rowCount = 4 
      default: 
       break; 
      } 

      return rowCount! 
     } 
     func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { 

      switch CellIdentifier 
      { 
      case "dashBoardTableCellID": 
       cell = tableView.dequeueReusableCellWithIdentifier(CellIdentifier)! 
       print("CELL 1") 

      case "dashBoardTableReportsCellID": 
       cell = tableView.dequeueReusableCellWithIdentifier(CellIdentifier)! 
       print("CELL 2") 
      default: 
       break; 
      } 
      return cell! 
     } 


// OR 
    func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { 

     if (CellIdentifier == "dashBoardTableCellID") 
     { 
      let surveyCell = tableView.dequeueReusableCellWithIdentifier(CellIdentifier)! as! DashBoardTableViewCell 
      surveyCell.itemDetailLabel.text = "Survey Cell Title"    

      return surveyCell 
     } 
     else 
     { 
      let reportsCell = tableView.dequeueReusableCellWithIdentifier(CellIdentifier)! as! DashBoardTableViewReportsCell 


      return reportsCell 
     } 
     } 


    //Mark:- Segment Control Action 
     @IBAction func dashBoardSegmentValueChanged(sender: AnyObject) { 
      switch dashBoardSegment.selectedSegmentIndex 
      { 
      case 0: 
       CellIdentifier = "dashBoardTableCellID" 
       self.dashBoardTableView.reloadData() 

      case 1: 
       CellIdentifier = "dashBoardTableReportsCellID" 
       self.dashBoardTableView.reloadData() 
      default: 
       break; 
      } 

     } 
    } 
0
//UISegmentedControl with TableViewController 

//complete working code 

@interface TabTwoScheduleViewController() <UITableViewDelegate , UITableViewDataSource> 
{ 
    CGRect rect; 
    NSArray *list; 
    NSArray *list1; 
    NSArray *list2; 
    NSArray *list3; 
    NSArray *list4; 
    UITableView *segmentTableView; 
} 
@end 

@implementation TabTwoScheduleViewController 

- (void)viewDidLoad { 
[super viewDidLoad]; 

rect = [[UIScreen mainScreen]bounds]; 

UIScrollView *scroll = [[UIScrollView alloc] initWithFrame:CGRectMake(10, 10, rect.size.width-20, rect.size.height/10-23)]; 
scroll.contentSize = CGSizeMake(rect.size.width, rect.size.height * 2); 
scroll.showsHorizontalScrollIndicator = YES; 
scroll.backgroundColor = [UIColor yellowColor]; 

NSArray *itemArray = [NSArray arrayWithObjects: @"ONLINE", @"CLASSROOM", @"WEBCASTING", nil]; 
list = [NSArray arrayWithObjects:@"list.1",@"list.2",@"list.3",@"list.4",@"list.5", nil]; 
list1 = [NSArray arrayWithObjects:@"list1.1",@"list1.2",@"list1.3",@"list1.4",@"list1.5", nil]; 
list2 = [NSArray arrayWithObjects:@"list2.1",@"list2.2",@"list2.3",@"list2.4",@"list2.5", nil]; 
list3 = [NSArray arrayWithObjects:@"list3.1",@"list3.2",@"list3.3",@"list3.4",@"list3.5", nil]; 
list4 = [NSArray arrayWithObjects:@"list4.1",@"list4.2",@"list4.3",@"list4.4",@"list4.5", nil]; 

// segmentedControl is declared as property 
self.segmentedControl = [[UISegmentedControl alloc] initWithItems:itemArray]; 
self.segmentedControl.frame = CGRectMake(0, 0, rect.size.width-20, 50); 
self.segmentedControl.segmentedControlStyle =UISegmentedControlStylePlain; 
[self.segmentedControl addTarget:self action:@selector(MySegmentControlAction:) forControlEvents: UIControlEventValueChanged]; 
self.segmentedControl.selectedSegmentIndex = 0; 
[scroll addSubview:self.segmentedControl]; 
[self.view addSubview:scroll]; 

//ADDING TABLEVIEW OVER VIEW(I added this view to get leading and trailing space for tableViewCell) 

UIView *vw = [[UIView alloc]initWithFrame:CGRectMake(0, 70, rect.size.width, rect.size.height)]; 
vw.backgroundColor = [UIColor redColor]; 
[self.view addSubview:vw]; 

//TABLE VIEW 
segmentTableView = [[UITableView alloc]initWithFrame:CGRectMake(10, 0, rect.size.width-20, rect.size.height-230) style:UITableViewStylePlain]; 
segmentTableView.backgroundColor = [UIColor yellowColor]; 

segmentTableView.delegate = self; 
segmentTableView.dataSource = self; 

[vw addSubview:segmentTableView]; 
} 

// number of sections for tableView 
- (NSInteger)numberOfSectionsInTableView:(UITableView *)theTableView 
{ 
NSInteger a=0; 
switch (self.segmentedControl.selectedSegmentIndex) 
{ 
    case 0: 
     a=list.count; 
     break; 

    case 1: 
     a=list1.count; 
     break; 

case 2: 
     a=list1.count; 
     break; 

    default: 
     a=list1.count; 
     break; 
} 
return a; 
} 
// number of row in the section 
- (NSInteger)tableView:(UITableView *)theTableView numberOfRowsInSection:(NSInteger)section 
{ 
return 1; 

}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
static NSString *simpleTableIdentifier = @"ScheduleCustomTableViewCell"; 

//ScheduleCustomTableViewCell is name of custom TabelViewCell 
ScheduleCustomTableViewCell *cell =(ScheduleCustomTableViewCell *) [tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier]; 
if (cell == nil) 
{ 
    NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"ScheduleCustomTableViewCell" owner:self options:nil]; 
    cell = [nib objectAtIndex:0]; 
} 

// conditions to get different values on label 
if (self.segmentedControl.selectedSegmentIndex==0) 
{ 
    cell.labelAddress1.text = [list objectAtIndex:indexPath.section]; 
    cell.labelAddress2.text = [list1 objectAtIndex:indexPath.section]; 
} 
else if (self.segmentedControl.selectedSegmentIndex==1) 
{ 
    cell.labelAddress1.text = [list1 objectAtIndex:indexPath.section]; 
    cell.labelAddress2.text = [list2 objectAtIndex:indexPath.section]; 
} 
else 
{ 
    cell.labelAddress1.text = [list2 objectAtIndex:indexPath.section]; 
    cell.labelAddress2.text = [list3 objectAtIndex:indexPath.section]; 
} 
cell.backgroundColor = [UIColor yellowColor]; 

return cell ; 

}

-(CGFloat)tableView:(UITableView*)tableView heightForRowAtIndexPath:(NSIndexPath*)indexPath 
{ 
return 130; 

}

// Header is given to get spacing between TableViewCells 
-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section 
{ 
return 10; 
} 

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section 
{ 
UIView *headerView = [[UIView alloc] init]; 
headerView.backgroundColor = [UIColor redColor]; 
return headerView; 
} 

- (void)didReceiveMemoryWarning { 
[super didReceiveMemoryWarning]; 
// Dispose of any resources that can be recreated. 
} 

- (void)MySegmentControlAction:(UISegmentedControl *)segment 
{ 
    [segmentTableView reloadData]; 
} 

@end 
+0

// segmentedControl est déclaré comme propriété self.segmentedControl = [[UISegmentedControl alloc] initWithItems: itemArray]; self.segmentedControl.frame = CGRectMake (0, 0, rect.size.width-20, 50); Jetez un oeil .. self.segmentedControl.segmentedControlStyle = UISegmentedControlStylePlain; [self.segmentedControl addTarget: action de soi: @selector (MySegmentControlAction :) forControlEvents: UIControlEventValueChanged]; self.segmentedControl.selectedSegmentIndex = 0; [défiler addSubview: self.segmentedControl]; [self.view addSubview: scroll]; –

Questions connexes