ios - 如何使用循环在行中仅显示 3 个按钮,在列中仅显示一个按钮

标签 ios objective-c iphone

我想在行中显示 3 个按钮,在列中只显示一个按钮我已经做到了,但只有 3 个按钮应该在一行中。如何以适当的方式管理它。我只想以矩阵形式动态显示按钮。这个过程的正确循环是什么。请解决这个问题。

//这是我的代码

//  DPSGalleryViewController.m
//  DPSApp
//
//  Created by EDUNEXT on 18/05/15.
//  Copyright (c) 2015 Edunext. All rights reserved.
//

#import "DPSGalleryViewController.h"
#import "DPSAppParser.h"
#import "DPSAppService.h"
#import "SharedPreferenceUtil.h"
#import "DPSGallery1ViewController.h"
#import "UIImageView+WebCache.h"
@interface DPSGalleryViewController ()
{
    NSMutableArray *imagearray;
    UIImageView *groupimageView;
  //  NSMutableArray *array;
   // UIButton *buttons;
    NSString *filepath1;
    }


@end

@implementation DPSGalleryViewController
@synthesize sv;
-(IBAction)firstview:(id)sender
{


    UIButton *aButton = [[UIButton alloc]init];//[[UIButton buttonWithType:UIButtonTypeCustom]initWithFrame:CGRectMake(120, 60, 80, 60)];
    aButton.backgroundColor=[UIColor clearColor];
    [self.view addSubview:aButton];
    DPSGallery1ViewController *galleryViewController =[self.storyboard instantiateViewControllerWithIdentifier:@"gallery1view"];
    [self.navigationController pushViewController:galleryViewController animated:YES];


}


- (void)viewDidLoad
{
    //set frame
    sv.frame =CGRectMake(0, 0, 320, 480);
    // set content size
    [sv setContentSize:CGSizeMake(320, 1000)];
    [super viewDidLoad];

    // Do any additional setup after loading the view.

    NSInteger intTagNumber  = 0;
    NSInteger intLeftMargin = 10; // horizontal offset from the edge of the screen
    NSInteger intTopMargin  = 20; // vertical offset from the edge of the screen
    NSInteger intXSpacing   = 85; // number of pixels between the button origins (horizontally)
    NSInteger intYSpacing   = 79; // number of pixels between the button origins (vertically)
    NSInteger intXTile;
    NSInteger intYTile;
    int k =1;

       for (int y = 1; y <1+k; y++)
    {
        for (int x = y; x <=4-k; x++)
        {

            intXTile = (x * intXSpacing) + intLeftMargin;
            intYTile = (y * intYSpacing) + intTopMargin;

            // create a value button, text, or image
            buttons[x][y] = [[UIButton alloc] initWithFrame:CGRectMake(intXTile, intYTile, 58, 58)];
            [buttons[x][y] setBackgroundImage:[UIImage imageNamed:@"gallery_back.png"] forState:UIControlStateNormal];
     //      [buttons[x][y] addTarget:self action:@selector(actionPick:) forControlEvents:UIControlEventTouchDown];
           [buttons[x][y] setTitleColor:[UIColor colorWithRed:0/255.0 green:0/255.0 blue:92/255.0 alpha:1] forState:UIControlStateNormal];
         //  buttons[x][y].titleLabel.font = [UIFont fontWithName:@"TrebuchetMS-Bold" size:42];
            buttons[x][y].adjustsImageWhenHighlighted = NO;
            buttons[x][y].adjustsImageWhenDisabled = NO;
            buttons[x][y].tag = intTagNumber;
            [self.view addSubview:buttons[x][y]];

            intTagNumber++;
        }
       k++;

    }



}
- (IBAction)actionPick:(id)sender
{
    UIButton *button = (UIButton *)sender;
    NSLog(@"Button tag: %i",button.tag);
}

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

-(void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:YES];
    [self updateUI];
    [self addBottomView];
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

-(NSString *)getURL
{
    return [NSString stringWithFormat:@"%@%@studentprofileid=%@",ROOT_URL,ImageGallery_Data,[[[SharedPreferenceUtil getNSObject:STUDENT_DATA_JSON ]valueForKey:@"studentprofileid"] valueForKey:@"studentimagegallary"]];
}


-(void)updateUI
{
    [self addCutomTitleToNavigationController:@"Image Gallery"];
    [self addCustomBackButton];

    if (IS_IOS7)
        [self.navigationController.navigationBar setBackgroundImage:[UIImage imageNamed:@"Gallery_navigation2.png"] forBarMetrics:UIBarMetricsDefault];
    else
        [self.navigationController.navigationBar setBackgroundImage:[UIImage imageNamed:@"Gallery_navigation.png"] forBarMetrics:UIBarMetricsDefault];








  //  NSMutableArray *datainview =(NSMutableArray*) [SharedPreferenceUtil getNSObject:@"gallary_data"];
 //   NSString *groupname1=[datainview valueForKey:@"group_name"];
//    NSLog(@"The group is : %@",groupname1);
//    NSLog(@"The data in  View is : %@",datainview);
//    [march2015label setText:groupname1];



}




@end

屏幕截图在这里//this is not a correct form, i just want from the top of screen 3 button in a row only and one in column only

最佳答案

最好将 UITableView 与 n 行一起使用,并为每个单元格创建一个按钮循环,并将其添加为单元格的内容 View (作为列)。请按以下方式设置按钮标签 -

  - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return 4;
}

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

    static NSString *CellIdentifier = @"Cell";

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

    if (cell == nil) {

        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] ;

    }


    for (int columnIndex = 0; columnIndex <4; columnIndex++)
    {
        UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
        [button addTarget:self
                   action:@selector(aMethod:)
         forControlEvents:UIControlEventTouchUpInside];
        button.tag = indexPath.row + columnIndex;
        [button setTitle:@"Show View" forState:UIControlStateNormal];
        button.frame = CGRectMake(0.0,columnIndex * 40.0 + 10.0, 40.0, 40.0);
        [cell. contentView addSubview:button];

    }


    return cell;

}

更新

-(void)aMethod:(UIButton *)sender
    {

        int tagValue = sender.tag;

        int columnIndex = tagValue%4 ;
        int rowIndex = (tagValue - columnIndex)/4;

        NSLog(@"Button clicked Row %d Column %d",rowIndex,columnIndex);
    }

关于ios - 如何使用循环在行中仅显示 3 个按钮,在列中仅显示一个按钮,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30391018/

相关文章:

ios - 由于未捕获的异常 'NSGenericException' 而终止应用程序,原因 : '*** Collection <__NSSetM...> was mutated while being enumerated.'

objective-c - 如何增加 NSIndexPath

ios - 将 AFNetworking 添加到新项目时出现错误

iphone - 进行界面定位时图像弹跳

Xcode 6.3 不支持 iOS 9

ios - 在 iOS 上,如果我使用他们的地点自动完成查询,我是否必须显示 google Logo

objective-c - 如何在 UIScrollView 的上下文中找到 UIView 的框架?

ios - 无法构建 Objective-C 模块 'zipzap'

java - Objective-C main 方法中的数组。 Sigserve 错误。纽布

iphone - 游戏中心排行榜显示计数,不输入分数