2012-11-26 2 views
0

J'ai un certain nombre de boutons (5 UIButtons) et je veux les placer en grille dans chaque rangée je veux placer 3 boutons.Pour afficher les UIButtons dans GridView

Comment puis-je les placer, Une idée?

-(void)createSubMenusforView:(UIView *)selectedView 
{ 
int y=75; 

for(int i=0;i<=([arr count]-1)/3;i++) 
{ 
    int si=(selectedView.frame.size.width/3-50)/2; 

    int x=si+si/2; 

    for(int j=0;j<3;j++) 
    { 
     UIButton *btn_item=[[UIButton alloc]initWithFrame:CGRectMake(x,y,50,50)]; 
     btn_item.backgroundColor=[UIColor grayColor]; 
     [selectedView addSubview:btn_item]; 

     [self doAnimateforView:btn_item forFrame:CGRectMake(btn_item.frame.origin.x, btn_item.frame.origin.y, btn_item.frame.size.width, btn_item.frame.size.height) forDelay:0.5]; 

     x=x+75; 

    } 

    y=y+60; 

} 
} 
+0

montrent vos efforts en question, s'il vous plaît –

+0

Mais je ne sais Son travail me manquait logique si trouvé me guider grâce – krishh

+0

vous voulez afficher seulement 5 boutons ou plus de 5 boutons? parce que si c'est plus de 5 boutons alors créez customTableCell avec 3 boutons, donnez le nombre de boutons array comme datasource à TableView –

Répondre

1

Créer un compte de bouton global, int totalButton=5;

Et utilisé tableView pour montrer Gridview, le code ci-dessous vous aidera à résoudre votre problème, il est généraliser le code, vous pouvez ajouter plus de 5 bouton Gridview:

- (void)viewDidLoad 
{ 
    [super viewDidLoad]; 
    // Do any additional setup after loading the view, typically from a nib. 

    // add tableView on your View or thr xib 
     tableView.delegate=self; 
     tableView.dataSource=self;     
     [self.view addSubView:tableview]; 

} 

#pragma mark 
#pragma mark UITableViewDataSource 

//@required 

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { 

    int numberOfRows=totalButton/3; 
    if(totalButton%3) 
     numberOfRows++; 
    return numberOfRows; 
} 

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

    // You can create custom cell with xib and drag drop 3 buttons on it and handle the logic to show total buttons on tableview 
    // Here I am adding buttons as subView to cell 
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"UITableViewCell"]; 
    if (cell == nil) { 
     // No cell to reuse => create a new one 
     cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"UITableViewCell"] autorelease]; 

     // Initialize cell 
     int numberOfRows=totalButton/3; 
     if(totalButton%3) 
      numberOfRows++; 

     if(indexPath.row < numberOfRows) { 
      int x=10; 
      for(int i=0; i< 3;i++) { 

       UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect]; 
       //[button addTarget:self action:@selector(aMethod:) forControlEvents:UIControlEventTouchDown]; 
       [button.titleLabel setText:@"Button"]; 
       button.tag=i+1; 
       button.frame = CGRectMake(x, 10.0, 40.0, 40.0); 
       [cell addSubview:button]; 
       x+=70; 
      } 
     } 
     else { 

       int x=10; 
      for(int i=0; i< totalButton%3;i++) { 

       UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect]; 
       //[button addTarget:self action:@selector(aMethod:) forControlEvents:UIControlEventTouchDown]; 
       [button.titleLabel setText:@"Button"]; 
       button.tag=i+1; 
       button.frame = CGRectMake(x, 10.0, 40.0, 40.0); 
       [cell addSubview:button]; 
       x+=70; 
      } 
     } 
    } 

    // Customize cell 

    return cell; 
}
Questions connexes