How to customize a UITableViewCell so the text doesn't overlap - iphone

Any idea how to move the UITableViewCell text over so that it doesn't overlap the small image on the left hand side?
here's a screenshot:
thanks for any help
here's my TableCell code:
- (UITableViewCell *)tableView:(UITableView *)tableView1 cellForRowAtIndexPath:(NSIndexPath *)indexPath{
UILabel* nameLb;
UILabel* detailsLb;
UITableViewCell *cell = [tableView1 dequeueReusableCellWithIdentifier:#"log-cell"];
if (cell == nil){
cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:#"log-cell"] autorelease];
UIImage* img = [ImgUtil image:[NSString stringWithFormat:#"butList3_%d.png", [[SelectCategory instance] select] + 1 ]];
UIImageView* bgImage = [[[UIImageView alloc] initWithImage:img ]autorelease];
[cell addSubview:bgImage ];
[cell sendSubviewToBack:bgImage ];
nameLb = [[[UILabel alloc] initWithFrame:CGRectMake(5, 5, 250, 40)] autorelease];
[nameLb setNumberOfLines:4];
[nameLb setBackgroundColor:[UIColor clearColor]];
[nameLb setTextColor:[UIColor whiteColor]];
[nameLb setTag:NAME_TEXT];
[nameLb setFont:[UIFont fontWithName:#"Helvetica-Bold" size:15]];
[nameLb setMinimumFontSize:10];
[cell addSubview:nameLb];
//add UIImage
cell.imageView.image = [UIImage imageNamed:#"shop.png"];
detailsLb = [[[UILabel alloc] initWithFrame:CGRectMake(5, 45, 250, 20)] autorelease];
[detailsLb setBackgroundColor:[UIColor clearColor]];
[detailsLb setTextColor:[UIColor whiteColor]];
[detailsLb setTag:DETAILS_TEXT];
[detailsLb setFont:[UIFont fontWithName:#"Helvetica" size:12]];
[cell addSubview:detailsLb];
}else {
nameLb = (UILabel*)[cell viewWithTag:NAME_TEXT];
detailsLb = (UILabel*)[cell viewWithTag:DETAILS_TEXT];
}
ObjectInfo* obj = [myList objectAtIndex:indexPath.row ] ;
nameLb.text = [obj name];
// cell.textLabel.textColor = [UIColor whiteColor];
// cell.textLabel.font = [UIFont boldSystemFontOfSize:17];
double distance = [ProjUtil getDistanceWithLat1:currentLocation.latitude long1:currentLocation.longitude lat2:obj.location.latitude long2:obj.location.longitude];
detailsLb.text = [NSString stringWithFormat:#"%.2f miles", [ProjUtil kmToMi:distance]];
// cell.detailTextLabel.textColor = [UIColor whiteColor];
// cell.detailTextLabel.font = [UIFont systemFontOfSize:15];
[cell setSelectionStyle:UITableViewCellSelectionStyleNone];
[cell setBackgroundColor:[UIColor clearColor]];
///[cell setAccessoryType:UITableViewCellAccessoryDisclosureIndicator];
return cell;
}

When you create the label you are positioning them with this line:
detailsLb = [[[UILabel alloc] initWithFrame:CGRectMake(5, 45, 250, 20)] autorelease];
This sets the frame of your label. CGRectMake is a function that gives you a CGRect, in this case with origin (5,45), width 250 and height 20.
If you want to move the labels over to the right, increase the x value of your frame (the 5 in the CGRectMake call above).

(Givin one of my code ,widout modifying sry)
I am not able to see the image but you should create a custom cell and you can set the frames of your controls of cell in method:-
-(void)layoutSubviews
{
}
sample code :-
#import <UIKit/UIKit.h>
#interface CustomizedCellProductDetails : UITableViewCell {
UILabel *sNO;
UILabel *abcWine;
UILabel *redWine;
UILabel *two;
UILabel *hundred;
UILabel *fourTwo;
UILabel *twoOne;
UIImageView *imgView;
UILabel *itemNo;
UILabel *itemName;
UILabel *itemDesc;
UILabel *department;
UILabel *qtyAvailable;
UIButton *check;
}
#property (nonatomic , retain) UILabel *sNO;
#property (nonatomic , retain) UILabel *abcWine;
#property (nonatomic , retain) UILabel *redWine;
#property (nonatomic , retain) UILabel *two;
#property (nonatomic , retain) UILabel *hundred;
#property (nonatomic , retain) UILabel *fourTwo;
#property (nonatomic , retain) UILabel *twoOne;
#property (nonatomic , retain) UIImageView *imgView;
#property (nonatomic , retain) UILabel *itemNo;
#property (nonatomic , retain) UILabel *itemName;
#property (nonatomic , retain) UILabel *itemDesc;
#property (nonatomic , retain) UILabel *department;
#property (nonatomic , retain) UILabel *qtyAvailable;
#property (nonatomic , retain) UIButton *check;
-(void) clicked;
#end
#import "CustomizedCellProductDetails.h"
#implementation CustomizedCellProductDetails
#synthesize sNO,abcWine,redWine,two,hundred,fourTwo,twoOne,imgView,itemNo,itemName,itemDesc,department,qtyAvailable,check;
- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
if (self) {
sNO=[[UILabel alloc] init];
abcWine=[[UILabel alloc] init];
redWine=[[UILabel alloc] init];
two=[[UILabel alloc] init];
hundred=[[UILabel alloc] init];
fourTwo=[[UILabel alloc] init];
twoOne=[[UILabel alloc] init];
imgView=[[UIImageView alloc] init];
itemNo=[[UILabel alloc] init];
itemName=[[UILabel alloc] init];
itemDesc=[[UILabel alloc] init];
department=[[UILabel alloc]init];
qtyAvailable=[[UILabel alloc] init];
check=[UIButton buttonWithType:UIButtonTypeCustom];
[check addTarget:self action:#selector(clicked) forControlEvents:UIControlEventTouchUpInside];
[check setTitle:#"Check" forState:UIControlStateNormal];
[self.contentView addSubview:sNO];
[self.contentView addSubview:abcWine];
[self.contentView addSubview:redWine];
[self.contentView addSubview:two];
[self.contentView addSubview:hundred];
[self.contentView addSubview:fourTwo];
[self.contentView addSubview:twoOne];
[self.contentView addSubview:itemNo];
[self.contentView addSubview:itemName];
[self.contentView addSubview:itemDesc];
[self.contentView addSubview:department];
[self.contentView addSubview:qtyAvailable];
[self.contentView addSubview:check];
}
return self;
}
- (void)layoutSubviews {
[super layoutSubviews];
CGRect contentRect = self.contentView.bounds;
CGFloat boundsX = contentRect.origin.x;
CGRect frame;
frame=CGRectMake(boundsX+10, 0, 50, 40);
sNO.frame = frame;
frame= CGRectMake(boundsX+70 ,0, 150, 40);
abcWine.frame = frame;
frame= CGRectMake(boundsX+230 ,0, 150, 40);
redWine.frame = frame;
frame= CGRectMake(boundsX+390 ,0, 50, 40);
two.frame = frame;
frame= CGRectMake(boundsX+450 ,0, 50, 40);
hundred.frame = frame;
frame= CGRectMake(boundsX+510 ,0, 50, 40);
fourTwo.frame = frame;
frame= CGRectMake(boundsX+570 ,0, 50, 40);
twoOne.frame = frame;
/************************ ***********************/
frame= CGRectMake(boundsX+10 ,50, 100, 200);
imgView.frame = frame;
}
-(void) clicked
{
}
- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
[super setSelected:selected animated:animated];
// Configure the view for the selected state.
}
- (void)dealloc {
[super dealloc];
}
#end
also change the height in cellForRowAtIndexPath delegate method of uitableviewcell.

When generating your cell in cellForRowAtIndexPath, update your creation of the labels with this code to set the frames correctly
nameLb = [[[UILabel alloc] initWithFrame:CGRectMake(bgImage.frame.origin.x + bgImage.frame.size.width + 5, 5, 250, 40)] autorelease];
detailsLb = [[[UILabel alloc] initWithFrame:CGRectMake(bgImage.frame.origin.x + bgImage.frame.size.width + 5, 5, 250, 40)] autorelease];
That will dynamically create your custom rows text to the right of the image.

Related

Create new UI elements programmatically

I'd like to be able to create elements (like UIViews) when for example the user touches a button
NSMutableString *myVar = [NSMutableString stringWithFormat:#"_view%i", num];
UIView * newView = [self valueForKey:myVar];
but without adding all the
UIView * _view1;
UIView * _view2;
...
in the .h file (if only this is possible..)
You can use an NSMutableArray to hold them. Each time you create a new view just add it to the array.
Here's sample code that should do what you want.
#interface MyViewController ()
#property (strong, nonatomic) NSMutableArray *listChildViews;
#end
#implementation MyViewController
- (void)viewDidLoad
{
[super viewDidLoad];
self.listChildViews = [[NSMutableArray alloc] init];
}
- (IBAction)addChildViewTapped:(id)sender
{
int numChildViews = [self.listChildViews count];
++numChildViews;
// add new child view
NSString *labelForNewView = [NSString stringWithFormat:#"view %d", numChildViews];
CGFloat labelHeight = 28.0;
UILabel *childView = [[UILabel alloc] initWithFrame:CGRectMake(10, numChildViews*labelHeight, 120.0, labelHeight)];
childView.text = labelForNewView;
[self.listChildViews addObject:childView];
[self.view addSubview:childView];
}
#end
Here is code implementation of pauls answer:
- (IBAction)userTouchedButton:(id)sender{
for (int i = 0; i < 100; i++) {
UIView *view = [[UIView alloc] initWithFrame:CGRectMake(x, y, width, height)];
[view setBackgroundColor:[UIColor redColor]];//to distinguish theseViews from other views
[view setTag:i];//to identified it later
[_array insertObject:view atIndex:i];// globleMutble array
[self.view addSubview:view];
}
}
You need not add your views in the .h file. Just instantiate before and where you add them
-(void) addButton
{
UIView *view = [self view];
UIButton *button1 = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[button1 setTitle:#"My Button" forState:UIControlStateNormal];
UILabel *myLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 50, 0, 0)];
[myLabel setText:#"My Label"];
[button1 sizeToFit];
[myLabel sizeToFit];
[view addSubview:button1];
[view addSubview:myLabel];
}

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];

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

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)

received memory warning. level 1

I am new to iPhone development. I am creating an application in which i need to create buttons and labels programmatically.
So I am doing this in .h file
#interface FirstQuizViewControlleriPad : UIViewController{
IBOutlet UIButton *startgame;
UILabel *theQuestion;
IBOutlet UIScrollView *scrollview;
UILabel *Question;
UILabel *Description;
UIView *ContainerView;
UILabel *Challengetext;
UIImageView *img1;
UIImageView *img2;
UIImageView *background;
IBOutlet UIButton *reviewbtn;
UILabel *ans1;
UILabel *ans2;
UILabel *ans3;
UILabel *ans4;
UIButton *button;
UIButton *button2;
UIButton *button3;
UIButton *button4;
UILabel *rightans;
UILabel *theScore;
UIButton *nextbtn;
UIButton *backbtn;
NSString *setchallange;
AVAudioPlayer *AVback;
AVAudioPlayer *AVstart;
NSArray *theQuiz;
NSInteger questionNumber;
NSInteger rightAnswer;
NSInteger myScore;
NSInteger finalScoreipad1;
BOOL restartGame;
id QuestionReview;
}
#property (retain , nonatomic) IBOutlet UIButton *startgame;
#property (retain , nonatomic) NSArray *theQuiz;
#property (retain , nonatomic) IBOutlet UILabel *theScore;
#property (retain , nonatomic) IBOutlet UIButton *nextbtn;
#property (retain , nonatomic) IBOutlet UIScrollView *scrollview;
#property (retain , nonatomic) IBOutlet UIButton *backbtn;
#property (retain , nonatomic) IBOutlet UILabel *Challengetext;
#property (retain , nonatomic) IBOutlet UIImageView *img1;
#property (retain , nonatomic) IBOutlet UIImageView *img2;
#property (retain , nonatomic) id QuestionReview;
#property (retain , nonatomic) IBOutlet UIButton *reviewbtn;
#property (retain ,nonatomic) IBOutlet UIButton *button;
#property (retain ,nonatomic) IBOutlet UIButton *button2;
#property (retain ,nonatomic) IBOutlet UIButton *button3;
#property (retain ,nonatomic) IBOutlet UIButton *button4;
#property (retain ,nonatomic) IBOutlet UILabel *ans1;
#property (retain ,nonatomic) IBOutlet UILabel *ans2;
#property (retain ,nonatomic) IBOutlet UILabel *ans3;
#property (retain ,nonatomic) IBOutlet UILabel *ans4;
#property (retain , nonatomic) IBOutlet UILabel *Question;
#property (retain , nonatomic) NSString *setchallange;
-(void)askQuestion;
in .m file
questionNumber = questionNumber + 1;
NSInteger row = 0;
if(questionNumber == 1)
{
row = questionNumber - 1;
}
else
{
row = ((questionNumber - 1) * 10);
}
NSString *selected = [theQuiz objectAtIndex:row];
NSString *activeQuestion = [[NSString alloc] initWithFormat:#"%#", selected];
QuestionReview = activeQuestion;
//lbl ref to btn1
ans1 = [[[UILabel alloc] initWithFrame:CGRectMake(36, 292, 697, 100)] autorelease];
ans1.numberOfLines = 0;
ans1.font = [UIFont systemFontOfSize:26];
ans1.text = [theQuiz objectAtIndex:row+1];
ans1.lineBreakMode = UILineBreakModeWordWrap;
[ans1 sizeToFit];
[self.view addSubview:ans1];
//lbl ref to btn2
ans2 = [[[UILabel alloc] initWithFrame:CGRectMake(36, 312 + ans1.frame.size.height , 697, 100)] autorelease];
ans2.numberOfLines = 0;
ans2.font = [UIFont systemFontOfSize:26];
ans2.text = [theQuiz objectAtIndex:row+2];
ans2.lineBreakMode = UILineBreakModeWordWrap;
[ans2 sizeToFit];
[self.view addSubview:ans2];
//lbl ref to btn3
ans3 = [[[UILabel alloc] initWithFrame:CGRectMake(36 ,332 + ans1.frame.size.height + ans2.frame.size.height , 697 , 100)] autorelease];
ans3.numberOfLines = 0;
ans3.font = [UIFont systemFontOfSize:26];
ans3.text = [theQuiz objectAtIndex:row+3] ;
ans3.lineBreakMode = UILineBreakModeWordWrap;
[ans3 sizeToFit];
[self.view addSubview:ans3];
//lbl ref to btn4
ans4 = [[[UILabel alloc] initWithFrame:CGRectMake(36, 352 + ans1.frame.size.height + ans2.frame.size.height + ans3.frame.size.height, 697, 100)] autorelease];
ans4.numberOfLines = 0;
ans4.font = [UIFont systemFontOfSize:26];
ans4.text = [theQuiz objectAtIndex:row+4];
ans4.lineBreakMode = UILineBreakModeWordWrap;
[ans4 sizeToFit];
[self.view addSubview:ans4];
rightAnswer = [[theQuiz objectAtIndex:row+5] intValue];
Question = [[[UILabel alloc] initWithFrame:CGRectMake(22, 130, 725, 160)] autorelease];
Question.numberOfLines = 0;
//Question.font = [UIFont systemFontOfSize:27];
Question.text = activeQuestion;
Question.lineBreakMode = UILineBreakModeWordWrap;
[Question setFont:[UIFont fontWithName:#"Futura" size:26]];
Question.backgroundColor = [UIColor clearColor];
[Question sizeToFit];
[self.view addSubview:Question];
//For 1st Option.
button = [UIButton buttonWithType:UIButtonTypeCustom];
[button addTarget:self
action:#selector(buttonOne)
forControlEvents:UIControlEventTouchDown];
button.titleLabel.lineBreakMode = UILineBreakModeWordWrap;
button.titleLabel.font = [UIFont systemFontOfSize:24];
[button setTitle:[theQuiz objectAtIndex:row+1] forState:UIControlStateNormal];
[button setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
button.frame = CGRectMake(34, 200 + Question.frame.size.height , 700, ans1.frame.size.height + 20);
[self.view addSubview:button];
button.layer.borderWidth = 3;
button.layer.borderColor = [[UIColor purpleColor ] CGColor];
button.layer.cornerRadius = 10.0;
[button setBackgroundImage:[UIImage imageNamed:#"buttun.png"] forState:UIControlStateNormal];
button.titleEdgeInsets = UIEdgeInsetsMake(5, 5, 5, 5);
//For 2nd Option.
button2 = [UIButton buttonWithType:UIButtonTypeCustom];
[button2 addTarget:self
action:#selector(buttonTwo)
forControlEvents:UIControlEventTouchDown];
[button2 setTitle:[theQuiz objectAtIndex:row+2] forState:UIControlStateNormal];
[button2 setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
button2.titleLabel.lineBreakMode = UILineBreakModeWordWrap;
button2.titleLabel.font = [UIFont systemFontOfSize:24];
button2.frame = CGRectMake(34, 230 + Question.frame.size.height + button.frame.size.height , 700, ans2.frame.size.height + 20);
button2.layer.borderWidth = 3;
button2.layer.borderColor = [[UIColor darkGrayColor ] CGColor];
button2.layer.cornerRadius = 10.0;
[self.view addSubview:button2];
[button2 setBackgroundImage:[UIImage imageNamed:#"buttun2.png"] forState:UIControlStateNormal];
button2.titleEdgeInsets = UIEdgeInsetsMake(5, 5, 5, 5);
//For 3rd Option.
button3 = [UIButton buttonWithType:UIButtonTypeCustom];
[button3 addTarget:self
action:#selector(buttonThree)
forControlEvents:UIControlEventTouchDown];
[button3 setTitle:[theQuiz objectAtIndex:row+3] forState:UIControlStateNormal];
[button3 setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
button3.titleLabel.lineBreakMode = UILineBreakModeWordWrap;
button3.titleLabel.font = [UIFont systemFontOfSize:24];
button3.frame = CGRectMake(34, 260 + Question.frame.size.height + button.frame.size.height + button2.frame.size.height , 700, ans3.frame.size.height + 20);
button3.layer.borderWidth = 3;
button3.layer.borderColor = [[UIColor purpleColor ] CGColor];
button3.layer.cornerRadius = 10.0;
[self.view addSubview:button3];
[button3 setBackgroundImage:[UIImage imageNamed:#"buttun.png"] forState:UIControlStateNormal];
button3.titleEdgeInsets = UIEdgeInsetsMake(5, 5, 5, 5);
//For 4th Option.
button4 = [UIButton buttonWithType:UIButtonTypeCustom];
[button4 addTarget:self
action:#selector(buttonFour)
forControlEvents:UIControlEventTouchDown];
[button4 setTitle:[theQuiz objectAtIndex:row+4] forState:UIControlStateNormal];
[button4 setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
button4.titleLabel.lineBreakMode = UILineBreakModeWordWrap;
button4.titleLabel.font = [UIFont systemFontOfSize:24];
button4.frame = CGRectMake(34, 290 + Question.frame.size.height + button.frame.size.height + button2.frame.size.height + button3.frame.size.height , 700, ans4.frame.size.height + 20);
button4.layer.borderWidth = 3;
button4.layer.borderColor = [[UIColor darkGrayColor ] CGColor];
button4.layer.cornerRadius = 10.0;
[self.view addSubview:button4];
[button4 setBackgroundImage:[UIImage imageNamed:#"buttun2.png"] forState:UIControlStateNormal];
button4.titleEdgeInsets = UIEdgeInsetsMake(5, 5, 5, 5);
NSString *strright = [theQuiz objectAtIndex:row+7];
NSString *strtext = [[NSString alloc] initWithFormat:#"%#" , strright];
rightans = [[UILabel alloc] initWithFrame:CGRectMake(50, 330, 650, 150)];
rightans.numberOfLines = 0;
rightans.text = strtext;
rightans.backgroundColor = [UIColor clearColor];
rightans.font = [UIFont systemFontOfSize:26];
[self.view addSubview:rightans];
[rightans sizeToFit];
[rightans setHidden:YES];
//Description.
NSString *des = [theQuiz objectAtIndex:row+6];
NSString *destext = [[NSString alloc] initWithFormat:#"\n> Explanation :\n%#\n\n" , des];
Description = [[[UILabel alloc] initWithFrame:CGRectMake(40, 400 + rightans.frame.size.height , 690, 150)] autorelease];
Description.numberOfLines = 0;
// Description.font = [UIFont systemFontOfSize:13.5];
Description.text = destext;
[Description setFont:[UIFont fontWithName:#"Futura" size:26]];
Description.lineBreakMode = UILineBreakModeWordWrap;
Description.backgroundColor = [UIColor clearColor];
[Description sizeToFit];
[self.view addSubview:Description];
[Description setHidden:YES];
ContainerView = [[UIView alloc] initWithFrame:CGRectMake(30, 400 + rightans.frame.size.height , 700 ,Description.frame.size.height)];
ContainerView.backgroundColor = [UIColor clearColor];
ContainerView.layer.cornerRadius = 10.0;
ContainerView.layer.borderColor = [[UIColor grayColor] CGColor];
ContainerView.layer.borderWidth = 3;
// [ContainerView addSubview:Description];
[self.view addSubview:ContainerView];
[ContainerView setHidden:YES];
//For Challenge Text
NSString *challange = [theQuiz objectAtIndex:row+8];
setchallange = [[NSString alloc] initWithFormat:#"%#" , challange];//Memory leak here
Challengetext.text = setchallange;
//Image For Every Challenge
background = [[[UIImageView alloc] initWithFrame:CGRectMake(15.0f, 15.0f, 130.0f, 110.0f)] autorelease];
[background setImage:[UIImage imageNamed:[theQuiz objectAtIndex: row + 9]]];
[self.view addSubview:background];
In dealloc function , I release all the labels and buttons and other objects that i have created but still i got this warning for memory leakage (received memory warning level 1 and some times level 2)
I searched where my memory is being leaked and it is leak at NSString setchallenge
Please help me out this..
Any help will be appreciated..
Thanks..
You're creating a new string with every call to setchallenge = [[NSString alloc]....
Should be:
//For Challenge Text
NSString *challange = [theQuiz objectAtIndex:row+8];
Challengetext.text = [NSString stringWithFormat:#"%#" , challange];
stringWithFormat returns an auto-released string, which will get deallocated next time the auto-release pool is drained.
joe
Since you alloced memory, you need to release it as well. Anytime you use alloc, copy or new, remember to always release. Its not a good idea to release everything only at dealloc. Release objects when you are done with them. For example:
setchallange = [[NSString alloc] initWithFormat:#"%#" , challange];//Memory leak here
Challengetext.text = setchallange;
[setchallange release]
Also checkout out the iOS memory management guide. I'm sure it will help.
May be for some object you allocating more than one times and release it once. just check it out I hope it will be solve.

UISliders lose target action when in UIView

I am trying to create a page of UISliders. The amount of sliders is dynamic.
I have a PageViewController class and when I put the UISliders in a subview of PageViewController the sliders work.
I would like to encapsulate the sliders into a UIView subclass so I can save their state/change coupled labels, etc. However when I do it the sliders stop working. I've tried a few things and am at the point to where I think I may just be missing a fundamental understanding that hopefully someone can point out.
This works (from PageViewController.m):
if ([[controlData type] isEqualToString:#"SliderInt"])
{
NSDictionary *data = controlData.data;
NSLog(#"build SliderInt %# %# %#", [controlData.data objectForKey:#"min"], [controlData.data objectForKey:#"max"], [controlData.data objectForKey:#"value"]);
CGRect frame = CGRectMake(startX, startY, 200.0, 22.00);
UISlider *slider = [[UISlider alloc] initWithFrame:frame];
[slider addTarget:self action:#selector(sliderAction:) forControlEvents:UIControlEventValueChanged];
slider.minimumValue = 0.0;
slider.maximumValue = 50.0;
slider.continuous = YES;
slider.value = 25.0;
CGRect labelFrame = CGRectMake(startX, startY+22.0, 100.0, 30.0);
UILabel *minLabel = [[UILabel alloc] initWithFrame:labelFrame];
[minLabel setFont:[UIFont systemFontOfSize:12]];
minLabel.textAlignment = UITextAlignmentLeft;
[minLabel setText:#"minLabel:"];
labelFrame = CGRectMake(startX+200.0, startY+22.0, 100.0, 30.0);
UILabel *maxLabel = [[UILabel alloc] initWithFrame:labelFrame];
[maxLabel setFont:[UIFont systemFontOfSize:12]];
maxLabel.textAlignment = UITextAlignmentLeft;
[maxLabel setText:#"maxLabel:"];
labelFrame = CGRectMake(startX+220.0, startY+0.0, 100.0, 30.0);
UILabel *valueLabel = [[UILabel alloc] initWithFrame:labelFrame];
[valueLabel setFont:[UIFont systemFontOfSize:12]];
valueLabel.textAlignment = UITextAlignmentLeft;
[valueLabel setText:#"valueLabel:"];
minLabel.text = [data objectForKey:#"min"];
maxLabel.text = [data objectForKey:#"max"];
valueLabel.text = [data objectForKey:#"value"];
slider.minimumValue = [[data objectForKey:#"min"] intValue];
slider.maximumValue = [[data objectForKey:#"max"] intValue];
slider.value = [[data objectForKey:#"value"] intValue];
//
[contentView addSubview:slider];
[contentView addSubview:minLabel];
[contentView addSubview:maxLabel];
[contentView addSubview:valueLabel];
[slider release];
[minLabel release];
[maxLabel release];
[valueLabel release];
startY+=60;
}
if ([[controlData type] isEqualToString:#"SliderFloat"])
{
NSLog(#"build SliderFloat");
}
However moving it into this class breaks it
#interface SliderIntView : UIView {
UILabel *minLabel;
UILabel *maxLabel;
UILabel *valueLabel;
UISlider *slider;
NSDictionary *data;
}
#property(nonatomic, retain) UISlider *slider;
#property(nonatomic, retain) UILabel *minLabel;
#property(nonatomic, retain) UILabel *maxLabel;
#property(nonatomic, retain) UILabel *valueLabel;
#property(nonatomic, retain) NSDictionary *data;
- (id)initWithFrame:(CGRect)frame withData:(NSDictionary *)controlData;
-(void) sliderAction:(id) sender;
#end
The implementation
#import "SliderIntView.h"
#implementation SliderIntView
#synthesize slider, minLabel, maxLabel, valueLabel, data;
- (id)initWithFrame:(CGRect)frame withData:(NSDictionary *) controlData {
self = [super initWithFrame:frame];
if (self) {
self.data = controlData;
float startX = frame.origin.x;
float startY = frame.origin.y;
slider = [[UISlider alloc] initWithFrame:frame];
[slider addTarget:self action:#selector(sliderAction:) forControlEvents:UIControlEventTouchUpInside];
CGRect labelFrame = CGRectMake(startX, startY+22.0, 100.0, 30.0);
minLabel = [[UILabel alloc] initWithFrame:labelFrame];
[minLabel setFont:[UIFont systemFontOfSize:12]];
minLabel.textAlignment = UITextAlignmentLeft;
[minLabel setText:#"minLabel:"];
labelFrame = CGRectMake(startX+200.0, startY+22.0, 100.0, 30.0);
maxLabel = [[UILabel alloc] initWithFrame:labelFrame];
[maxLabel setFont:[UIFont systemFontOfSize:12]];
maxLabel.textAlignment = UITextAlignmentLeft;
[maxLabel setText:#"maxLabel:"];
labelFrame = CGRectMake(startX+220.0, startY+0.0, 100.0, 30.0);
valueLabel = [[UILabel alloc] initWithFrame:labelFrame];
[valueLabel setFont:[UIFont systemFontOfSize:12]];
valueLabel.textAlignment = UITextAlignmentLeft;
[valueLabel setText:#"valueLabel:"];
minLabel.text = [data objectForKey:#"min"];
maxLabel.text = [data objectForKey:#"max"];
valueLabel.text = [data objectForKey:#"value"];
slider.minimumValue = [[data objectForKey:#"min"] intValue];
slider.maximumValue = [[data objectForKey:#"max"] intValue];
slider.value = [[data objectForKey:#"value"] intValue];
[self addSubview:slider];
//[self addSubview:maxLabel];
//[self addSubview:minLabel];
//[self addSubview:valueLabel];
}
return self;
}
-(void) sliderAction:(id) sender
{
NSLog("#never happens");
}
The frame was the offending code. I just needed to start the slider with it's own frame as opposed to using the one passed in.
float startX = frame.origin.x;
float startY = frame.origin.y;
slider = [[UISlider alloc] initWithFrame:frame];
needs to be
slider = [[UISlider alloc] initWithFrame:CGRectMake(0.0, 0.0, 250, 22.0)];
Did you set userInteractionEnabled to YES for your new subclass? Is your view sure to cover the whole area your sliders take up?