我想在行中显示3个按钮,而在列i中只有一个按钮已完成,但只有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
屏幕截图位于//
答案 0 :(得分:2)
最好使用带有n个数字行的UITableView,并为每个单元格创建一个按钮循环,并将其添加为单元格的内容视图(作为列)。请按以下方式设置按钮标记 -
- (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);
}