issues creating a class that handles contacts - cocos2d-iphone

All along I have been handling contacts within the CCPhysicsSprite class since I mainly set the userdata to a CCPhysicsSprite. As below:
ContactListenerClass.mm:
void ContactListenerClass:: BeginContact(b2Contact *contact){
b2Fixture *fixtureA = contact->GetFixtureA();
b2Fixture *fixtureB = contact->GetFixtureB();
b2Body *fixtureABody = fixtureA->GetBody();
b2Body *fixtureBBody = fixtureB->GetBody();
CCPhysicsSprite* physicsSprite = (CCPhysicsSprite*)fixtureABody->GetUserData();
CCPhysicsSprite* physicsSprite2 = (CCPhysicsSprite*)fixtureBBody->GetUserData();
[physicsSprite spritesHadContact:physicsSprite2];
}
The spritesHadContact method is in the CCPhysicsSprite.mm class:
-(void)spritesHadContact:(CCPhysicsSprite*)secondPhysicsSprite {
int tag1 = self.tag;
int tag2 = secondPhysicsSprite.tag;
if ((tag1 == 30) && (tag2 == 40 )) || ((tag1 == 40) && (tag2 == 30)) {
CCLOG(#"proceed to handle contact");
}
The above example is a bit straight forward. But when I started creating and destroying joints I realized it would be better to have a class other than the CCPhysicsSprite that does handles contacts. I made a class (contactHandler) that does that, but it's not working. I don't know what I've done wrong. Please see below:
ContactHandler.h:
#import "CCPhysicsSprite.h"
#interface ContactHandler : CCPhysicsSprite {
}
-(void)spritesHadContact:(CCPhysicsSprite*)firstPhysicsSprite otherSprite:(CCPhysicsSprite*)secondPhysicsSprite;
#end
ContactHandler.mm:
#import "ContactHandler.h"
#implementation ContactHandler
-(void)spritesHadContact:(CCPhysicsSprite*)firstPhysicsSprite otherSprite:(CCPhysicsSprite*)secondPhysicsSprite {
int tag1 = firstPhysicsSprite.tag;
int tag2 = secondPhysicsSprite.tag;
if ((tag1 == 30) && (tag2 == 40 )) || ((tag1 == 40) && (tag2 == 30)) {
CCLOG(#"proceed to handle contact");
}
#end
ContactListenerClass.h:
#import <Foundation/Foundation.h>
#import "cocos2d.h"
#import "Box2D.h"
#import "ContactHandler.h"
class ContactListenerClass : public b2ContactListener {
public:
ContactHandler* contactHandler;
void BeginContact(b2Contact* contact);
};
ContactListenerClass.mm:
#import "ContactListenerClass.h"
#import "HelloWorldLayer.h"
void ContactListenerClass:: BeginContact(b2Contact *contact)
{
b2Fixture *fixtureA = contact->GetFixtureA();
b2Fixture *fixtureB = contact->GetFixtureB();
b2Body *fixtureABody = fixtureA->GetBody();
b2Body *fixtureBBody = fixtureB->GetBody();
CCPhysicsSprite* physicsSprite = (CCPhysicsSprite*)fixtureABody->GetUserData();
CCPhysicsSprite* physicsSprite2 = (CCPhysicsSprite*)fixtureBBody->GetUserData();
[contactHandler spritesHadContact:physicsSprite otherSprite:physicsSprite2];
}
UPDATED
The spritesHadContact method in the ContactHandler class is not printing the CCLOG to the console. I tried a CCLOG to see which contacts were taking place but the method does not recognize any contacts.

Related

Cocos2d: Controller calling method of a CCNode

My GameController.m calls a method of CCNode class. It seems to call it as I can see NSLog but the method is not either operate or not result in view. In other words, screen does not change when calling the method from GameController. (method operates itself in its own class)
How should I change my code to fix it?
Summary:
GameController -> calls -> GoalPost.GoalKeeper.method.
Code:
GameController.m, GoalPost.m and GoalKeeper.m
GameController.m
#import "GameController.h"
#import "cocos2d-ui.h"
#import "GoalPost.h"
#import "Ball.h"
#import "Field.h"
#interface GameController()
#property Field* field;
//#property Ball* ball;
#property GoalPost* post;
#end
#implementation GameController
- (instancetype)init
{
self = [super init];
if (self) {
self.field = [Field node];
// self.ball = [Ball node];
self.post = [GoalPost node];
}
return self;
}
//control computer.
-(void)kickerTurn{
NSLog(#"Player kick");
//random location generation
//call keeper.
int randomKeeperX = arc4random() % (int)self.post.contentSize.width;
int randomKeeperY = arc4random() % (int)self.post.contentSize.height;
CGPoint keeperLoc = ccp(randomKeeperX, randomKeeperY);
[self.post.keeper keeperLocation:keeperLoc];
}
#end
GoalPost.h
#import "GoalKeeper.h"
#interface GoalPost : CCNodeColor { }
#property GoalKeeper* keeper;
#end
GoalPost.m
#import "GoalPost.h"
#import "GoalKeeper.h"
#interface GoalPost ()
#property NSArray* post;
#end
#implementation GoalPost
- (instancetype)init
{
self = [super initWithColor:[CCColor lightGrayColor]];
if (self) {
....
[self addChild:aPost];
}
//Call goal keeper and add.
//for touch to run goal keeper.
[self setUserInteractionEnabled:true];
self.keeper = [GoalKeeper node];
//Put it in front of goal post.
self.keeper.position = ccp(self.contentSize.width/2, 0);
[self addChild:self.keeper];
}
return self;
}
-(void)touchBegan:(UITouch *)touch withEvent:(UIEvent *)event{
CGPoint touchLoc = [touch locationInNode:self];
NSLog(#"Goal touched: %f %f", touchLoc.x, touchLoc.y);
//Goal keeper call
[self.keeper keeperLocation:touchLoc];
}
#end
Finally, GoalKeeper.m
.h
#import "cocos2d.h"
#interface GoalKeeper : CCNodeColor { }
-(void)keeperLocation:(CGPoint)location;
#end
.m
#import "GoalKeeper.h"
#interface GoalKeeper()
#property CGPoint touchLoc;
#end
#implementation GoalKeeper
- (instancetype)init
{
self = [super initWithColor:[CCColor blueColor]];
if (self) {
[self setUserInteractionEnabled:TRUE];
[self setContentSize:CGSizeMake(30, 70)];
}
return self;
}
-(void)keeperLocation:(CGPoint)location {
CGPoint offset = ccpSub(location, self.position);
int targetX = self.position.x + offset.x;
int targetY = self.position.y + offset.y;
CGPoint targetPosition = ccp(targetX, targetY);
CCActionMoveBy *actionMoveBy = [CCActionMoveBy actionWithDuration:0.5f position:offset];
[self runAction:actionMoveBy];
}
#end

why my admob doesn't show?

There is no errors in console, and seems everything work. But the bannerView_ just didn't show.
here is my AdMobObject.h
#import "AdMobObject.h"
#import "RootViewController.h"
#import "GADBannerView.h"
#class RootViewController;
#class GADBannerView;
#interface AdMobObject : UIViewController{
RootViewController * viewController;
GADBannerView * bannerView_;
}
+ (AdMobObject *) shared;
- (void) addAdMob;
- ( void) hideAdMob;
#end
here is my AdMobObject.mm
#import "AdMobObject.h"
#import "AppController.h"
#import "RootViewController.h"
#import "EAGLView.h"
#import "cocos2d.h"
#implementation AdMobObject
static AdMobObject* instance;
+(AdMobObject *) shared{
#synchronized(self){
if( instance == nil ){
instance = [[self alloc] init];
}
}
return instance;
}
- (void) addAdMob{
NSLog(#"----------addAdMob");
bannerView_ = [[GADBannerView alloc] initWithAdSize:kGADAdSizeSmartBannerPortrait];
CGRect screenRect = [[UIScreen mainScreen] bounds];
CGFloat screenHeight = screenRect.size.height;
CGFloat screenWidth = screenRect.size.width;
viewController.view.frame = CGRectMake(0,0,screenWidth,screenHeight);
[bannerView_ setFrame:CGRectMake(0,
screenHeight-bannerView_.bounds.size.height,
//0,
bannerView_.bounds.size.width,
bannerView_.bounds.size.height)];
bannerView_.adUnitID = #"myadmobid";
bannerView_.rootViewController = viewController;
[viewController.view addSubview:bannerView_];
GADRequest *request = [GADRequest request];
// For testing
request.testDevices = [NSArray arrayWithObjects:#"mydeviceid", nil];
[bannerView_ loadRequest:request];
[viewController.view addSubview:bannerView_];
}
- (void) showAdMob{
}
- (void) hideAdMob{
[bannerView_ setHidden:YES];
}
#end
Then i have a class to manage it:
#interface MyGameCenterManager : NSObject
{
}
+(MyGameCenterManager *) shared;
+(void) addAdMob;
+(void) hideAdMob;
#end
here is the class implementation:
#implementation MyGameCenterManager
static MyGameCenterManager *instance;
+(MyGameCenterManager*)shared{
#synchronized(self){
if(instance == nil){
instance = [[self alloc] init];
}
}
return instance;
}
+ (void) addAdMob
{
[[AdMobObject shared] addAdMob];
}
+ (void) hideAdMob{
[[AdMobObject shared] hideAdMob];
}
Finally, I have cpp class:
include "cocos2d.h"
#include "ScriptingCore.h"
namespace ls{
class GameCenterBridge: public cocos2d::CCObject{
public:
static cocos2d::CCScene* scene();
virtual bool init();
CREATE_FUNC(GameCenterBridge);
void addAdMob();
void hideAdMob();
};
}
I called my class:
ls::GameCenterBridge * class = new ls::GameCenterBridge();
class->addAdMob();
I can see the console log:
----------addAdMob
which means it enter into the addAdMob function. And there has no other errors.
But the banner view just didn't show.
the only way I make it show is to add codes into AppController.mm in didFinishLaunchingWithOptions. But I am just wondering why it won't work for my self created class.
When i was implementing AdMob i showed the ad only when it got loaded from the server. You can achieve this by implementing GADBannerViewDelegate, especially interesting methods are
- (void)adViewDidReceiveAd:(GADBannerView *)bannerView;
- (void)adView:(GADBannerView *)bannerView didFailToReceiveAdWithError:(GADRequestError *)error
This way you can be sure that you received the ad.

EXC_BAD_ACCESS when using NSKeyedUnarchiver to load saved state

I have subclassed a CCSprite to make an object that can be encoded and decoded. I want to save sprites state and load it again at particular positions. Everything seems to be okay apart from decoding with NSKeyedUnarchiver (see loadIconSpriteState below) which gives an EXC_BAD_ACCESS Below is the code:
HelloWorldLayer.h
#interface CCIconSprite : CCSprite {
NSString *iconName;
float iconXPos;
float iconYPos;
}
#property (nonatomic, retain) NSString *iconName;
#property (nonatomic, assign) float iconXPos;
#property (nonatomic, assign) float iconYPos;
+ (id)iconWithType:(NSString*)imageName;
- (id)initWithIconType:(NSString*)imageName;
#end
#interface HelloWorldLayer : CCLayer < NSCoding>
{
CCIconSprite* testSprite;
BOOL savedState;
CGSize size;
CCMoveTo* moveTo;
NSMutableArray* saveSpriteArray;
NSData* savedSpriteData;
}
+(CCScene *) scene;
#end
HelloWorldLayer.m:
CCIconSprite implementation:
#implementation CCIconSprite
#synthesize iconXPos;
#synthesize iconYPos;
#synthesize iconName;
+ (id)iconWithType:(NSString*)imageName
{
return [[[[self class] alloc] initWithIconType:imageName] autorelease];
}
- (id)initWithIconType:(NSString*)imageName
{
self = [super initWithFile:imageName];
if (!self) return nil;
iconName = imageName;
self.position = ccp(iconXPos, iconYPos);
return self;
}
Encoding and decoding:
- (id)initWithCoder:(NSCoder *)decoder {
NSString* imageFileName = [decoder decodeObjectForKey:#"imageFileName"];
self = [self initWithIconType:imageFileName];
if (!self) return nil;
self.iconXPos = [decoder decodeFloatForKey:#"iconXPos"];
self.iconYPos = [decoder decodeFloatForKey:#"iconYPos"];
return self;
}
- (void)encodeWithCoder:(NSCoder *)encoder
{
[encoder encodeObject:iconName forKey:#"imageFileName"];
[encoder encodeFloat:self.iconXPos forKey:#"iconXPos"];
[encoder encodeFloat:self.iconYPos forKey:#"iconYPos"];
}
#end
HelloWorldLayer implementation:
#implementation HelloWorldLayer
+(CCScene *) scene
{
CCScene *scene = [CCScene node];
HelloWorldLayer *layer = [HelloWorldLayer node];
[scene addChild: layer];
return scene;
}
-(id) init
{
if( (self=[super init]) ) {
self.scale = 0.5;
savedState = NO;
size = [[CCDirector sharedDirector] winSize];
testSprite = [CCIconSprite spriteWithFile:#"Icon.png"];
testSprite.position = ccp(size.width/4, size.width/4);
testSprite.anchorPoint = ccp(0,0);
[self addChild:testSprite];
moveTo = [CCMoveTo actionWithDuration:3 position:ccp(3*size.width/4, 3*size.width/4)];
[testSprite runAction:moveTo];
[self schedule:#selector(saveAndLoadSpriteState)];
}
return self;
}
Saving and loading state:
-(void)saveIconSpriteState {
saveSpriteArray = [[NSMutableArray alloc] init];
[saveSpriteArray addObject:testSprite];
savedSpriteData = [NSKeyedArchiver archivedDataWithRootObject:saveSpriteArray];
}
-(void)loadIconSpriteState {
[NSKeyedUnarchiver unarchiveObjectWithData:savedSpriteData];
}
-(void)saveAndLoadSpriteState {
if ((testSprite.position.x > size.width/2) && !savedState) {
savedState = YES;
[self saveIconSpriteState];
}
else if ((testSprite.position.x == 3*size.width/4) && savedState) {
savedState = NO;
[self loadIconSpriteState];
[testSprite runAction:moveTo];
}
}
#end
EDIT
After setting an exception break point I got the following error
Assertion failure in -[CCIconSprite initWithFile:]
pointing to the in line:
NSAssert(filename != nil, #"Invalid filename for sprite");
in the
-(id) initWithFile:(NSString*)filename
method of the CCSprite.m class.
Just a hunch but maybe that's it. When decoding a string, you should copy it because you don't own it at this point, nor are you assigning it to a strong or copy property.
- (id)initWithCoder:(NSCoder *)decoder {
NSString* imageFileName = [[decoder decodeObjectForKey:#"imageFileName"] copy];
self = [self initWithIconType:imageFileName];
if (!self) return nil;
...
}
If that's not it, set a breakpoint and verify that the string is indeed correct when encoding and when decoding.

connecting 2 or more tableviews to display information from the same nsarray

I'm confussed and tried many different things to eliminate the errors im facing.
I have a NSArray of proteins, in my storyboard i have two seperate table views that i wish to both display the protiens array.
i am having errors at -(UITableViewcell *) and #end of which there are two } errors.
If anyone can help please find my ViewController.m code below: (Please disregard the segue coding near the end)
#import "JViewController.h"
#import "OneViewController.h"
#import "TwoViewController.h"
#interface JViewController ()
#end
#implementation JViewController
{
NSArray *proteins;
}
#synthesize tableView1;
#synthesize tableView2;
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
proteins = [NSArray arrayWithObjects:#"Chicken", #"Turkey", #"Ham", #"Steak", #"Pork Chop", #"Roast Lamb", #"Salmon", #"Egg", #"Lentils", #"Kidney Beans", nil];
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
if (tableView == tableView1) {
return [proteins count];
{
if (tableView == tableView2) {
return [proteins count];
}
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"ProteinCell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
if (tableView1 == tableView2)
{
proteins;
}
else
{
proteins;
}
cell.textLabel.text = [proteins objectAtIndex:indexPath.row];
return cell;
}
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
if ([segue.identifier isEqualToString:#"showMealOneDetail"])
{
NSIndexPath *indexPath = [self.tableView indexPathForSelectedRow];
OneViewController *destViewController = segue.destinationViewController;
destViewController.proteinName = [proteins objectAtIndex:indexPath.row];
}
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
#end
Big thankyou in advance.
You have two open brackets here, close this one:
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
if (tableView == tableView1) {
return [proteins count];
->> {
if (tableView == tableView2) {
return [proteins count];
}
}
And what do you mean with this?
if (tableView1 == tableView2)
{
proteins;
}
else
{
proteins;
}
Try this:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"ProteinCell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
cell.textLabel.text = [proteins objectAtIndex:indexPath.row];
return cell;
}
Have you been able to show anything in one tableview? Make sure you have connected dataSource and delegate.
Hope it helps =)

Replace textfields with dropdown select fields

I have three model classes that look as below:
class Model(models.Model):
model = models.CharField(max_length=20, blank=False)
manufacturer = models.ForeignKey(Manufacturer)
date_added = models.DateField(default=datetime.today)
def __unicode__(self):
name = ''+str(self.manufacturer)+" "+str(self.model)
return name
class Series(models.Model):
series = models.CharField(max_length=20, blank=True, null=True)
model = models.ForeignKey(Model)
date_added = models.DateField(default=datetime.today)
def __unicode__(self):
name = str(self.model)+" "+str(self.series)
return name
class Manufacturer(models.Model):
MANUFACTURER_POPULARITY_CHOICES = (
('1', 'Primary'),
('2', 'Secondary'),
('3', 'Tertiary'),
)
manufacturer = models.CharField(max_length=15, blank=False)
date_added = models.DateField(default=datetime.today)
manufacturer_popularity = models.CharField(max_length=1,
choices=MANUFACTURER_POPULARITY_CHOICES)
def __unicode__(self):
return self.manufacturer
I want to have the fields for model series and manufacturer represented as dropdowns instead of text fields. I have customized the model forms as below:
class SeriesForm(ModelForm):
series = forms.ModelChoiceField(queryset=Series.objects.all())
class Meta:
model = Series
exclude = ('model', 'date_added',)
class ModelForm(ModelForm):
model = forms.ModelChoiceField(queryset=Model.objects.all())
class Meta:
model = Model
exclude = ('manufacturer', 'date_added',)
class ManufacturerForm(ModelForm):
manufacturer = forms.ModelChoiceField(queryset=Manufacturer.objects.all())
class Meta:
model = Manufacturer
exclude = ('date_added',)
However, the dropdowns are populated with the unicode in the respective class...how can I further customize this to get the end result I want?
Also, how can I populate the forms with the correct data for editing? Currently only SeriesForm is populated. The starting point of all this is from another class whose declaration is as below:
class CommonVehicle(models.Model):
year = models.ForeignKey(Year)
series = models.ForeignKey(Series)
....
def __unicode__(self):
name = ''+str(self.year)+" "+str(self.series)
return name
Check out http://docs.djangoproject.com/en/dev/ref/forms/fields/#modelchoicefield and in particular the chunk that starts
The unicode method of the model will be called to generate string representations of the objects for use in the field's choices; to provide customized representations, subclass ModelChoiceField and override label_from_instance. This method will receive a model object, and should return a string suitable for representing it.
[There then follows a good example of how to do that]
HTH
Steve
.h files: (UIView Create)
#import <UIKit/UIKit.h>
#class NIDropDown;
#protocol NIDropDownDelegate
- (void) niDropDownDelegateMethod: (NIDropDown *) sender;
#end
#interface NIDropDown : UIView <UITableViewDelegate, UITableViewDataSource>
{
NSString *animationDirection;
UIImageView *imgView;
}
#property (nonatomic, retain) id <NIDropDownDelegate> delegate;
#property (nonatomic, retain) NSString *animationDirection;
-(void)hideDropDown:(UIButton *)b;
- (id)showDropDown:(UIButton *)b :(CGFloat *)height :(NSArray *)arr :(NSArray *)imgArr :(NSString *)direction;
#end
.m Files:
#import "NIDropDown.h"
#import "QuartzCore/QuartzCore.h"
#interface NIDropDown ()
#property(nonatomic, strong) UITableView *table;
#property(nonatomic, strong) UIButton *btnSender;
#property(nonatomic, retain) NSArray *list;
#property(nonatomic, retain) NSArray *imageList;
#end
#implementation NIDropDown
#synthesize table;
#synthesize btnSender;
#synthesize list;
#synthesize imageList;
#synthesize delegate;
#synthesize animationDirection;
- (id)showDropDown:(UIButton *)b :(CGFloat *)height :(NSArray *)arr :(NSArray *)imgArr :(NSString *)direction {
btnSender = b;
animationDirection = direction;
self.table = (UITableView *)[super init];
if (self) {
// Initialization code
CGRect btn = b.frame;
self.list = [NSArray arrayWithArray:arr];
self.imageList = [NSArray arrayWithArray:imgArr];
if ([direction isEqualToString:#"up"]) {
self.frame = CGRectMake(btn.origin.x, btn.origin.y, btn.size.width, 0);
self.layer.shadowOffset = CGSizeMake(-5, -5);
}else if ([direction isEqualToString:#"down"]) {
self.frame = CGRectMake(btn.origin.x, btn.origin.y+btn.size.height, btn.size.width, 0);
self.layer.shadowOffset = CGSizeMake(-5, 5);
}
self.layer.masksToBounds = NO;
self.layer.cornerRadius = 8;
self.layer.shadowRadius = 5;
self.layer.shadowOpacity = 0.5;
table = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, btn.size.width, 0)];
table.delegate = self;
table.dataSource = self;
table.layer.cornerRadius = 5;
//table.backgroundColor = [UIColor clearColor];
table.backgroundColor = [UIColor scrollViewTexturedBackgroundColor];//[UIColor colorWithRed:0.239 green:0.239 blue:0.239 alpha:1];
table.separatorStyle = UITableViewCellSeparatorStyleNone;
table.separatorColor = [UIColor clearColor];
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:0.5];
if ([direction isEqualToString:#"up"]) {
self.frame = CGRectMake(btn.origin.x, btn.origin.y-*height, btn.size.width, *height);
} else if([direction isEqualToString:#"down"]) {
self.frame = CGRectMake(btn.origin.x, btn.origin.y+btn.size.height, btn.size.width, *height);
}
table.frame = CGRectMake(0, 0, btn.size.width, *height);
[UIView commitAnimations];
[b.superview addSubview:self];
[self addSubview:table];
}
return self;
}
-(void)hideDropDown:(UIButton *)b {
CGRect btn = b.frame;
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:0.5];
if ([animationDirection isEqualToString:#"up"]) {
self.frame = CGRectMake(btn.origin.x, btn.origin.y, btn.size.width, 0);
}else if ([animationDirection isEqualToString:#"down"]) {
self.frame = CGRectMake(btn.origin.x, btn.origin.y+btn.size.height, btn.size.width, 0);
}
table.frame = CGRectMake(0, 0, btn.size.width, 0);
[UIView commitAnimations];
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return 1;
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
return 40;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return [self.list count];
}
- (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];
if(UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad){
cell.textLabel.font = [UIFont systemFontOfSize:20];
} else {
cell.textLabel.font = [UIFont systemFontOfSize:8];
}
cell.textLabel.textAlignment = UITextAlignmentCenter;
}
if ([self.imageList count] == [self.list count]) {
//cell.textLabel.text =[list objectAtIndex:indexPath.row];
//cell.imageView.image = [imageList objectAtIndex:indexPath.row];
} else if ([self.imageList count] > [self.list count]) {
// cell.textLabel.text =[list objectAtIndex:indexPath.row];
if (indexPath.row < [imageList count]) {
// cell.imageView.image = [imageList objectAtIndex:indexPath.row];
}
} else if ([self.imageList count] < [self.list count]) {
cell.textLabel.text =[list objectAtIndex:indexPath.row];
if (indexPath.row < [imageList count]) {
//cell.imageView.image = [imageList objectAtIndex:indexPath.row];
}
}
cell.textLabel.textColor = [UIColor blackColor];
UIView * v = [[UIView alloc] init];
v.backgroundColor = [UIColor grayColor];
cell.selectedBackgroundView = v;
return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
[self hideDropDown:btnSender];
UITableViewCell *c = [tableView cellForRowAtIndexPath:indexPath];
//[btnSender setTitle:c.textLabel.text forState:UIControlStateNormal];
NSUserDefaults *prefCountOne = [NSUserDefaults standardUserDefaults];
[prefCountOne setObject:c.textLabel.text forKey:#"TYPETEXT"];
for (UIView *subview in btnSender.subviews) {
if ([subview isKindOfClass:[UIImageView class]]) {
[subview removeFromSuperview];
}
}
imgView.image = c.imageView.image;
imgView = [[UIImageView alloc] initWithImage:c.imageView.image];
imgView.frame = CGRectMake(5, 5, 25, 25);
[btnSender addSubview:imgView];
[self myDelegate];
}
- (void) myDelegate {
[self.delegate niDropDownDelegateMethod:self];
}
Use TextField in .m Files:
- (IBAction) fnDisplayCategory:(id)sender {
[pTxtCategory resignFirstResponder];
if(pBtnCategory == nil) {
CGFloat f = 200;
pBtnCategory = [[NIDropDownForTwoArrays alloc]showDropDown:pTxtCategory :&f :pArCategory :#"down" :1];
pBtnCategory.delegate = self;
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
[defaults setObject:#"First" forKey:#"DropDown"];
[[NSUserDefaults standardUserDefaults] synchronize];
}
else {
[pBtnCategory hideDropDown:pTxtCategory];
[self rel];
}
}
- (void)rel {
pBtnCategory = nil;
}