I am currently working on a socket based application in which I have to change color of UIlabel inside custom UITableView cell on the basis of comparing previous value of label with current value received from socket.
I take two string variables also in custom cell to check previous and current value.
Here is cellFor row at index path code...
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
MWCell *mwCell=(MWCell*)[tableView dequeueReusableCellWithIdentifier:#"mwCellIdentifier"];
if (mwCell==nil) {
NSArray *topLevelObjects=[[NSBundle mainBundle] loadNibNamed:#"MWCell" owner:self options:nil];
for (id temp in topLevelObjects) {
if ([temp isKindOfClass:[MWCell class]]) {
mwCell=(MWCell*)temp;
}
}
}
NSDictionary *tempDic=[watchScriptArray objectAtIndex:indexPath.row];
if ([[tempDic valueForKey:#"expirydate"] valueForKey:#"text"]) {
mwCell.companyName.text=[NSString stringWithFormat:#"%#-%#",[[tempDic valueForKey:#"symbolname"] valueForKey:#"text"],[[tempDic valueForKey:#"expirydate"] valueForKey:#"text"]];
}else{
mwCell.companyName.text=[NSString stringWithFormat:#"%#",[[tempDic valueForKey:#"symbolname"] valueForKey:#"text"]];
}
[mwCell.companyName setTextColor:t.formfgColor];
[mwCell.companyName setShadowColor:t.formShadowColor];
if (![mwCell.buyRate.text isEqualToString:#""]) {
float preVal=[mwCell.prev_br floatValue];
float nxtVal=[[[tempDic valueForKey:#"bestbuyprice"] valueForKey:#"text"] floatValue];
if (nxtVal>preVal) {
[mwCell.buyRate setBackgroundColor:t.socketHighbgColor];
mwCell.buyRate.textColor=t.socketHighfgColor;
}
if (nxtVal<preVal){
[mwCell.buyRate setBackgroundColor:t.socketLowbgColor];
mwCell.buyRate.textColor=t.socketLowfgColor;
}
}else{
[mwCell.buyRate setBackgroundColor:t.socketNormalbgColor];
mwCell.buyRate.textColor=t.socketNormalfgColor;
}
if (![mwCell.sellRate.text isEqualToString:#""]) {
float preVal=[mwCell.sellRate.text floatValue];
float nxtVal=[[[tempDic valueForKey:#"bestsellprice"] valueForKey:#"text"] floatValue];
if (nxtVal>preVal) {
[mwCell.sellRate setBackgroundColor:t.socketHighbgColor];
[mwCell.sellRate setTextColor:t.socketHighfgColor];
}
if (nxtVal<preVal) {
[mwCell.sellRate setBackgroundColor:t.socketLowbgColor];
[mwCell.sellRate setTextColor:t.socketLowfgColor];
}
}else{
[mwCell.sellRate setBackgroundColor:t.socketNormalbgColor];
mwCell.sellRate.textColor=t.socketNormalfgColor;
}
mwCell.buyRate.textAlignment=NSTextAlignmentCenter;
mwCell.sellRate.textAlignment=NSTextAlignmentCenter;
mwCell.buyRate.text=[[tempDic valueForKey:#"bestbuyprice"] valueForKey:#"text"];
mwCell.sellRate.text=[[tempDic valueForKey:#"bestsellprice"] valueForKey:#"text"];
mwCell.prev_br=[[tempDic valueForKey:#"bestbuyprice"] valueForKey:#"text"];
mwCell.pre_sr=[[tempDic valueForKey:#"bestsellprice"] valueForKey:#"text"];
[mwCell.buyRate setShadowOffset:CGSizeMake(0, 0)];
[mwCell.sellRate setShadowOffset:CGSizeMake(0, 0)];
if ([mwCell.buyRate.text floatValue]<0) {
[mwCell.buyRate setBackgroundColor:t.socketLowbgColor];
[mwCell.buyRate setTextColor:t.socketLowfgColor];
}
if ([mwCell.sellRate.text floatValue]<0) {
[mwCell.sellRate setBackgroundColor:t.socketLowbgColor];
[mwCell.sellRate setTextColor:t.socketLowfgColor];
}
return mwCell;
}
I have change my code to check with previous array values with current array values instead of checking current array with text in cell.But this also doesn't seems to work properly.On scrolling it fills other row color also.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
MWCell *mwCell=(MWCell*)[tableView dequeueReusableCellWithIdentifier:#"mwCellIdentifier"];
if (mwCell==nil) {
NSArray *topLevelObjects=[[NSBundle mainBundle] loadNibNamed:#"MWCell" owner:self options:nil];
for (id temp in topLevelObjects) {
if ([temp isKindOfClass:[MWCell class]]) {
mwCell=(MWCell*)temp;
mwCell.buyRate.backgroundColor=[UIColor whiteColor];
mwCell.sellRate.backgroundColor=[UIColor whiteColor];
mwCell.buyRate.textColor=[UIColor blackColor];
mwCell.sellRate.textColor=[UIColor blackColor];
}
}
}
NSDictionary *tempDic=[watchScriptArray objectAtIndex:indexPath.row];
NSDictionary *prevDict=[prevScriptArray objectAtIndex:indexPath.row];
if ([[tempDic valueForKey:#"expirydate"] valueForKey:#"text"]) {
mwCell.companyName.text=[NSString stringWithFormat:#"%#-%#",[[tempDic valueForKey:#"symbolname"] valueForKey:#"text"],[[tempDic valueForKey:#"expirydate"] valueForKey:#"text"]];
}else{
mwCell.companyName.text=[NSString stringWithFormat:#"%#",[[tempDic valueForKey:#"symbolname"] valueForKey:#"text"]];
}
[mwCell.companyName setTextColor:t.formfgColor];
[mwCell.companyName setShadowColor:t.formShadowColor];
float br_preVal=[[[prevDict valueForKey:#"bestbuyprice"] valueForKey:#"text"] floatValue];
float br_nxtVal=[[[tempDic valueForKey:#"bestbuyprice"] valueForKey:#"text"] floatValue];
float sr_preVal=[[[prevDict valueForKey:#"bestsellprice"] valueForKey:#"text"] floatValue];
float sr_nxtVal=[[[tempDic valueForKey:#"bestsellprice"] valueForKey:#"text"] floatValue];
if (br_nxtVal>br_preVal) {
[mwCell.buyRate setBackgroundColor:t.socketHighbgColor];
mwCell.buyRate.textColor=t.socketHighfgColor;
}
if (br_nxtVal<br_preVal){
[mwCell.buyRate setBackgroundColor:t.socketLowbgColor];
mwCell.buyRate.textColor=t.socketLowfgColor;
}
if (sr_nxtVal>sr_preVal) {
[mwCell.sellRate setBackgroundColor:t.socketHighbgColor];
[mwCell.sellRate setTextColor:t.socketHighfgColor];
}
if (sr_nxtVal<sr_preVal) {
[mwCell.sellRate setBackgroundColor:t.socketLowbgColor];
[mwCell.sellRate setTextColor:t.socketLowfgColor];
}
NSLog(#"tag of cell at index %i is %i",indexPath.row,mwCell.tag);
mwCell.buyRate.textAlignment=NSTextAlignmentCenter;
mwCell.sellRate.textAlignment=NSTextAlignmentCenter;
mwCell.buyRate.text=[[tempDic valueForKey:#"bestbuyprice"] valueForKey:#"text"];
mwCell.sellRate.text=[[tempDic valueForKey:#"bestsellprice"] valueForKey:#"text"];
[mwCell.buyRate setShadowOffset:CGSizeMake(0, 0)];
[mwCell.sellRate setShadowOffset:CGSizeMake(0, 0)];
if ([mwCell.buyRate.text floatValue]<0) {
[mwCell.buyRate setBackgroundColor:t.socketLowbgColor];
[mwCell.buyRate setTextColor:t.socketLowfgColor];
}
if ([mwCell.sellRate.text floatValue]<0) {
[mwCell.sellRate setBackgroundColor:t.socketLowbgColor];
[mwCell.sellRate setTextColor:t.socketLowfgColor];
}
return mwCell;
}
MWCell h file
#interface MWCell : UITableViewCell
#property(nonatomic,strong)IBOutlet UILabel *companyName,*buyRate,*sellRate;
#property(nonatomic,strong)NSString *prev_br,*pre_sr;
#end
MWCell m file
#import "MWCell.h"
#implementation MWCell
#synthesize companyName,buyRate,sellRate;
#synthesize pre_sr,prev_br;
- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
if (self) {
pre_sr=#"";
prev_br=#"";
// Initialization code
}
return self;
}
#end
It is working fine , but facing problem while scrolling.
If I scroll tableView its color automatically changes (even if no data coming from socket).I know every time we scroll tableView cell prepares and that causes custom cell's label color to change.
Is there any solution for this.
my screen shot is given below:----
Thanks!.
Apply an alternative to set previous color:--
take two NSMutableArray and init with white colors.
NSMutableArray *celllc,*cellrc;
prevScriptArray=[NSArray arrayWithArray:watchScriptArray];
[celllc removeAllObjects];
[cellrc removeAllObjects];
for (int i=0;i<[watchScriptArray count];i++) {
[cellrc addObject:[UIColor whiteColor]];
[celllc addObject:[UIColor whiteColor]];
}
Every time create cell by setting mwCell object to nil before checking it if(mwcell==nil)
and set color to cell from array of color as given below in cellforRowAtIndexPath method
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:#"cellIdentifier"];
if (cell==nil) {
cell=[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:#"cellIdentifier"];
}
MWCell *mwCell=(MWCell*)[tableView dequeueReusableCellWithIdentifier:#"mwCellIdentifier"];
if (mwCell!=nil)mwCell=nil;
if (mwCell==nil) {
NSArray *topLevelObjects=[[NSBundle mainBundle] loadNibNamed:#"MWCell" owner:self options:nil];
for (id temp in topLevelObjects) {
if ([temp isKindOfClass:[MWCell class]]) {
mwCell=(MWCell*)temp;
mwCell.buyRate.backgroundColor=[celllc objectAtIndex:indexPath.row];
mwCell.sellRate.backgroundColor=[cellrc objectAtIndex:indexPath.row];
mwCell.buyRate.textColor=([[celllc objectAtIndex:indexPath.row] isEqual:t.socketLowbgColor])?t.socketLowfgColor:t.socketHighfgColor;
mwCell.sellRate.textColor=([[cellrc objectAtIndex:indexPath.row] isEqual:t.socketLowbgColor])?t.socketLowfgColor:t.socketHighfgColor;
}
}
}
NSDictionary *tempDic=[watchScriptArray objectAtIndex:indexPath.row];
NSDictionary *prevDict=[prevScriptArray objectAtIndex:indexPath.row];
if ([[tempDic valueForKey:#"expirydate"] valueForKey:#"text"]) {
mwCell.companyName.text=[NSString stringWithFormat:#"%#-%#",[[tempDic valueForKey:#"symbolname"] valueForKey:#"text"],[[tempDic valueForKey:#"expirydate"] valueForKey:#"text"]];
}else{
mwCell.companyName.text=[NSString stringWithFormat:#"%#",[[tempDic valueForKey:#"symbolname"] valueForKey:#"text"]];
}
[mwCell.companyName setTextColor:t.formfgColor];
[mwCell.companyName setShadowColor:t.formShadowColor];
float br_preVal=[[[prevDict valueForKey:#"bestbuyprice"] valueForKey:#"text"] floatValue];
float br_nxtVal=[[[tempDic valueForKey:#"bestbuyprice"] valueForKey:#"text"] floatValue];
float sr_preVal=[[[prevDict valueForKey:#"bestsellprice"] valueForKey:#"text"] floatValue];
float sr_nxtVal=[[[tempDic valueForKey:#"bestsellprice"] valueForKey:#"text"] floatValue];
if (br_nxtVal>br_preVal) {
[mwCell.buyRate setBackgroundColor:t.socketHighbgColor];
mwCell.buyRate.textColor=t.socketHighfgColor;
[celllc replaceObjectAtIndex:indexPath.row withObject:t.socketHighbgColor];
}
if (br_nxtVal<br_preVal){
[mwCell.buyRate setBackgroundColor:t.socketLowbgColor];
mwCell.buyRate.textColor=t.socketLowfgColor;
[celllc replaceObjectAtIndex:indexPath.row withObject:t.socketLowbgColor];
}
if (sr_nxtVal>sr_preVal) {
[mwCell.sellRate setBackgroundColor:t.socketHighbgColor];
[mwCell.sellRate setTextColor:t.socketHighfgColor];
[cellrc replaceObjectAtIndex:indexPath.row withObject:t.socketHighbgColor];
}
if (sr_nxtVal<sr_preVal) {
[mwCell.sellRate setBackgroundColor:t.socketLowbgColor];
[mwCell.sellRate setTextColor:t.socketLowfgColor];
[cellrc replaceObjectAtIndex:indexPath.row withObject:t.socketLowbgColor];
}
NSLog(#"tag of cell at index %i is %i",indexPath.row,mwCell.tag);
mwCell.buyRate.textAlignment=NSTextAlignmentCenter;
mwCell.sellRate.textAlignment=NSTextAlignmentCenter;
mwCell.buyRate.text=[[tempDic valueForKey:#"bestbuyprice"] valueForKey:#"text"];
mwCell.sellRate.text=[[tempDic valueForKey:#"bestsellprice"] valueForKey:#"text"];
[mwCell.buyRate setShadowOffset:CGSizeMake(0, 0)];
[mwCell.sellRate setShadowOffset:CGSizeMake(0, 0)];
if ([mwCell.buyRate.text floatValue]<0) {
[mwCell.buyRate setBackgroundColor:t.socketLowbgColor];
[mwCell.buyRate setTextColor:t.socketLowfgColor];
}
if ([mwCell.sellRate.text floatValue]<0) {
[mwCell.sellRate setBackgroundColor:t.socketLowbgColor];
[mwCell.sellRate setTextColor:t.socketLowfgColor];
}
return mwCell;
}
Better answer will be appreciated.If any?
In my app am using UIScrollView which is having UIImageView(20) to add images iused the - (void)populateScrollView method.
- (void)populateScrollView
{
TonifyAppDelegate *appDelegate = (TonifyAppDelegate *)[UIApplication sharedApplication].delegate;
double x1 = 0, y1 = 3;
NSUserDefaults *ud = [NSUserDefaults standardUserDefaults];
NSMutableArray *getEffectsImageData = [ud objectForKey:#"getimageeffects"];
imageViewsArray = [[NSMutableArray alloc] initWithCapacity:[getEffectsImageData count]];
for(int i = 0; i < [getEffectsImageData count]; i++)
{
NSString *sfxUrlFileName = [ [getEffectsImageData objectAtIndex:i] lastPathComponent];
NSLog(#"sfxUrlFileName: %#", sfxUrlFileName);
NSData *imageData = [appDelegate readSongDataFromDocsDirectory:sfxUrlFileName];
UIImage *image = [[UIImage alloc] initWithData:imageData];
UIImageView *anImageView = [[UIImageView alloc]initWithImage:image];
CGRect imageFrame = anImageView.frame;
imageFrame.origin.x = x1;
imageFrame.origin.y = y1;
anImageView.frame = CGRectMake(x1, y1, 45, 41);
anImageView.userInteractionEnabled = YES;
[scrollView addSubview:anImageView];
x1 += anImageView.frame.size.width + 3;
if (anImageView && [anImageView isMemberOfClass: [UIImageView class]])
[imageViewsArray addObject: anImageView];
}
NSLog(#"imageViewsArray:%#",imageViewsArray);
}
And to recognize touch in UIScrollView i used
UITapGestureRecognizer *singleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(singleTapGestureCaptured:)];
[scrollView addGestureRecognizer:singleTap];
Meanwhile am running song by using NSTimer which is like
myTimer = [NSTimer scheduledTimerWithTimeInterval:timeinterval target:self selector:#selector(**updatplayer**) userInfo:nil repeats:YES];
[[NSRunLoop mainRunLoop] addTimer:myTimer forMode:NSRunLoopCommonModes];
in updatePlayer method am doing task as follows
- (void)updatplayer
{
TonifyAppDelegate *appDelegate = (TonifyAppDelegate *)[UIApplication sharedApplication].delegate;
if (mThresholdVal<mMainLoopLength)
{
mThresholdVal+=0.5;
}
else
{
mThresholdVal = 0.0;
}
if (divimageView)
{
[divimageView removeFromSuperview];
}
if ([self isHeadsetPluggedIn]==NO) {
UInt32 audioRouteOverride = kAudioSessionOverrideAudioRoute_Speaker;
AudioSessionSetProperty (kAudioSessionProperty_OverrideAudioRoute,sizeof (audioRouteOverride),&audioRouteOverride);
}
else
{
UInt32 audioRouteOverride = kAudioSessionOverrideAudioRoute_None;
AudioSessionSetProperty (kAudioSessionProperty_OverrideAudioRoute,sizeof (audioRouteOverride),&audioRouteOverride);
}
NSString *cu=[NSString stringWithFormat:#"%2.f",audioPlayer.currentTime];
NSString *du=[NSString stringWithFormat:#"%2.f",audioPlayer.duration];
NSLog(#"LOOP cu Duration = %#",cu);
NSLog(#"LOOP du = %#",du);
int curenttime=(int)[cu intValue];
NSLog(#"LOOP current Duration = %2.f",audioPlayer.duration);
if(curenttime==0)
{
[audioPlayer play];
x=20;
}
mixdata = [NSMutableDictionary dictionary];
{
[activityIndicator stopAnimating];
activityIndicator.hidden = YES;
[divimageView setFrame:CGRectMake(x, 87, 12, 144)];
[self.view addSubview:divimageView];
x=x+1;
if ((x>19 && x< 480) && appDelegate.songURL != NULL) {
[avplayer play];
NSLog(#"mixdata:%#", mixdata);
}
else if((x>19 && x< 480) && (appDelegate.songIntroUrl || appDelegate.songMidUrl || appDelegate.songChorusUrl) )
{
[audioPlayer play];
}
if (x==xCord && yCord<235)
{
[audioPlayer1 play];
}
if (x==recXcord)
{
recaudioPlayer.volume = 10.0;
[recaudioPlayer play];
[activityIndicator stopAnimating];
activityIndicator.hidden = YES;
}
if(appDelegate.xCordArray!=NULL && [appDelegate.tagArray count]>0)
{
for(int i = 0; i<[appDelegate.xCordArray count];i++)
{
if (i == 0){
NSLog(#"sfxCount in i==0......%d",sfxCount);
NSLog(#"[appDelegate.xCordArray count] in i==0......%d",[appDelegate.xCordArray count]);
NSLog(#"[appDelegate.mMixedSFXTrack2Array count] in i==0......%d",[appDelegate.mMixedSFXTrack2Array count]);
if (sfxCount-1 != [appDelegate.xCordArray count])
{
if ([appDelegate.xCordArray count] > [appDelegate.mMixedSFXTrack2Array count] )
{
mShouldBufferUpdate = TRUE;
sfxCount = [appDelegate.xCordArray count];
}
}
}
if (x==[[appDelegate.xCordArray objectAtIndex:i] intValue] && appDelegate.imgDragXCodr<480)
{
int j = [[appDelegate.tagArray objectAtIndex:i] intValue];
//mShouldBufferUpdate = TRUE;
[self getSetSongData:j :x];
[audioPlayer2 play];
}
}
}
if(appDelegate.xCordTrack3Array!=NULL && [appDelegate.track3tagArray count]>0)
{
for(int i = 0; i<[appDelegate.xCordTrack3Array count];i++)
{
if (i == 0)
{
if (sfxCount3-1 != [appDelegate.xCordTrack3Array count])
{
if ([appDelegate.xCordTrack3Array count] > [appDelegate.mMixedSFXTrack3Array count] )
{
mShouldBuffer3Update = TRUE;
sfxCount3 = [appDelegate.xCordTrack3Array count];
}
}
}
if (x==[[appDelegate.xCordTrack3Array objectAtIndex:i] intValue] && appDelegate.imgDragXCodr1<480)
{
int j = [[appDelegate.track3tagArray objectAtIndex:i] intValue];
[self getSetSongData:j :x];
[trac3AudioPlayer play]; //For SFX Sounds
}
}
}
if (x == 480)
{
x=20;
}
}
}
When i click on scrollView it generates an image in UIView(main View) at that time NSTimer is getting paused.
How can we solve this.
Any one can help or suggest me.
Thanks in advance.
-(void)textFieldDidBeginEditing:(UITextField *)myTextField{
[myTextField resignFirstResponder];
UITextField *tempbtn;
tempbtn = myTextField;
btntagvalue = tempbtn.tag;
if(btntagvalue == 1)
{
if ((quantitypicker.hidden == NO) || (frequencyvalues.hidden == NO) ||(daysnum.hidden ==
NO)) {
[quantitypicker setHidden:YES];
[frequencyvalues setHidden:YES];
[daysnum setHidden:YES];
}
if ((quantitypicker.hidden == NO)||(frequencyvalues.hidden==NO)||(daysnum.hidden==YES))
{
[quantitypicker setHidden:YES];
[frequencyvalues setHidden:YES];
}
if ((quantitypicker.hidden==YES)||(frequencyvalues.hidden==NO)||(daysnum.hidden==NO))
{
[frequencyvalues setHidden:YES];
[daysnum setHidden:YES];
}
if ((quantitypicker.hidden==NO)||(frequencyvalues.hidden==YES)||(daysnum.hidden==NO))
{
[quantitypicker setHidden:YES];
[daysnum setHidden:YES];
}
datevalues = [[UIDatePicker alloc] initWithFrame:CGRectMake(0, 150, 320, 280)];
datevalues.datePickerMode = UIDatePickerModeDateAndTime;
datevalues.minimumDate=[NSDate date];
[self.view addSubview:datevalues];
[datevalues addTarget:self
action:#selector(datepicker:)forControlEvents:UIControlEventValueChanged];
[datevalues release];
}
if (btntagvalue == 2) {
if ((datevalues.hidden == NO) || (frequencyvalues.hidden == NO) ||(daysnum.hidden == NO)) {
[datevalues setHidden:YES];
[frequencyvalues setHidden:YES];
[daysnum setHidden:YES];
}
if ((datevalues.hidden == NO) || (frequencyvalues.hidden == NO) ||(daysnum.hidden == YES)) {
[datevalues setHidden:YES];
[frequencyvalues setHidden:YES];
}
if ((datevalues.hidden == YES) || (frequencyvalues.hidden == NO) ||(daysnum.hidden == NO)) {
[frequencyvalues setHidden:YES];
[daysnum setHidden:YES];
}
if ((datevalues.hidden == NO) || (frequencyvalues.hidden == YES) ||(daysnum.hidden == NO)) {
[datevalues setHidden:YES];
[daysnum setHidden:YES];
}
quantitypicker = [[UIPickerView alloc] initWithFrame:CGRectMake(0,190,320,220)];
if ([meditype isEqualToString:#"Capsules"]) {
quantitytype = [[NSMutableArray
alloc]initWithObjects:#"1",#"2",#"3",#"4",#"5",#"6",#"7",#"8",#"9",#"10",nil];
}
if ([meditype isEqualToString:#"Eyedrops"]||[meditype isEqualToString:#"Eardrops"]||\
[meditype isEqualToString:#"Nosedrops"]) {
quantitytype = [[NSMutableArray
alloc]initWithObjects:#"1",#"2",#"3",#"4",#"5",#"6",#"7",#"8",#"9",#"10",nil];
}
if ([meditype isEqualToString:#"Inhaler"]) {
quantitytype = [[NSMutableArray alloc]initWithObjects:#"1 dose",#"2 doses",#"3
doses",#"4 doses",#"5 doses",#"6 doses",#"7 doses",#"8 doses",#"9 doses",#"10 doses",nil];
}
if ([meditype isEqualToString:#"Syrup"]) {
quantitytype = [[NSMutableArray alloc]initWithObjects:#"1 tablespoon",#"2
tablespoon",#"3 tablespoon",#"4 tablespoon",#"5 tablespoon",#"6 tablespoon",#"7
tablespoon",#"8 tablespoon",#"9 tablespoon",#"10 tablespoon",nil];
}
if ([meditype isEqualToString:#"Oils"]) {
quantitytype = [[NSMutableArray alloc]initWithObjects:#"1 ml",#"2 ml",#"3 ml",#"4
ml",#"5 ml",#"6 ml",#"7 ml",#"8 ml",#"9 ml",#"10 ml",nil];
}
if ([meditype isEqualToString:#"Injections"]) {
quantitytype = [[NSMutableArray
alloc]initWithObjects:#"1",#"2",#"3",#"4",#"5",#"6",#"7",#"8",#"9",#"10",nil];
}
quantitypicker.delegate = self;
quantitypicker.showsSelectionIndicator = YES;
quantitypicker.autoresizingMask = UIViewAutoresizingFlexibleWidth;
[self.view addSubview:quantitypicker];
}
if (btntagvalue ==3) {
if ((datevalues.hidden == NO) || (quantitypicker.hidden == NO) ||(daysnum.hidden == NO)) {
[datevalues setHidden:YES];
[quantitypicker setHidden:YES];
[daysnum setHidden:YES];
}
if ((datevalues.hidden == NO) || (quantitypicker.hidden == NO) ||(daysnum.hidden == YES)) {
[datevalues setHidden:YES];
[quantitypicker setHidden:YES];
}
if ((datevalues.hidden == YES) || (quantitypicker.hidden == NO) ||(daysnum.hidden == NO)) {
[quantitypicker setHidden:YES];
[daysnum setHidden:YES];
}
if ((datevalues.hidden == NO) || (quantitypicker.hidden == YES) ||(daysnum.hidden == NO)) {
[datevalues setHidden:YES];
[daysnum setHidden:YES];
}
frequencyvalues = [[UIPickerView alloc] initWithFrame:CGRectMake(0, 190, 320, 220)];
arrayfreq = [[NSMutableArray alloc]initWithObjects:#"1",#"2",#"3",#"4",#"5",
#"6",#"7",#"8",#"9",#"10",#"11",#"12",#"13",#"14",#"15",#"16",
#"17",#"18",#"19",#"20",#"21",#"22",#"23",#"24", nil];
frequencyvalues.delegate = self;
frequencyvalues.showsSelectionIndicator = YES;
frequencyvalues.autoresizingMask = UIViewAutoresizingFlexibleWidth;
[self.view addSubview:frequencyvalues];
}
if (btntagvalue ==4) {
if ((datevalues.hidden == NO) || (frequencyvalues.hidden == NO) ||(quantitypicker.hidden ==
NO)) {
[datevalues setHidden:YES];
[frequencyvalues setHidden:YES];
[quantitypicker setHidden:YES];
}
if ((datevalues.hidden == NO) || (frequencyvalues.hidden == NO) ||(quantitypicker.hidden ==
YES)) {
[datevalues setHidden:YES];
[frequencyvalues setHidden:YES];
}
if ((datevalues.hidden == YES) || (frequencyvalues.hidden == NO) ||(quantitypicker.hidden ==
NO)) {
[frequencyvalues setHidden:YES];
[quantitypicker setHidden:YES];
}
if ((datevalues.hidden == NO) || (frequencyvalues.hidden == YES) ||(quantitypicker.hidden ==
NO)) {
[datevalues setHidden:YES];
[quantitypicker setHidden:YES];
}
daysnum = [[UIPickerView alloc] initWithFrame:CGRectMake(0, 190, 320, 220)];
arraytime = [[NSMutableArray alloc]initWithObjects:#"1",#"2",#"3",#"4",#"5",
#"6",#"7",#"8",#"9",#"10",#"11",#"12",#"13",#"14",#"15",#"16",
#"17",#"18",#"19",#"20",#"21",#"22",#"23",#"24",#"25",#"26",#"27",#"28",#"29",#"30",#"31",
nil];
daysnum.delegate = self;
daysnum.showsSelectionIndicator = YES;
daysnum.autoresizingMask = UIViewAutoresizingFlexibleWidth;
[self.view addSubview:daysnum];
}
}
thanks to all for viewing my problem. I have solved it by using conditions in each if loop.. i have edited the above code
I am getting overlapping issue while using following code. I used custom cell and normal cell style for this form.
Any suggestion is appreciated
(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *RSVNIdentifier = #"RSVNCell";
CreateReservationViewCell *cell = (CreateReservationViewCell *)[tableView dequeueReusableCellWithIdentifier:RSVNIdentifier] ;
if (cell == nil) {
cell = [[[CreateReservationViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:RSVNIdentifier] autorelease];
cell.selectionStyle = UITableViewCellSelectionStyleNone;
}
if(indexPath.section == 0)
{
{
if(indexPath.row == 0){
//cell.selectionStyle = UITableViewCellSelectionStyleNone;
cell.editing =NO;
cell.m_rsvn_name.text =#"Customer";
cell.m_rsvn_value.text =[[[create_reservation_detail objectAtIndex:0] objectForKey:#"reservation"] objectForKey:#"createdBy"];
cell.m_rsvn_value.enabled=NO;
return cell;
}
if(indexPath.row == 1){
cell.m_rsvn_name.text =#"Till";
cell.m_rsvn_value.text =[[[create_reservation_detail objectAtIndex:0] objectForKey:#"reservation"] objectForKey:#"createdDate"];
cell.m_rsvn_value.enabled=NO;
return cell;
}
if(indexPath.row == 2){
//cell.selectionStyle = UITableViewCellSelectionStyleNone;
emailTextField = [[UITextField alloc] initWithFrame:CGRectMake(110, 10, 185, 30)];
emailTextField.adjustsFontSizeToFitWidth = YES;
emailTextField.font = [UIFont systemFontOfSize:12];
emailTextField.textAlignment= UITextAlignmentRight;
emailTextField.keyboardType= UIKeyboardTypeEmailAddress;
emailTextField.returnKeyType = UIReturnKeyDone;
emailTextField.tag=0;
emailTextField.delegate=self;
emailTextField.text =[textFields objectAtIndex:0];
cell.m_rsvn_name.text =#"Email";
[cell addSubview:emailTextField];
return cell;
}
if(indexPath.row == 3){
cell.m_rsvn_name.text =#"Telephone";
phoneTextField = [[UITextField alloc] initWithFrame:CGRectMake(110, 10, 185, 30)];
phoneTextField.adjustsFontSizeToFitWidth = YES;
phoneTextField.font = [UIFont systemFontOfSize:12];
phoneTextField.textAlignment= UITextAlignmentRight;
phoneTextField.keyboardType= UIKeyboardTypePhonePad;
phoneTextField.delegate=self;
phoneTextField.tag=1;
phoneTextField.text =[textFields objectAtIndex:1];
cell.m_rsvn_name.text =#"Phone";
[cell addSubview:phoneTextField];
return cell;
}
if(indexPath.row == 4){
cell.m_rsvn_name.text =#"Internal Note";
cell.m_rsvn_value.text =[[[create_reservation_detail objectAtIndex:0] objectForKey:#"reservation"] objectForKey:#"internalNote"];
cell.selectionStyle = UITableViewCellSelectionStyleNone;
cell.m_rsvn_value.enabled=NO;
return cell;
}
}
}
else if(indexPath.section == 1){
NSLog(#"%#",unit_details);
//[[create_reservation_detail objectAtIndex:0] objectForKey:#"reservation"] objectForKey:#"unitName"];
for (int i=0; i<[unit_details count]; i++) {
if(indexPath.row == i){
cell.m_unit_title.text = [[unit_details objectAtIndex:0] objectForKey:#"unitName"];
cell.m_unit_value1.text = [[unit_details objectAtIndex:0] objectForKey:#"nights"];
cell.m_unit_value2.text =[[unit_details objectAtIndex:0] objectForKey:#"adults"];
return cell;
}
}
}
else if(indexPath.section == 2){
if(indexPath.row == 0){
cell.m_rsvn_name.text = #"Total Charges";
cell.m_rsvn_value.text =[[[create_reservation_detail objectAtIndex:0] objectForKey:#"charges"] objectForKey:#"totalRoomCharge"];
return cell;
}
if(indexPath.row == 1){
cell.m_rsvn_name.text = #"10% Tax";
cell.m_rsvn_value.text =[[[create_reservation_detail objectAtIndex:0] objectForKey:#"charges"] objectForKey:#"roomTaxAmount"];
return cell;
}
if(indexPath.row == 2){
cell.m_rsvn_name.text = #"Total Charges1";
cell.m_rsvn_value.text =[[[create_reservation_detail objectAtIndex:0] objectForKey:#"charges"] objectForKey:#"otherCharge"];
return cell;
}
if(indexPath.row == 3){
cell.m_rsvn_name.text = #"Total Charges2";
cell.m_rsvn_value.text =[[[create_reservation_detail objectAtIndex:0] objectForKey:#"charges"] objectForKey:#"totalCharge"];
return cell;
}
if(indexPath.row == 4){
cell.m_rsvn_name.text = #"Total Payments";
//cell.m_rsvn_value.text =[[[create_reservation_detail objectAtIndex:0] objectForKey:#"charges"] objectForKey:#"totalPayment"];
return cell;
}
if(indexPath.row == 5){
cell.m_rsvn_name.text = #"Current Balance";
cell.m_rsvn_value.text =[[[create_reservation_detail objectAtIndex:0] objectForKey:#"charges"] objectForKey:#"currentBalance"];
return cell;
}
}
else if(indexPath.section == 3){
if(indexPath.row == 0){
cell.m_rsvn_name.text =[[[create_reservation_detail objectAtIndex:0] objectForKey:#"deposit"] objectForKey:#"description"];
cell.m_rsvn_value.text =[[[create_reservation_detail objectAtIndex:0] objectForKey:#"deposit"] objectForKey:#"amount"];
return cell;
}
else if(indexPath.row == 1){
cell.m_rsvn_name.text =#"Amount Due";
cell.m_rsvn_value.text =[[[create_reservation_detail objectAtIndex:0] objectForKey:#"deposit"] objectForKey:#"amount_due"];
return cell;
}
else if(indexPath.row == 2){
cell.m_rsvn_name.text =#"Security Deposit status";
cell.m_rsvn_value.text =[[[create_reservation_detail objectAtIndex:0] objectForKey:#"deposit"] objectForKey:#"securityStatus"];
return cell;
}
else if(indexPath.row == 3){
cell.m_rsvn_name.text =#"Security Deposit Due";
cell.m_rsvn_value.text =[[[create_reservation_detail objectAtIndex:0] objectForKey:#"deposit"] objectForKey:#"securityStatus_due"];
return cell;
}
else if(indexPath.row == 4){
static NSString *SecurityCellIdentifier = #"Cell";
UITableViewCell *security_cell = [tableView dequeueReusableCellWithIdentifier:SecurityCellIdentifier];
if (security_cell == nil) {
security_cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:SecurityCellIdentifier] autorelease];
}
// Configure the cell...
security_cell.textLabel.text = #"Security Deposit Instruction:";
security_cell.textLabel.font = [UIFont systemFontOfSize:14];
security_cell.detailTextLabel.text =[[[create_reservation_detail objectAtIndex:0] objectForKey:#"deposit"] objectForKey:#"instruction"];
security_cell.textLabel.font = [UIFont systemFontOfSize:14];
security_cell.detailTextLabel.lineBreakMode = UILineBreakModeWordWrap;
security_cell.detailTextLabel.numberOfLines = 0;
[security_cell.detailTextLabel sizeToFit];
return security_cell;
}
}
else if(indexPath.section == 4){
static NSString *Payment_CellIdentifier = #"Payment_Cell";
UITableViewCell *pay_cell = [tableView dequeueReusableCellWithIdentifier:Payment_CellIdentifier];
if (!pay_cell) {
pay_cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:Payment_CellIdentifier] autorelease];
}
pay_cell.textLabel.font=[UIFont systemFontOfSize:14];
if(indexPath.row == 0){
if ([[[[create_reservation_detail objectAtIndex:0] objectForKey:#"payment"] objectForKey:#"paypal"] isEqualToString:#""])
{
pay_cell.accessoryType=UITableViewCellAccessoryNone;
pay_cell.selectionStyle = UITableViewCellSelectionStyleNone;
}
else
{
pay_cell.accessoryType = UITableViewCellAccessoryCheckmark;
}
NSLog(#"accessory type ->%d",cell.accessoryType);
pay_cell.textLabel.text = #"PayPal";
pay_cell.selectionStyle = UITableViewCellSelectionStyleGray;
pay_cell.detailTextLabel.text=[[[create_reservation_detail objectAtIndex:0] objectForKey:#"payment"] objectForKey:#"paypal"];
pay_cell.detailTextLabel.lineBreakMode = UILineBreakModeWordWrap;
pay_cell.detailTextLabel.numberOfLines = 0;
[pay_cell.detailTextLabel sizeToFit];
return pay_cell;
}
if(indexPath.row == 1){
if ([[[[create_reservation_detail objectAtIndex:0] objectForKey:#"payment"] objectForKey:#"manualCheck"] isEqualToString:#""])
{
pay_cell.accessoryType=UITableViewCellAccessoryNone;
pay_cell.selectionStyle = UITableViewCellSelectionStyleNone;
}
else
{
pay_cell.accessoryType = UITableViewCellAccessoryCheckmark;
}
NSLog(#"accessory type ->%d",cell.accessoryType);
pay_cell.textLabel.text = #"Manual Check";
pay_cell.selectionStyle = UITableViewCellSelectionStyleGray;
pay_cell.selectionStyle = UITableViewCellSelectionStyleGray;
pay_cell.detailTextLabel.text=[[[create_reservation_detail objectAtIndex:0] objectForKey:#"payment"] objectForKey:#"manualCheck"];
pay_cell.detailTextLabel.lineBreakMode = UILineBreakModeWordWrap;
pay_cell.detailTextLabel.numberOfLines = 0;
[pay_cell.detailTextLabel sizeToFit];
return pay_cell;
}
if(indexPath.row == 2){
if ([[[[create_reservation_detail objectAtIndex:0] objectForKey:#"payment"] objectForKey:#"tranfer"] isEqualToString:#""])
{
pay_cell.accessoryType=UITableViewCellAccessoryNone;
NSLog(#"accessory type ->%d",UITableViewCellAccessoryCheckmark);
pay_cell.selectionStyle = UITableViewCellSelectionStyleNone;
}
else
{
pay_cell.accessoryType = UITableViewCellAccessoryCheckmark;
}
NSLog(#"accessory type ->%d",UITableViewCellAccessoryCheckmark);
pay_cell.textLabel.text = #"Wire / Transfer";
pay_cell.selectionStyle = UITableViewCellSelectionStyleGray;
pay_cell.selectionStyle = UITableViewCellSelectionStyleGray;
pay_cell.detailTextLabel.text=[[[create_reservation_detail objectAtIndex:0] objectForKey:#"payment"] objectForKey:#"tranfer"];
pay_cell.detailTextLabel.lineBreakMode = UILineBreakModeWordWrap;
pay_cell.detailTextLabel.numberOfLines = 0;
[pay_cell.detailTextLabel sizeToFit];
return pay_cell;
}
if(indexPath.row == 3){
other.borderStyle=UITextBorderStyleRoundedRect;
other.placeholder=#"other\n";
other.autocorrectionType=UITextAutocorrectionTypeNo;
other.delegate=self;
other.autocapitalizationType=UITextAutocapitalizationTypeNone;
other.hidden=[[textFields objectAtIndex:2] intValue];
other.returnKeyType = UIReturnKeyDone;
[pay_cell addSubview:other];
pay_cell.textLabel.text = #"Other";
pay_cell.selectionStyle = UITableViewCellSelectionStyleGray;
return pay_cell;
}
}
else {
if(indexPath.row == 0){
static NSString *Button_CellIdentifier = #"Button_Cell";
UITableViewCell *button_cell = [tableView dequeueReusableCellWithIdentifier:Button_CellIdentifier];
if (button_cell == nil)
{
button_cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:Button_CellIdentifier] autorelease];
}
button_cell.backgroundView= [[[UIImageView alloc] initWithImage:[UIImage imageNamed:#"save_reservation_button.png"]] autorelease];
button_cell.selectedBackgroundView= [[[UIImageView alloc] initWithImage:[UIImage imageNamed:#"save_reservation_button.png"]] autorelease];
return button_cell;
}
}
return nil;
}
Regards,
sathish
You should make custom view added to cell inside the block
if (cell == nil) {
cell = [[[CreateReservationViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:RSVNIdentifier] autorelease];
cell.selectionStyle = UITableViewCellSelectionStyleNone;
// deal with your custom views here
}
For example, this codes will make one more sub-views on the cell. If this cell is come from dequeue, it already has a emailTextField added on this cell, and following code will add one more emailTextField on this cell.
if(indexPath.row == 2){
//cell.selectionStyle = UITableViewCellSelectionStyleNone;
emailTextField = [[UITextField alloc] initWithFrame:CGRectMake(110, 10, 185, 30)];
... // assign emailTextField settings
cell.m_rsvn_name.text =#"Email";
[cell addSubview:emailTextField];
return cell;
}
where overlapping is happening. means on cell item or anywhere else. and in code somewhere u using [cell addSubView] instead of that use [cell.contentView addSubView]
I'm programming an app for the iPhone. I'm not very good with loops just yet. How do I shorten this code into a for loop?
if(CGRectContainsRect([space1 frame], [box frame])){
space1.image = [UIImage imageNamed:#"box.png"];
}
else if(CGRectContainsRect([space2 frame], [box frame])){
space2.image = [UIImage imageNamed:#"box.png"];
}
else if(CGRectContainsRect([space3 frame], [box frame])){
space3.image = [UIImage imageNamed:#"box.png"];
}
else if(CGRectContainsRect([space4 frame], [box frame])){
space4.image = [UIImage imageNamed:#"box.png"];
}
else if(CGRectContainsRect([space5 frame], [box frame])){
space5.image = [UIImage imageNamed:#"box.png"];
}
NSArray * spaces = [NSArray arrayWithObjects:space1, space2, space3, space4, space5, nil];
for (Space * space in spaces) {
if (CGRectContainsRect([space frame], [box frame])) {
space.image = [UIImage imageNamed:#"box.png"];
}
}