Have you considered creating a subclass of UIView for each of your cells. Since you are creating a minesweeper game I believe each of your cells would need to have the following functions:
- Have 2 states - opened and closed
- Respond to touches - for flagging or opening
- Have a label - representing number of mines
- Have an image - if it is a mine or flag
- Ability to change rectangle color
All of this can be achieved by the UIView method. For example your exampleMine.h:
#import <UIKit/UIKit.h>
@interface exampleMine :UIView{
UIImageView *backgroundImage; //used for either flags or mines
UIButton* cellButton; //used for flagging or opening actions
UILabel* numMineLabel;
BOOL isMine, isOpen, isFlagged;
int numSurroundingMines;
}
And in your exampleMine.m have the following functions
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if(self){
backgroundImage = [[UIImageView alloc] initWithFrame: self.bounds];
backgroundImage.hidden = YES; //You don't need this image to be shown at the start
backgroundImage.backgroundColor = [UIColor clearColor];
numMineLabel = [[UILabel alloc] initWithFrame: self.bounds];
numMineLabel.hidden = YES; //You don't need this label at the start either
numMineLabel.textAlignment = NSTextAlignmentCenter;
numMineLabel.backgroundColor = [UIColor clearColor];
cellButton = [UIButton buttonWithType: UIButtonTypeCustom];//you can add a target and selector for touch responses in your main view
cellButton.frame = self.bounds;
cellButton.backgroundColor = [UIColor clearColor];
cellButton.enabled = YES;
cellButton.hidden = NO:
[self addSubview: backgroundImage];
[self addSubview: numMineLabel];
[self addSubview: cellButton];
self.backgroundColor = [UIColor blueColor];//I've set blueColor as default for closed cell - you can choose any color
isMine = isOpen = isFlagged = NO;
}
return self;
}
This is to initialize a new cell. The following functions would help set up the rest of the details.
- (void) setMine{ //Called only if given cell is a mine
isMine = YES;
}
- (void) setNumSurroundingMines: (int) numSurrMines{
numSurroundingMines = numSurrMines;
numMineLabel.text = [NSString stringWithFormat: @"%d", numSurroundingMines];
}
- (void) setOpen{
if(isMine){
backgroundImage.image = [UIImage imageNamed: @"mine.png"]; //or whichever image you would use
backgroundImage.hidden = NO;
self.backgroundColor = [UIColor redColor];
}
else{
numMineLabel.hidden = NO;
}
cellButton.hidden = YES; //You would not need the button any longer
isOpen = YES;
}
- (void) setFlagged{
if(isFlagged){
backgroundImage.hidden = YES;
}
else{
backgroundImage.image = [UIImage imageNamed: @"flags.png"]; //or whichever image you would use
backgroundImage.hidden = NO;
}
}
I realise this is a long answer, and it may not directly answer your question. However, it might be an alternative approach to solving your problem. I have tried this approach once using java swing, and it works very well, and makes event handling very easy.