iPhone-Grouped Table View adding two radio buttons for each row - iphone

I want to display two radio buttons in each row and i need to set action for two buttons i was facing problem in setting the actions and display text and radio buttons in correct position for each row, If some one help please.
Here is my code
Thanks in advance
.h file
#import <UIKit/UIKit.h>
//#class CustomCellQuestionnaireList;
#interface QuestionnaireListView : UIViewController <UITableViewDataSource, UITableViewDelegate,UINavigationControllerDelegate,UISearchBarDelegate>
{
//NSMutableArray *dataArray;
UITableView *theTableView;
UILabel *lbl_child;
UILabel *lbl_title;
UILabel *lbl_time;
NSMutableArray *tableData;//will be storing data that will be displayed in table
NSMutableArray *tableSubData;
//NSMutableArray *tableTitleArray;
//CustomCellQuestionnaireList *cell;
IBOutlet UIButton *myButton;
IBOutlet UIButton *myButton2;
BOOL isSelected;
IBOutlet UIView *view1;
IBOutlet UIBarButtonItem *saveBtn;
IBOutlet UIBarButtonItem *discardBtn;
IBOutlet UIBarButtonItem *okBtn;
//IBOutlet UIToolbar *m_toolBar;
//IBOutlet UIToolbar *m_okToolBar;
}
#property (nonatomic,retain) IBOutlet UILabel *lbl_child;
#property (nonatomic,retain) IBOutlet UILabel *lbl_title;
#property (nonatomic,retain) IBOutlet UILabel *lbl_time;
#property (nonatomic,retain) IBOutlet UITableView * theTableView;
//#property(nonatomic,retain) NSMutableArray *dataArray;
#property(nonatomic, retain) NSMutableArray *tableData;
#property(nonatomic, retain)NSMutableArray *tableSubData;
#property(nonatomic, retain)UIView *view1;
#property(nonatomic, retain)UIBarButtonItem *saveBtn;
#property(nonatomic, retain)UIBarButtonItem *discardBtn;
#property(nonatomic, retain)UIBarButtonItem *okBtn;
//#property(nonatomic, retain)UIToolbar *m_toolBar;
//#property(nonatomic, retain)UIToolbar *m_okToolBar;
- (IBAction)onClickLeftArrow;
- (IBAction)onClickRightArrow;
//- (IBAction)savePressed:(id)sender;
- (IBAction)selectRadioButon:(UIButton *)button;
//-(IBAction)okPressed:(id)sender;
#end
.m file
#implementation QuestionnaireListView
#synthesize tableData, tableSubData;
#synthesize theTableView;
//#synthesize dataArray;
#synthesize lbl_child;
#synthesize lbl_title;
#synthesize lbl_time;
#synthesize view1;
#synthesize saveBtn;
#synthesize discardBtn;
#synthesize okBtn;
//#synthesize m_toolBar;
//#synthesize m_okToolBar;
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization.
UIButton *button1 = [UIButton buttonWithType:UIButtonTypeCustom];
[button1 setImage:[UIImage imageNamed:#"saveBtn.png"] forState:UIControlStateNormal];
[button1 setFrame:CGRectMake(0, 0, 66, 34)];
[button1 addTarget:self action:#selector(saveBtn:) forControlEvents:UIControlEventTouchUpInside];
[button1 setTitle:#"Save" forState:UIControlStateNormal];
UIBarButtonItem *barButtonItem1 = [[UIBarButtonItem alloc] initWithCustomView:button1];
NSMutableArray *toolBarItems = [[[NSMutableArray alloc] init] autorelease];
[toolBarItems addObject:barButtonItem1];
//[self setToolbarItems:toolBarItems];
UIButton *button2 = [UIButton buttonWithType:UIButtonTypeCustom];
[button2 setImage:[UIImage imageNamed:#"okBtn.png"] forState:UIControlStateNormal];
[button2 setFrame:CGRectMake(100, 0, 66, 34)];
//[button2 addTarget:self action:#selector(home:) forControlEvents:UIControlEventTouchUpInside];
//[button2 setTitle:#"Home" forState:UIControlStateNormal];
UIBarButtonItem *barButtonItem2 = [[UIBarButtonItem alloc] initWithCustomView:button2];
[toolBarItems addObject:barButtonItem2];
button2.hidden = YES;
//[self setToolbarItems:toolBarItems];
UIButton *button3 = [UIButton buttonWithType:UIButtonTypeCustom];
[button3 setImage:[UIImage imageNamed:#"okBtn.png"] forState:UIControlStateNormal];
[button3 setFrame:CGRectMake(150, 0, 66, 34)];
//[button4 addTarget:self action:#selector(okBtn:) forControlEvents:UIControlEventTouchUpInside];
//[button4 setTitle:#"Home" forState:UIControlStateNormal];
UIBarButtonItem *barButtonItem3 = [[UIBarButtonItem alloc] initWithCustomView:button3];
[toolBarItems addObject:barButtonItem3];
button3.hidden = YES;
UIButton *button4 = [UIButton buttonWithType:UIButtonTypeCustom];
[button4 setImage:[UIImage imageNamed:#"discardBtn.png"] forState:UIControlStateNormal];
[button4 setFrame:CGRectMake(200, 0, 66, 34)];
[button4 addTarget:self action:#selector(discardBtn:) forControlEvents:UIControlEventTouchUpInside];
[button4 setTitle:#"Discard" forState:UIControlStateNormal];
UIBarButtonItem *barButtonItem4 = [[UIBarButtonItem alloc] initWithCustomView:button4];
[toolBarItems addObject:barButtonItem4];
[self setToolbarItems:toolBarItems];
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
//[self.theTableView.layer setCornerRadius:30.0];
// create button
//UIButton* logOutButton = [UIButton buttonWithType:101]; // left-pointing shape!
UIButton *logOutButton = [UIButton buttonWithType:100];
[logOutButton setImage:[UIImage imageNamed:#"exitBtn.png"] forState:UIControlStateNormal];
[logOutButton addTarget:self action:#selector(logoutButtonTouched) forControlEvents:UIControlEventTouchUpInside];
[logOutButton setTitle:#"Logout" forState:UIControlStateNormal];
UIBarButtonItem* logOutItem = [[UIBarButtonItem alloc] initWithCustomView:logOutButton];
self.navigationItem.leftBarButtonItem = logOutItem;
[lbl_title setText:#"Store View"];
[lbl_title setTextColor:[UIColor whiteColor]];
//NSString *date = [[NSDate date] dateFormat];
NSString *date = [NSDate date];
NSLog(#"date:%#",date);
[lbl_time setText:[NSString stringWithFormat:#"%#",date]];
[lbl_time setTextColor:[UIColor whiteColor]];
// add to toolbar, or to a navbar (you should only have one of these!)
//[toolbar setItems:[NSArray arrayWithObject:backItem]];
//tableTitleArray = [[NSMutableArray alloc]init];
// [tableTitleArray addObject:#"General"];
// [tableTitleArray addObject:#"Product"];
tableData = [[NSMutableArray alloc] init];
[tableData addObject:#"Is the floor clean?"];
[tableData addObject:#"Are the food items stacked in freezer?"];
tableSubData = [[NSMutableArray alloc] init];
[tableData addObject:#"Has the snow been cleared at the entrance?"];
[tableData addObject:#"Are milk products available"];
view1.hidden = YES;
//m_okToolBar.hidden= YES;
//m_toolBar.barStyle = UIBarStyleBlackTranslucent;
//m_toolBar.tintColor = [UIColor blackColor]; m_toolBar.alpha = 0.7;
//UIBarButtonItem *btn = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:#"nextBtn.png" style:UIBarButtonItemStyleBordered target:self action:#selector(customButtonHandler:)]];
// btn.frame = CGRectMake(5, 20, 30, 30);
//[btn release];
}
-(void)saveBtn:(id)sender {
NSLog(#"Button pressed");
view1.hidden = NO;
UIButton *button1 = [UIButton buttonWithType:UIButtonTypeCustom];
[button1 setImage:[UIImage imageNamed:#"saveBtn.png"] forState:UIControlStateNormal];
[button1 setFrame:CGRectMake(0, 0, 105, 34)];
//[button1 addTarget:self action:#selector(saveBtn:) forControlEvents:UIControlEventTouchUpInside];
//[button1 setTitle:#"Save" forState:UIControlStateNormal];
UIBarButtonItem *barButtonItem1 = [[UIBarButtonItem alloc] initWithCustomView:button1];
NSMutableArray *toolBarItems = [[[NSMutableArray alloc] init] autorelease];
[toolBarItems addObject:barButtonItem1];
UIButton *button2 = [UIButton buttonWithType:UIButtonTypeCustom];
UIButton *button3 = [UIButton buttonWithType:UIButtonTypeCustom];
UIButton *button4 = [UIButton buttonWithType:UIButtonTypeCustom];
[button2 setImage:[UIImage imageNamed:#"okBtn.png"] forState:UIControlStateNormal];
[button2 setFrame:CGRectMake(0, 0, 66, 34)];
[button2 addTarget:self action:#selector(okBtn:) forControlEvents:UIControlEventTouchUpInside];
[button2 setTitle:#"OK" forState:UIControlStateNormal];
UIBarButtonItem *barButtonItem2 = [[UIBarButtonItem alloc] initWithCustomView:button2];
[toolBarItems addObject:barButtonItem2];
[self setToolbarItems:toolBarItems];
button1.hidden = YES;
button2.hidden = NO;
button3.hidden = YES;
button4.hidden = YES;
}
-(void)okBtn:(id)sender {
view1.hidden = YES;
[self.navigationController popToViewController:[self.navigationController.viewControllers objectAtIndex:3] animated:YES];
}
//-(void)viewWillAppear:(BOOL)animated
//{
//
// //VizueraQAppDelegate *appDelegate = (VizueraQAppDelegate *)[[UIApplication sharedApplication] delegate];
// //[appDelegate RefreshIndexArray];
//
//}
- (void)logoutButtonTouched {
[self.navigationController popToViewController:[self.navigationController.viewControllers objectAtIndex:0] animated:YES];
}
- (IBAction)onClickLeftArrow {
[self.navigationController popViewControllerAnimated:YES];
}
- (IBAction)onClickRightArrow {
//[self.navigationController popViewControllerAnimated:YES];
}
- (void)viewDidUnload
{
[super viewDidUnload];
// release the array
// self.dataArray = nil;
// tableData = nil;
}
- (void)dealloc
{
//[cell release];
[tableData release];
[tableSubData release];
[super dealloc];
}
#pragma mark - UIViewController delegate methods
- (void)didReceiveMemoryWarning
{
// invoke super's implementation to do the Right Thing, but also release the input controller since we can do that
// In practice this is unlikely to be used in this application, and it would be of little benefit,
// but the principle is the important thing.
//
[super didReceiveMemoryWarning];
}
//- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
//{
// return YES;
//}
#pragma mark - UITableView delegate methods
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
if (tableData!=nil && tableSubData!=nil) {
return 2;
}
else {
return 1;
}
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
if (section == 0) {
return [tableData count];
}
else {
return [tableSubData count];
}
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
return 74;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
}
//- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section{
//return ([NSString stringWithFormat:#"%#", [tableTitleArray objectAtIndex:section]]);
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {
NSString *returnString = #"";
if (tableData!=nil && section == 0) {
returnString = #"General";
}
else if(section == 1){
returnString = #"Product";
}
return returnString;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *kCustomCellID = #"MyCellID";
UITableViewCell *cell = (UITableViewCell *)[theTableView dequeueReusableCellWithIdentifier:kCustomCellID];
//CustomCell *cell = (CustomCell *)[theTableView dequeueReusableCellWithIdentifier:kCustomCellID];
if (cell == nil)
{
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle
reuseIdentifier:kCustomCellID] autorelease];
//cell = (CustomCell *)[[[CustomCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:kCustomCellID] autorelease];
cell.selectionStyle = UITableViewCellSelectionStyleNone;
//cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
cell.selectionStyle = UITableViewCellSelectionStyleNone;
cell.tag=[indexPath row];
NSInteger tagCount;
myButton = [[UIButton alloc]initWithFrame:CGRectMake(10, 48, 20, 20)];
[myButton setImage:[UIImage imageNamed:#"radioBtn.png"] forState:UIControlStateNormal];
[myButton addTarget:self action:#selector(selectRadioButon:) forControlEvents:UIControlEventTouchUpInside];
myButton.tag = ++tagCount;
[cell.contentView addSubview:myButton];
tagCount++;
myButton2 = [[UIButton alloc]initWithFrame:CGRectMake(100, 48, 20, 20)];
[myButton2 setImage:[UIImage imageNamed:#"radioBtn.png"] forState:UIControlStateNormal];
[myButton2 addTarget:self action:#selector(selectRadioButon:) forControlEvents:UIControlEventTouchUpInside];
myButton2.tag = tagCount;
[cell.contentView addSubview:myButton2];
tagCount++;
/*UILabel *m_label = [[UILabel alloc] init];
m_label.text = #"Yes";
m_label.textColor = [UIColor redColor];
m_label.frame = CGRectMake(15, 48, 40, 40);
[m_label release]; */
CGRect labelRect = CGRectMake(35, 49, 30, 15);
UILabel *label = [[UILabel alloc] initWithFrame:labelRect];
//label.backgroundColor = [UIColor redColor];
label.text = #"Yes";
label.textAlignment = UITextAlignmentLeft;
label.font = [UIFont boldSystemFontOfSize:14];
label.tag = 10;
[cell.contentView addSubview:label];
[label release];
CGRect labelRect1 = CGRectMake(125, 50, 30, 15);
UILabel *label1 = [[UILabel alloc] initWithFrame:labelRect1];
//label.backgroundColor = [UIColor redColor];
label1.text = #"No";
label1.textAlignment = UITextAlignmentLeft;
label1.font = [UIFont boldSystemFontOfSize:14];
label1.tag = 10;
[cell.contentView addSubview:label1];
[label1 release];
}
//cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
if([tableData count]>indexPath.row)
cell.textLabel.text = [tableData objectAtIndex:indexPath.row];
cell.textLabel.frame = CGRectMake(0, 0, 20, 20);
return cell;
}
- (IBAction)selectRadioButon:(UIButton *)button{
for (UIButton *but in [self.view subviews]) {
if ([but isKindOfClass:[UIButton class]] && ![but isEqual:button]) {
[but setSelected:NO];
}
}
if (!button.selected) {
button.selected = !button.selected;
}
if (isSelected) {
[button setImage:[UIImage imageNamed:#"radioBtn.png"] forState:UIControlStateNormal];
isSelected = NO;
}
else {
[button setImage:[UIImage imageNamed:#"radioBtn_active.png"] forState:UIControlStateNormal];
isSelected = YES;
}
}

Keep your "initWithFrame" codes inside the if(cell == nil) { } for when you're drawing your table cells and move all the "setting" UI elements outside that if.
See my code example here:
Can't Change Transparent BG in Custom UITableViewCell (Grouped Table View)

Related

how do i achieve this on navigationBar

i want design my navigationBar like following figure, how can i achieve this.is it possible on navigationBar or i don't need navigationBar.
Topics & settings must be buttons, because i want to navigate to another viewController
I don't know whether it is possible on navigation bar.
I saw a similar one using tabbar: Center Button in Tab Bar for iOS
And you can achieve the same thing using : ALToolbar
Also check KLHorizontalselect
Create custom Navigation Bar with custom Delegate for that.. i used this see example bellow..
create CustomNavBar.h file like bellow...
// Created by Paras on 03/12/11.
// Copyright 2010 __MyCompanyName__. All rights reserved.
//
#import <UIKit/UIKit.h>
#import "CustomNavBarDelegate.h"
#interface CustomNavBar : UIView {
NSObject<CustomNavBarDelegate> *delegate;
NSString *strCatOrLoc;
UIButton *btnBack;
UIButton *btnLeft;
UIImageView *imgRightImage;
UILabel *lbl;
UIImageView *imgTitle;
}
#property (nonatomic, retain) UIButton *btnBack;
#property (nonatomic, assign) NSObject<CustomNavBarDelegate> *delegate;
-(void)onclickBack:(id)sender;
- (id) initWithFrame: (CGRect)rect;
-(void)setImage:(UIImage*)img NavTitle:(NSString *)title;
-(void)setTitleImage:(UIImage*)img rightImage:(UIImage *)imgRight;
-(void)setwithoutlogo:(UIImage*)img NavTitle:(NSString *)title;
#end
now see the code of CustomNavBar.m file bellow...
// Created by Paras on 03/12/11.
// Copyright 2010 __MyCompanyName__. All rights reserved.
//
#import "CustomNavBar.h"
#import <QuartzCore/QuartzCore.h>
#import "AppDelegate.h"
#implementation CustomNavBar
#synthesize delegate;
#synthesize btnBack;
#pragma mark -
#pragma mark init methods
- (id) initWithFrame: (CGRect)rect {
if (self == [super initWithFrame:rect]) {
//[self setBackgroundColor:[UIColor colorWithRed:241.0f/255.0f green:241.0f/255.0f blue:237.0f/255.0f alpha:1.0f]];
// [self setBackgroundColor:[UIColor whiteColor]];
btnBack = [UIButton buttonWithType:UIButtonTypeCustom];
btnBack.frame = CGRectMake(10.0, 8.0, 50.0, 28.0);
[btnBack addTarget:self
action:#selector(onclickBack:)
forControlEvents:UIControlEventTouchDown];
btnBack.layer.masksToBounds = YES;
btnBack.layer.cornerRadius = 8.0;
btnBack.layer.borderWidth = 0.5;
btnBack.layer.borderColor = [[UIColor blackColor] CGColor];
//btnBack.titleLabel.textColor = [UIColor blackColor];
// btnBack.titleLabel.text = #"Back";
[btnBack.titleLabel setFont:Arial13];
[btnBack setTitle:#"" forState:UIControlStateNormal];
[btnBack setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
// [btnBack setBackgroundColor:[UIColor colorWithRed:241.0f/255.0f green:241.0f/255.0f blue:237.0f/255.0f alpha:1.0f]];
[btnBack setBackgroundColor:[UIColor clearColor]];
[self addSubview:btnBack];
btnLeft = [UIButton buttonWithType:UIButtonTypeCustom];
btnLeft.frame = CGRectMake(280.0, 8.0, 310.0, 28.0);
[btnLeft addTarget:self
action:#selector(onclickLeft:)
forControlEvents:UIControlEventTouchDown];
[btnLeft setBackgroundColor:[UIColor clearColor]];
[self addSubview:btnLeft];
}
return self;
}
-(void)setImage:(UIImage*)img NavTitle:(NSString *)title {
imgRightImage = [[UIImageView alloc] initWithFrame:CGRectMake(0.0,0.0,320.0,44.0)];
imgRightImage.contentMode = UIViewContentModeScaleAspectFill;
imgRightImage.clipsToBounds = YES;
imgRightImage.layer.masksToBounds = YES;
// imgRightImage.layer.cornerRadius = 11.0;
// imgRightImage.layer.borderWidth = 0.5;
[imgRightImage setImage:img];
[imgRightImage setBackgroundColor:[UIColor clearColor]];
[self addSubview:imgRightImage];
[imgRightImage release];
}
-(void)setwithoutlogo:(UIImage*)img NavTitle:(NSString *)title {
imgRightImage = [[UIImageView alloc] initWithFrame:CGRectMake(275.0,2.0,40.0,40.0)];
imgRightImage.contentMode = UIViewContentModeScaleAspectFill;
imgRightImage.clipsToBounds = YES;
imgRightImage.layer.masksToBounds = YES;
// imgRightImage.layer.cornerRadius = 11.0;
// imgRightImage.layer.borderWidth = 0.5;
//[imgRightImage setImage:img];
[imgRightImage setBackgroundColor:[UIColor clearColor]];
[self addSubview:imgRightImage];
[imgRightImage release];
lbl = [[UILabel alloc] initWithFrame:CGRectMake(70.0, 7.0, 180, 30.0)];
// lbl.font = [UIFont fontWithName:#"Arial" size:20.0];
lbl.font = Arial16;
lbl.numberOfLines = 1;
lbl.tag = 11;
lbl.backgroundColor = [UIColor clearColor];
lbl.textColor = [UIColor blackColor];
lbl.textAlignment = UITextAlignmentCenter;
lbl.text = title;
[self addSubview:lbl];
}
-(void)setTitleImage:(UIImage*)img rightImage:(UIImage *)imgRight {
imgRightImage = [[UIImageView alloc] initWithFrame:CGRectMake(275.0,2.0,40.0,40.0)];
imgRightImage.contentMode = UIViewContentModeScaleAspectFill;
imgRightImage.clipsToBounds = YES;
imgRightImage.layer.masksToBounds = YES;
[imgRightImage setImage:imgRight];
[imgRightImage setBackgroundColor:[UIColor clearColor]];
[self addSubview:imgRightImage];
[imgRightImage release];
imgTitle = [[UIImageView alloc] initWithFrame:CGRectMake(68.0,3.0,200.0,38.0)];
imgTitle.contentMode = UIViewContentModeScaleToFill;
[imgTitle setBackgroundColor:[UIColor clearColor]];
[imgTitle setImage:img];
[self addSubview:imgTitle];
[imgTitle release];
}
-(void)onclickLeft:(id)sender{
NSLog(#">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>Nav LeftClick");
[delegate btnleft_clicked:self];
}
-(void)onclickBack:(id)sender {
NSLog(#">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>Nav BackClick");
[delegate popViewController:self];
}
#pragma mark -
#pragma mark
- (void)dealloc {
//[array release];
[super dealloc];
}
#end
and after that create Delegate class file like bellow..
#class CustomNavBar;
#protocol CustomNavBarDelegate
#required
- (void)popViewController:(CustomNavBar *)navBar;
-(void)btnleft_clicked:(CustomNavBar *)navBar1;
#end
after use this code in your classes ..For Example..
in .h file import it and then use like bellow..
#import "CustomNavBarDelegate.h"
#class CustomNavBar;
#interface ViewController : UIViewController<CustomNavBarDelegate>
{
CustomNavBar *navBar;
}
- (void)popViewController:(CustomNavBar *)navBar1;
#end
and in .m file define that delegate method and create and add navigation like bellow...
- (void)viewDidLoad
{
navBar = [[CustomNavBar alloc] initWithFrame:CGRectMake(0, 0, 322, 44)];
[navBar setDelegate:self];
[self.view addSubview:navBar];
[navBar setImage:[UIImage imageNamed:#"yourImageName"] NavTitle:#"yourTitle"];
}
- (void)popViewController:(CustomNavBar *)navBar1 {
// NSLog(#">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> delegate called");
[self.navigationController popViewControllerAnimated:YES];
}
-(void)btnleft_clicked:(CustomNavBar *)navBar1{
NSLog(#"\n\n btn Left Clicked InviteFriendsView");
}
NOTE: This is just an example , here implement your logic with your requirement.
Here you can also add 3rd button in middle and also define the method for called it in Delegate and also in this another .m file..
i hope this helpful for you...
i did this ,see below code
//IvisionApps Button
UIButton *ivisionButton= [UIButton buttonWithType:UIButtonTypeCustom];
UIImage *ivisionButtonImage = [UIImage imageNamed:#"ivisionapps"];
UIImage *ivisionButtonImagePressed = [UIImage imageNamed:#"ivisionappsSelected"];
[ivisionButton setBackgroundImage:ivisionButtonImage forState:UIControlStateNormal];
[ivisionButton setBackgroundImage:ivisionButtonImagePressed forState:UIControlStateHighlighted];
[ivisionButton addTarget:self action:#selector(goIVisionApp) forControlEvents:UIControlEventTouchUpInside];
ivisionButton.frame = CGRectMake(-8, -20, 106, 38);
UIView *backButtonView = [[UIView alloc] initWithFrame:CGRectMake(-8, -20, 106, 38)];
backButtonView.bounds = CGRectOffset(backButtonView.bounds, -14, -7);
[backButtonView addSubview:ivisionButton];
UIBarButtonItem *backBarButton = [[UIBarButtonItem alloc] initWithCustomView:backButtonView];
self.navigationItem.rightBarButtonItem = backBarButton;
//Auxilaries Button
UIButton *auxiliariesButton= [UIButton buttonWithType:UIButtonTypeCustom];
UIImage *auxiliariesButtonImage = [UIImage imageNamed:#"Auxiliaries"];
UIImage *auxiliariesButtonImagePressed = [UIImage imageNamed:#"AuxiliariesSelected"];
[auxiliariesButton setBackgroundImage:auxiliariesButtonImage forState:UIControlStateNormal];
[auxiliariesButton setBackgroundImage:auxiliariesButtonImagePressed forState:UIControlStateHighlighted];
[auxiliariesButton addTarget:self action:#selector(goBack) forControlEvents:UIControlEventTouchUpInside];
auxiliariesButton.frame = CGRectMake(-19, -20, 106, 38);
UIView *auxiliariesButtonView = [[UIView alloc] initWithFrame:CGRectMake(-19, -20, 106, 38)];
auxiliariesButtonView.bounds = CGRectOffset(auxiliariesButtonView.bounds, -14, -7);
[auxiliariesButtonView addSubview:auxiliariesButton];
UIBarButtonItem *auxiliariesBarButton = [[UIBarButtonItem alloc] initWithCustomView:auxiliariesButtonView];
self.navigationItem.leftBarButtonItem = auxiliariesBarButton;
//hide backBarButton of NavigationItem
[self.navigationItem setHidesBackButton:YES];

How to implement PSCollectionView in iphone App?

In my application i have six button which need to be place on pscollectionview.
i have placed pscollectionview in app but i am not abel to use the datasource and delegate method?
can any one come up with this example will be helpfull?
following is my code:
- (void)viewDidLoad
{
self.collectionView = [[PSCollectionView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)];
self.collectionView.delegate = self;
self.collectionView.collectionViewDelegate = self;
self.collectionView.collectionViewDataSource = self;
self.collectionView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
[self.view addSubview:self.collectionView];
UIButton *btn1 = [UIButton buttonWithType:UIButtonTypeRoundedRect];
btn1.frame = CGRectMake(10, 30, 140, 130);
[self.collectionView addSubview:btn1];
UIButton *btn2 = [UIButton buttonWithType:UIButtonTypeRoundedRect];
btn2.frame = CGRectMake(170, 30, 140, 130);
[self.collectionView addSubview:btn2];
UIButton *btn3 = [UIButton buttonWithType:UIButtonTypeRoundedRect];
btn3.frame = CGRectMake(10, 180, 140, 130);
[self.collectionView addSubview:btn3];
UIButton *btn4 = [UIButton buttonWithType:UIButtonTypeRoundedRect];
btn4.frame = CGRectMake(170, 180, 140, 130);
[self.collectionView addSubview:btn4];
UILabel *loadingLabel = [[UILabel alloc]initWithFrame:CGRectMake(20, 100, 50, 20)];
loadingLabel.text = #"Loading...";
loadingLabel.textAlignment = UITextAlignmentCenter;
[self.collectionView addSubview:loadingLabel];
//self.collectionView.loadingView = loadingLabel;
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
}
- (NSInteger)numberOfViewsInCollectionView:(PSCollectionView *)collectionView {
return 1;
}
- (PSCollectionViewCell *)collectionView:(PSCollectionView *)collectionView viewAtIndex:(NSInteger)index {
}
- (CGFloat)heightForViewAtIndex:(NSInteger)index {
NSDictionary *item = [self.items objectAtIndex:index];
return [NDACCustomView heightForViewWithObject:item inColumnWidth:self.collectionView.colWidth];
}
- (void)collectionView:(PSCollectionView *)collectionView didSelectView:(PSCollectionViewCell *)view atIndex:(NSInteger)index {
// NSDictionary *item = [self.items objectAtIndex:index];
// You can do something when the user taps on a collectionViewCell here
}
PSCollectionView's delegate method has been updated after this question posted.
Visit https://github.com/ptshih/PSCollectionView.
Just share my code here.
- (void)viewDidLoad
{
self.collectionView = [[PSCollectionView alloc] initWithFrame:self.view.frame];
self.collectionView.delegate = self; // This is for UIScrollViewDelegate
self.collectionView.collectionViewDelegate = self;
self.collectionView.collectionViewDataSource = self;
self.collectionView.backgroundColor = [UIColor clearColor];
self.collectionView.autoresizingMask = ~UIViewAutoresizingNone;
// Specify number of columns for both iPhone and iPad
self.collectionView.numColsPortrait = 3;
self.collectionView.numColsLandscape = 4;
self.view.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:#"bg.png"]];
[self.view addSubview:_collectionView];
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (Class)collectionView:(PSCollectionView *)collectionView cellClassForRowAtIndex:(NSInteger)index {
return [MCCollectionViewCell class];
}
- (NSInteger)numberOfRowsInCollectionView:(PSCollectionView *)collectionView {
return 30;
}
- (UIView *)collectionView:(PSCollectionView *)collectionView cellForRowAtIndex:(NSInteger)index {
MCCollectionViewCell *cell = (MCCollectionViewCell *)[_collectionView dequeueReusableViewForClass:[MCCollectionViewCell class]];
if (cell == nil) {
NSArray *nib = [[NSBundle mainBundle] loadNibNamed:#"MCCollectionViewCell" owner:self options:nil];
cell = (MCCollectionViewCell *)[nib objectAtIndex:0];
}
NSOperationQueue *imageQueue = [[NSOperationQueue alloc] init];
[imageQueue addOperation:[NSBlockOperation blockOperationWithBlock:^{
int r = arc4random() % 3;
NSString *imageName = [NSString stringWithFormat:#"image%d", r];
UIImage *image = [UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:imageName ofType:#"jpg"]];
[[NSOperationQueue mainQueue] addOperation:[NSBlockOperation blockOperationWithBlock:^{
cell.previewImageView.image = image;
cell.titleLabel.text = imageName;
}]];
}]];
return cell;
}
- (CGFloat)collectionView:(PSCollectionView *)collectionView heightForRowAtIndex:(NSInteger)index {
return 290.0;
}

UITableView tableFooterView not appearing

I have a UITableView to which I add a tableFooterView, for some reason the tableFooterView is not appearing?
How I add my tableFooterView
I add the tableFooterView in a connectionDidFinishLoading method after reloading the tableview data.
So what I do is
[controls reloadData];
UIView *myFooterView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 100)];
if(self.item.canRunOnDemand)
{
UIButton *buttonRunWorkflow = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[buttonRunWorkflow addTarget:self action:#selector(runWorkflow:) forControlEvents:UIControlEventTouchDown];
[buttonRunWorkflow setTitle:#"Run Now" forState:UIControlStateNormal];
buttonRunWorkflow.frame = CGRectMake(15, 5, 290, 44);
buttonRunWorkflow.backgroundColor = [UIColor clearColor];
[buttonRunWorkflow setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
[myFooterView addSubview:buttonRunWorkflow];
}
if(item.canRunAlways)
{
UILabel *canRunAlwaysLabel = [[UILabel alloc] initWithFrame:CGRectMake(15, 46, 100, 44)];
canRunAlwaysLabel.backgroundColor = [UIColor clearColor];
canRunAlwaysLabel.text = #"Run Always:";
UISwitch *canRunAlways = [[UISwitch alloc] initWithFrame:CGRectMake(115, 56, 100, 44)];
[canRunAlways addTarget:self action:#selector(canRunAlwaysChanged:) forControlEvents:UIControlEventValueChanged];
[myFooterView addSubview:canRunAlways];
[myFooterView addSubview:canRunAlwaysLabel];
[canRunAlwaysLabel release];
[canRunAlways release];
}
[myFooterView release];
[controls.tableFooterView addSubview:myFooterView];
Footer view height
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
return 100;
}
I have also tried this:
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
if(section == [[fields objectAtIndex:section] count] - 1)
{
return 100;
}
else {
return 0;
}
}
-(UIView*) tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section
{
if(section == [[fields objectAtIndex:section] count] - 1)
{
UIView *myFooterView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 80)];
if(self.item.canRunOnDemand)
{
UIButton *buttonRunWorkflow = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[buttonRunWorkflow addTarget:self action:#selector(runWorkflow:) forControlEvents:UIControlEventTouchDown];
[buttonRunWorkflow setTitle:#"Run Now" forState:UIControlStateNormal];
buttonRunWorkflow.frame = CGRectMake(15, 5, 290, 44);
buttonRunWorkflow.backgroundColor = [UIColor clearColor];
[buttonRunWorkflow setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
[myFooterView addSubview:buttonRunWorkflow];
}
if(item.canRunAlways)
{
UILabel *canRunAlwaysLabel = [[UILabel alloc] initWithFrame:CGRectMake(15, 46, 100, 44)];
canRunAlwaysLabel.backgroundColor = [UIColor clearColor];
canRunAlwaysLabel.text = #"Run Always:";
UISwitch *canRunAlways = [[UISwitch alloc] initWithFrame:CGRectMake(115, 56, 100, 44)];
[canRunAlways addTarget:self action:#selector(canRunAlwaysChanged:) forControlEvents:UIControlEventValueChanged];
[myFooterView addSubview:canRunAlways];
[myFooterView addSubview:canRunAlwaysLabel];
[canRunAlwaysLabel release];
[canRunAlways release];
}
return myFooterView;
}
else {
return nil;
}
}
By looking at the header file of UITableView.h we see the declaration of property tableFooterView like this:
#property(nonatomic,retain) UIView *tableFooterView; // accessory view below content. default is nil. not to be confused with section footer
so the default property is nil. That's why you can't add another UIView to nil UIView.
You should do something like this:
controls.tableFooterView = myFooterView;
Are you trying to add footer view in table view?
If you are trying for that Implement your footer view in this method
- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section
This may helps you.Thanks!!

cant able to add click event in UIbutton

I am creating custom view which include two labels and One button. I cant get the button click. I am also sending the code which is below.
.h File
#import <UIKit/UIKit.h>
#interface customDeleteButton : UITableViewCell {
IBOutlet UILabel *lbl;
IBOutlet UILabel *lbl1;
}
#property(nonatomic,retain)IBOutlet UILabel *lbl;
#property(nonatomic,retain)IBOutlet UILabel *lbl1;
-(IBAction)btnClick:(id)sender;
#end
.m File
#import "customDeleteButton.h"
#implementation customDeleteButton
#synthesize lbl,lbl1;
- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
if (self) {
// Initialization code.
lbl = [[UILabel alloc] initWithFrame:CGRectMake(50, 10, 200, 20)];
lbl1 = [[UILabel alloc] initWithFrame:CGRectMake(260, 10, 200, 20)];
[self addSubview:lbl];
[self addSubview:lbl1];
}
return self;
}
- (void)willTransitionToState:(UITableViewCellStateMask)state{
[super willTransitionToState:state];
if ((state & UITableViewCellStateShowingDeleteConfirmationMask) == UITableViewCellStateShowingDeleteConfirmationMask) {
for (UIView *subview in self.subviews) {
if ([NSStringFromClass([subview class]) isEqualToString:#"UITableViewCellDeleteConfirmationControl"]) {
UIButton *btn;
btn=[[UIButton alloc]initWithFrame:CGRectMake(0, 0, 64, 33)];
[btn addTarget:subview action:#selector(btnClick:) forControlEvents:UIControlEventTouchUpInside];
[btn setImage:[UIImage imageNamed:#"delete.png"] forState:UIControlStateNormal];
btn.userInteractionEnabled=YES;
btn.frame=CGRectMake(0,0,64,33);
//UIImageView *deleteBtn = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, 64, 33)];
//[deleteBtn setImage:[UIImage imageNamed:#"delete.png"]];
[[subview.subviews objectAtIndex:0] addSubview:btn];
//[deleteBtn release];
}
}
}
}
-(void)btnClick:(id)sender
{
NSLog(#"HTllo");
}
- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
[super setSelected:selected animated:animated];
// Configure the view for the selected state.
}
- (void)dealloc {
[super dealloc];
}
You need to set the target to your main view not the subviews, when you create it:
[btn addTarget:self action:#selector(btnClick:) forControlEvents:UIControlEventTouchUpInside];
And also - do you realize what you're doing in your code?
In a function willTransitionToState you are adding UIButtons to subviews of the button itself?
Is this really what you want to do? Is the function willTransitionToState even called?
Please try setting UIButton Type while initializing and change the target to self.
UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
btn.frame = CGRectMake(0, 0, 64, 33);
[btn addTarget:self action:#selector(btnClick:) forControlEvents:UIControlEventTouchUpInside];
[btn setImage:[UIImage imageNamed:#"delete.png"] forState:UIControlStateNormal];
btn.userInteractionEnabled=YES;
[[subview.subviews objectAtIndex:0] addSubview:btn];
Change the target to self instead of subview.
UIButton *btn;
btn=[[UIButton alloc]initWithFrame:CGRectMake(0, 0, 64, 33)];
[btn addTarget:self action:#selector(btnClick:) forControlEvents:UIControlEventTouchUpInside];
[btn setImage:[UIImage imageNamed:#"delete.png"] forState:UIControlStateNormal];
btn.userInteractionEnabled=YES;
btn.frame=CGRectMake(0,0,64,33);
//UIImageView *deleteBtn = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, 64, 33)];
//[deleteBtn setImage:[UIImage imageNamed:#"delete.png"]];
[[subview.subviews objectAtIndex:0] addSubview:btn];
//[deleteBtn release];
newString = [oldString stringByReplacingOccurrencesOfString:#"-"withString:#" ' "];

Forward event from custom UIControl subclass

My custom subclass extend UIControl (EditableImageView)
Basically it contains 3 subviews:
UIButton (UIButtonTypeCustom)
UIImageView
UILabel
All the class works great but now I want to connect some events generated from this class to another. In particular when the button is pressed I want to forward the event to the owner viewcontroller so that I can handle the event. The problem is that I can't figure out how to implement this behaviour.
Within EditableImageView I can catch the touch event using [button addTarget:self action:#selector(buttonPressed:) forControlEvents:UIControlEventTouchUpInside] but I don't know how to forward it inside of the buttonPressed selector.
I also tried to implement touchesBegan but it seems never called...
I'd like to capture the button press event from the viewcontroller in this way:
- (void)viewDidLoad {
[super viewDidLoad];
self.imageButton = [[EditableImageView alloc] initWithFrame:CGRectMake(50.0f, 50.0f, 80.0f, 80.0f)];
[imageButton addTarget:self action:#selector(buttonPressed:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:imageButton];
[imageButton setEditing:NO];
}
This is my UIControl subclass initialization method:
- (id)initWithFrame:(CGRect)frame {
if (self = [super initWithFrame:frame]) {
[self setBackgroundColor:[UIColor clearColor]];
button = [UIButton buttonWithType:UIButtonTypeCustom];
button.frame = CGRectMake(0.0f, 0.0f, frame.size.width, frame.size.height);
[button setImage:[UIImage imageNamed:#"nene_70x70.png"] forState:UIControlStateNormal];
[button addTarget:self action:#selector(buttonPressed:) forControlEvents:UIControlEventTouchUpInside];
[self addSubview:button];
transparentLabelBackground = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"editLabelBackground.png"]];
transparentLabelBackground.hidden = YES;
[self addSubview:transparentLabelBackground];
// create edit status label
editLabel = [[UILabel alloc] initWithFrame:CGRectZero];
editLabel.hidden = YES;
editLabel.userInteractionEnabled = NO; // without this assignment the button will not be clickable
editLabel.textColor = [UIColor whiteColor];
editLabel.backgroundColor = [UIColor clearColor];
editLabel.textAlignment = UITextAlignmentLeft;
UIFont *labelFont = [UIFont systemFontOfSize:16.0];
editLabel.font = labelFont;
editLabel.text = #"edit";
labelSize = [#"edit" sizeWithFont:labelFont];
[self addSubview:editLabel];
}
return self;
}
Thanks.
I solved in this way:
EditableImageView.h:
#interface EditableImageView : UIControl {
UIButton *button;
UIImageView *transparentLabelBackground;
UILabel *editLabel;
CGSize labelSize;
BOOL editing;
}
#property (nonatomic, retain) UIButton *button;
#property (nonatomic, retain) UILabel *editLabel;
#property (nonatomic, retain) UIImageView *transparentLabelBackground;
#property (nonatomic, getter=isEditing) BOOL editing;
- (void)buttonPressed:(id)sender;
#end
EditableImageView.m:
(id)initWithFrame:(CGRect)frame {
if (self = [super initWithFrame:frame]) {
[self setBackgroundColor:[UIColor clearColor]];
button = [UIButton buttonWithType:UIButtonTypeCustom];
button.frame = CGRectMake(0.0f, 0.0f, frame.size.width, frame.size.height);
[button setImage:[UIImage imageNamed:#"nene_70x70.png"] forState:UIControlStateNormal];
[button addTarget:self action:#selector(buttonPressed:) forControlEvents:UIControlEventTouchUpInside];
.......
}
}
- (void)buttonPressed:(id)sender {
[self sendActionsForControlEvents:UIControlEventTouchUpInside];
}
MyController.m:
- (void)viewDidLoad {
[super viewDidLoad];
self.imageButton = [[EditableImageView alloc] initWithFrame:CGRectMake(50.0f, 50.0f, 80.0f, 80.0f)];
[imageButton addTarget:self action:#selector(buttonPressed:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:imageButton];
}
- (void)buttonPressed:(id)sender {
NSLog(#"buttonPressed!");
}