我正在使用UIGridView因为2列就在那里,并且有保存图像的数组列表。现在我想比较两辆车,这样我就必须在UIGridView
中标记任意两张图像。所以我可以比较任何两辆车。我不完全了解GridView。
点击红色导航按钮时应该进行比较。
我的代码如下:
CarArray = [[NSMutableArray alloc]init];
[CarArray addObject:@"image.png"];
[CarArray addObject:@"car-image.png"];
[CarArray addObject:@"mercy.png"];
[CarArray addObject:@"lanbo.png"];
}
- (CGFloat) gridView:(UIGridView *)grid widthForColumnAt:(int)columnIndex
{
return 160;
}
- (CGFloat) gridView:(UIGridView *)grid heightForRowAt:(int)rowIndex
{
return 200;
}
- (NSInteger) numberOfColumnsOfGridView:(UIGridView *) grid
{
return 2;
}
- (NSInteger) numberOfCellsOfGridView:(UIGridView *) grid
{
return [CarArray count];
}
- (UIGridViewCell *) gridView:(UIGridView *)grid cellForRowAt:(int)rowIndex AndColumnAt:(int)columnIndex
{
Cell *cell = (Cell *)[grid dequeueReusableCell];
if (cell == nil) {
cell = [[Cell alloc] init];
}
cell.thumbnail.image=[UIImage imageNamed:[CarArray objectAtIndex:(rowIndex*2+columnIndex)]];
return cell;
}
- (void) gridView:(UIGridView *)grid didSelectRowAt:(int)rowIndex AndColumnAt:(int)colIndex
{
NSLog(@"%d, %d clicked", rowIndex, colIndex);
CarDetailViewController *servicesViewController_obj=[[CarDetailViewController alloc]initWithNibName:@"CarDetailViewController" bundle:nil];
[self.navigationController pushViewController:servicesViewController_obj animated:YES];
}
我还想实现标记和取消标记,以便我可以进一步移动。
答案 0 :(得分:0)
我假设您正在使用此UIGridView示例代码。
我已根据您的要求对示例代码进行了一些更改。请按照步骤更改网格视图中的图像: -
1.在 UIGridViewDelegate.h中: -
更改可选方法
- (void) gridView:(UIGridView *)grid didSelectRowAt:(int)rowIndex AndColumnAt:(int)columnIndex;
到
- (void) gridView:(UIGridView *)grid didSelectRowAt:(int)rowIndex AndColumnAt:(int)columnIndex gridViewCell:(UIGridViewCell *)gridViewCell;
2.在 UIGridView.m中: -
更改此代码
- (IBAction) cellPressed:(id) sender
{
UIGridViewCell *cell = (UIGridViewCell *) sender;
[uiGridViewDelegate gridView:self didSelectRowAt:cell.rowIndex AndColumnAt:cell.colIndex];
}
与
- (IBAction) cellPressed:(id) sender
{
UIGridViewCell *cell = (UIGridViewCell *) sender;
[uiGridViewDelegate gridView:self didSelectRowAt:cell.rowIndex AndColumnAt:cell.colIndex gridViewCell:cell];
}
3.在 RootViewController.m中: -
添加此代码
- (void) gridView:(UIGridView *)grid didSelectRowAt:(int)rowIndex AndColumnAt:(int)colIndex gridViewCell:(UIGridViewCell *)gridViewCell
{
NSLog(@"%d, %d clicked", rowIndex, colIndex);
Cell *cellTemp = (Cell *)gridViewCell;
UIImage *imgT = cellTemp.thumbnail.image;
// Change the image names according to your images
if (imgT == [UIImage imageNamed:@"default_picture.png"]) // If first image is there
{
// Replace the image with second image
cellTemp.thumbnail.image = [UIImage imageNamed:@"second.png"];
}
else
{
// Replace the image with first image
cellTemp.thumbnail.image = [UIImage imageNamed:@"default_picture.png"];
}
}
答案 1 :(得分:0)
//在某些按钮操作中调用此方法,以便您获得选定的汽车标记值
// [self getSelectedCars];还有
@interface ViewController ()
{
UIImageView *thumbnailView;
NSMutableArray *selectedThumbsArray;
NSArray *dumyimages;
}
- (void)viewDidLoad
{
[super viewDidLoad];
dumyimages=[[NSArray alloc]initWithObjects:[UIImage imageNamed:@"accept@2x.png"],[UIImage imageNamed:@"santa@2x.png"],[UIImage imageNamed:@"santa.png"],[UIImage imageNamed:@"accept.png"], nil];
selectedThumbsArray =[[NSMutableArray alloc]initWithObjects:@"0",@"0",@"0",@"0", nil];
int imagesCount =0;
int yAxis =50;
int xAxis =50;
for (int i=0; i<dumyimages.count; i++) {
thumbnailView =[[UIImageView alloc]init];
thumbnailView.frame=CGRectMake(xAxis, yAxis, 120, 120);
thumbnailView.tag=i+10;
thumbnailView.userInteractionEnabled=YES;
thumbnailView.image=dumyimages[i];
xAxis=xAxis+125;
imagesCount=imagesCount+1;
if (imagesCount==2) {
xAxis=50;
yAxis=yAxis+125;
imagesCount=0;
}
[self.view addSubview:thumbnailView];
UITapGestureRecognizer *gestureRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleLongPress:)];
gestureRecognizer.delegate = self;
gestureRecognizer.numberOfTouchesRequired = 1;
[thumbnailView addGestureRecognizer:gestureRecognizer];
}
}
- (void)handleLongPress:(UITapGestureRecognizer*)gestureRecognizer
{
NSLog(@"Gesture tag is,%i", gestureRecognizer.view.tag-10) ;
int indexofThumb =gestureRecognizer.view.tag-10;
if ([selectedThumbsArray[indexofThumb] intValue]==0 ) {
[selectedThumbsArray replaceObjectAtIndex:indexofThumb withObject:@"1"];
thumbnailView=(UIImageView*)[self.view viewWithTag:indexofThumb];
thumbnailView.image=[UIImage imageNamed:@"info.png"];
}
else
{
[selectedThumbsArray replaceObjectAtIndex:indexofThumb withObject:@"0"];
thumbnailView=(UIImageView*)[self.view viewWithTag:indexofThumb];
thumbnailView.image=dumyimages[indexofThumb];
}
}
- (void)getSelectedCars
{
for (int i=0; i<selectedThumbsArray.count; i++) {
if ([selectedThumbsArray[i] isEqualToString:@"1"]) {
NSLog(@"indexes are ------>%d",i); // here list of index of selected images will come you can use accordingly
}
}
}
答案 2 :(得分:0)
如果您想使用当前代码,“网格视图”表示
获取图像视图的索引
- (void) gridView:(UIGridView *)grid didSelectRowAt:(int)rowIndex AndColumnAt:(int)colIndex
{
NSLog(@"%d, %d clicked", rowIndex, colIndex);
int index =(1*rowIndex)+rowIndex+colIndex; // here 1 is the max no of columns-1.
NSLog(@"arrayindex is----->%d",index);
}