【问题标题】:To display UIButtons in GridView [closed]在 GridView 中显示 UIButtons [关闭]
【发布时间】:2012-11-26 06:32:53
【问题描述】:

我有一些按钮(5 个 UIButtons)并且想将它们放置在网格视图中的每一行中,我想放置 3 个按钮。

我该如何放置它们,知道吗?

-(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;

}
}

【问题讨论】:

  • 请展示你的努力
  • 但它不工作我知道我错过了逻辑如果找到指导我谢谢
  • 您想只显示 5 个按钮还是超过 5 个按钮?因为如果超过 5 个按钮,则创建带有 3 个按钮的 customTableCell,将按钮数组的数量作为数据源提供给 TableView

标签: iphone objective-c ios5 uibutton


【解决方案1】:

创建一个全局按钮计数,int totalButton=5;

并且使用tableView来展示Gridview,下面的代码可以帮你解决问题,是泛化代码,你可以在Gridview中添加5个以上的按钮:

- (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 &lt numberOfRows) {
          int x=10;
          for(int i=0; i&lt 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&lt 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;
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-05-31
    • 2015-03-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多