How to set Badge value based on NextviewControllerValue? - ios

I want to populate the firstComponentValue in a lbl_card_count badge.
UILabel *lbl_card_count = [[UILabel alloc]initWithFrame:CGRectMake(23,0, 13, 13)];
int Temp_card_count;
lbl_card_count.textColor = [UIColor whiteColor];
lbl_card_count.textAlignment = NSTextAlignmentCenter;
lbl_card_count.text = [NSString stringWithFormat:#"%d",Temp_card_count];
lbl_card_count.layer.borderWidth = 1;
lbl_card_count.layer.cornerRadius = 8;
lbl_card_count.layer.masksToBounds = YES;
lbl_card_count.layer.borderColor =[[UIColor clearColor] CGColor];
lbl_card_count.layer.shadowColor = [[UIColor clearColor] CGColor];
lbl_card_count.layer.shadowOffset = CGSizeMake(0.0, 0.0);
lbl_card_count.layer.shadowOpacity = 0.0;
lbl_card_count.backgroundColor = [UIColor colorWithRed:247.0/255.0 green:45.0/255.0 blue:143.0/255.0 alpha:1.0];
lbl_card_count.font = [UIFont fontWithName:#"ArialMT" size:11];
[lbl_card_count setHidden:YES];
[appliancesButton addSubview:lbl_card_count];
[categoryView addSubview:appliancesButton];
-(void)changecolor:(UIGestureRecognizer *)gestureRecognizer
{
AddApplianceViewController *addApplianceVC = [[AddApplianceViewController alloc]init];
[self showPopupWithTransitionStyle:STPopupTransitionStyleSlideVertical rootViewController:addApplianceVC];
addApplianceVC.labelText = gestureRecognizer.accessibilityLabel;
NSLog(#"tapped");
}
AddAppliancesViewController.m
-(void)setButtonAction
{
if ([self.picker selectedRowInComponent:0]!=0)
{
firstComponentValue = [self.picker selectedRowInComponent:0];
NSLog(#"Appliances Count==>>%d",firstComponentValue);
[self dismissViewControllerAnimated:YES completion:NO];
}
else
{
[self alert];
}
}`

Related

UITextField will not become first responder when touched, none of the usual mistakes apply

I have a UITextField that will not become the first responder when tapped. I can assign it to become first responder, which works find. But if it resigns first responder status and I try and tap it or tab back to it to make it become the first responder again, nothing happens. It appears as if the touch is being trapped somewhere, and yet I can't find anything in my code that could be causing that to happen. I've checked the usual suspects:
If the textfield the top view
is the textfield within the bounds of it's superview
is the textfield userEnabled.
I've also rewritten the code in several different ways, to no avail.
Can anyone help me with this problem. The textField at issue is the field titled answerTextField in the method createOneDoubleViewAtOrigin.
The relevant code is below.
-(instancetype)initForProblem:(NSString *)problem{
NSLog(#"%# '%#'",self.class, NSStringFromSelector(_cmd));
self = [super init];
if (self) {
[self parseBasicFractionProblem:problem];
if (_problemType == fractDoubleWithPic) {
NSLog(#"placed the fract views");
UIView *firstProblemView = [self createOneDoubleViewAtOrigin:CGPointMake(26, 30) withNumerator:_numerator1 denominator:_denominator1 forViewNumber:0];
UIView *secondProblemView = [self createOneDoubleViewAtOrigin:CGPointMake(342,30) withNumerator:_numerator2 denominator:_denominator2 forViewNumber:1];
[self addSubview:firstProblemView];
[self addSubview:secondProblemView];
[self bringSubviewToFront:firstProblemView];
[self bringSubviewToFront:secondProblemView];
}
else if (_problemType == fractDoubleNoPicAns||_problemType == fractDoubleNoPicExtendedAns ){
}
}
self.tag = 800;
self.backgroundColor = [UIColor redColor];
NSLog(#"made to end");
return self;
}
-(UIView *)createOneDoubleViewAtOrigin:(CGPoint)viewOrigin withNumerator:(NSInteger)numerator denominator:(NSInteger)denominator forViewNumber:(NSInteger)viewNumber{
NSLog(#"%# '%#'",self.class, NSStringFromSelector(_cmd));
UIView *containerView = [[UIView alloc] initWithFrame: CGRectMake(viewOrigin.x,viewOrigin.y, 310, 263)];
containerView.backgroundColor = [UIColor colorWithRed:178.0/255.0 green:222.0/255.0 blue:80.0/255.0 alpha:1.0];
containerView.layer.cornerRadius = 5.0;
UILabel *numeratorView = [self createSubview:CGRectMake(66, 23, 59, 47) text:[NSString stringWithFormat:#"%ld",(long)numerator] inView:containerView];
UILabel *divisorView = [self createSubview:CGRectMake(66, 40, 59, 47) text:#"___" inView:containerView];
UILabel *denominatorView = [self createSubview:CGRectMake(72, 82, 47, 47) text:[NSString stringWithFormat:#"%ld",(long)denominator] inView:containerView];
UILabel *equals = [self createSubview:CGRectMake(125, 50, 47, 47) text:#"=" inView:containerView];
/*
FFractSupportedTextField *answerField = [self createAnswerField:CGRectMake(173,50,82,47)];
*/
UITextField *answerTextField = [[UITextField alloc] initWithFrame:CGRectMake(173,50,82,47)];
//Inside
answerTextField.font = [UIFont fontWithName:#"Helvetica" size:30.0];
answerTextField.textAlignment = NSTextAlignmentCenter;
answerTextField.placeholder = #"?";
//border
answerTextField.layer.borderWidth = 1;
answerTextField.layer.borderColor = [[UIColor blackColor] CGColor];
answerTextField.layer.cornerRadius = 5.0;
answerTextField.userInteractionEnabled = YES;
[answerTextField addTarget:self action:#selector(textFieldDidChange:) forControlEvents:UIControlEventEditingChanged];
containerView.tag = 820 + 6*viewNumber;
numeratorView.tag = 821 + 6*viewNumber;
divisorView.tag = 822 + 6*viewNumber;
denominatorView.tag = 823 + 6*viewNumber;
equals.tag = 824 + 6*viewNumber;
answerTextField.tag = 801 + viewNumber;
UIView *pictureView = [self createFractPictureForNumerator:numerator denominator:denominator number:viewNumber];
pictureView.tag = 825 + 6*viewNumber;
if (viewNumber == 0){
_answerTextField1 = answerTextField;
[containerView addSubview:_answerTextField1];
[containerView bringSubviewToFront:_answerTextField1];
_pictureView1 = pictureView;
[containerView addSubview:_pictureView1];
[_answerTextField1 becomeFirstResponder];
} else if (viewNumber == 1) {
_answerTextField2 = answerTextField;
[containerView addSubview:_answerTextField2];
[containerView bringSubviewToFront:_answerTextField2];
_pictureView2 = pictureView;
[containerView addSubview:_pictureView2];
}
return containerView;
}
-(UILabel *)createSubview:(CGRect)frame text:(NSString *)text inView:(UIView *)containerView{
NSLog(#"%# '%#'",self.class, NSStringFromSelector(_cmd));
UILabel *labelView = [[UILabel alloc] initWithFrame:frame];
labelView.font = [UIFont fontWithName:#"Helvetica" size:30.0];
labelView.textAlignment = NSTextAlignmentCenter;
labelView.text = text;
[containerView addSubview:labelView];
return labelView;
}
-(FFractSupportedTextField *)createAnswerField:(CGRect)frame{
NSLog(#"%# '%#'",self.class, NSStringFromSelector(_cmd));
FFractSupportedTextField *fieldView = [[FFractSupportedTextField alloc] initWithFrame:frame];
//Inside
fieldView.font = [UIFont fontWithName:#"Helvetica" size:30.0];
fieldView.textAlignment = NSTextAlignmentCenter;
fieldView.placeholder = #"?";
//border
fieldView.layer.borderWidth = 1;
fieldView.layer.borderColor = [[UIColor blackColor] CGColor];
fieldView.layer.cornerRadius = 5.0;
fieldView.userInteractionEnabled = YES;
return fieldView;
}
-(UIView *)createFractPictureForNumerator:(NSInteger)numerator denominator:(NSInteger)denominator number:(NSInteger)viewNumber{
NSLog(#"%# '%#'",self.class, NSStringFromSelector(_cmd));
NSLog(#"numerator:%ld denominator:%ld",(long)numerator,(long)denominator);
UIView *containerView = [[UIView alloc] initWithFrame:CGRectMake(33, 165, 256, 78)];
containerView.backgroundColor = [UIColor whiteColor];
containerView.layer.borderColor = [[UIColor lightGrayColor] CGColor];
containerView.layer.borderWidth = 1.0;
containerView.layer.cornerRadius = 3.0;
NSInteger smallViewCount = denominator;
if (denominator == 0) {
smallViewCount = 1;
}
float smallWidth = 245.0/smallViewCount;
for (int n = 0; n < smallViewCount; n++) {
NSLog(#"count %d",n);
UILabel *smallLabel = [[UILabel alloc] initWithFrame:CGRectMake(8 + n*smallWidth, 8, smallWidth - 5, 29)];
smallLabel.backgroundColor = [UIColor colorWithRed:195.0/255.0 green:222.0/255.0 blue:172.0/255.0 alpha:1.0];
smallLabel.font = [UIFont fontWithName:#"Helvetica" size:17.0];
[smallLabel setAdjustsFontSizeToFitWidth:YES];
smallLabel.textAlignment = NSTextAlignmentCenter;
smallLabel.layer.cornerRadius = 3.0;
smallLabel.tag = 830+n + viewNumber*10;
[containerView addSubview:smallLabel];
}
UILabel *largeLabel = [[UILabel alloc] initWithFrame:CGRectMake(8, 41, 240, 29)];
largeLabel.backgroundColor = [UIColor colorWithRed:195.0/255.0 green:222.0/255.0 blue:172.0/255.0 alpha:1.0];
largeLabel.text = [NSString stringWithFormat:#"= %ld",(long)numerator];
largeLabel.textAlignment = NSTextAlignmentCenter;
largeLabel.layer.cornerRadius = 3.0;
[containerView addSubview:largeLabel];
NSLog(#"end of createFractPictFor..");
return containerView;
}

Stop iCarousel labels from scrolling

All my labels, done button and back button are scrolling within my icarousel. Is there a way to stop them from moving with the swipe gesture or are they inherently fixed? Or is it a case of taking them out of the function all together?
- (UIView *)carousel:(iCarousel *)carousel viewForItemAtIndex:(NSInteger)index reusingView:(UIView *)view
{
GalleryImage* gi = [self.galleryItem.galleryImages objectAtIndex:index];
UIImageView* imageView = nil;
UILabel* positionLabel = nil;
//UILabel* titleLabel = nil;
UILabel* label = nil;
UIButton* doneButton = nil;
UIView* labelBackground = nil;
UIActivityIndicatorView * spinnerView = nil;
UILabel * errorMessage = nil;
UILabel* titleLabel = nil;
//create new view if no view is available for recycling
if (view == nil) {
view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.carousel.frame.size.width, self.carousel.frame.size.height)] ;
// Image
imageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, self.carousel.frame.size.width, self.carousel.frame.size.height)] ;
imageView.contentMode = UIViewContentModeScaleAspectFit;// UIViewContentModeCenter;
imageView.tag = 1;
// Spiner
spinnerView = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhiteLarge];
[spinnerView setFrame:imageView.frame];
spinnerView.tag = 11;
//Error message
errorMessage = [[UILabel alloc] initWithFrame:spinnerView.frame];
[errorMessage setTextAlignment:NSTextAlignmentCenter];
[errorMessage setTextColor:[UIColor whiteColor]];
errorMessage.font = [UIFont fontWithName:#"Helvetica-Bold" size: 14.0];
errorMessage.tag = 111;
[imageView addSubview:errorMessage];
[imageView addSubview:spinnerView];
[view addSubview:imageView];
// Position
positionLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, self.carousel.frame.size.width, 30)] ;
positionLabel.backgroundColor = [UIColor clearColor];
positionLabel.textAlignment = NSTextAlignmentCenter;
positionLabel.lineBreakMode = NSLineBreakByWordWrapping;
positionLabel.numberOfLines=1;
positionLabel.font = [UIFont fontWithName:#"Helvetica-Bold" size: 14.0];
positionLabel.textColor = [UIColor whiteColor];
positionLabel.tag = 2;
//if(UIInterfaceOrientationPortrait == self.interfaceOrientation)
if(UIInterfaceOrientationIsPortrait(self.interfaceOrientation))
{
// Title
titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(10, 20, self.carousel.frame.size.width - 20, 100)] ;
titleLabel.backgroundColor = [UIColor clearColor];
titleLabel.textAlignment = NSTextAlignmentLeft;
titleLabel.lineBreakMode = NSLineBreakByWordWrapping;
titleLabel.numberOfLines = 4;
titleLabel.font = [UIFont fontWithName:#"Georgia" size: 18.0];
titleLabel.textColor = [UIColor whiteColor];
titleLabel.text = [NSString stringWithFormat:#"%# \n", self.galleryItem.title];
[view addSubview:titleLabel];
/*
* Manage carousel speed and deceleration rate for portrait mode
*/
//self.carousel.decelerationRate = 0.175f;
//self.carousel.scrollSpeed = 0.45f;
}
else if(UIInterfaceOrientationIsLandscape(self.interfaceOrientation))
{
/*
* Manage carousel speed and deceleration rate for landscape mode
*/
//self.carousel.decelerationRate = 0.25f;
//self.carousel.scrollSpeed = 0.55f;
}
labelBackground = [[UIView alloc] initWithFrame:CGRectMake(0, self.carousel.frame.size.height - 160, self.carousel.frame.size.width , 160)] ;
CAGradientLayer *labelBackgroundGradient = [CAGradientLayer layer];
labelBackgroundGradient.frame = labelBackground.bounds;
labelBackgroundGradient.colors = [NSArray arrayWithObjects: (id)[[UIColor clearColor]CGColor],
(id)[[UIColor blackColor]CGColor],
nil
];
[labelBackground.layer insertSublayer:labelBackgroundGradient atIndex:0];
labelBackground.opaque = false;
labelBackground.tag = 5;
// Description
UILabel* label = [[UILabel alloc] initWithFrame:CGRectMake( 0.0f,
0.0f,
labelBackground.bounds.size.width - 20.0f,
labelBackground.bounds.size.height)] ;
label.backgroundColor = [UIColor clearColor];
label.textAlignment = NSTextAlignmentLeft;
label.numberOfLines = 0;
label.font = [label.font fontWithSize:12];
label.textColor = [UIColor whiteColor];
label.text = gi.descriptionGalleryImage;
[label autoAdjustGalleryCaptionSize];
[label setFrame:CGRectMake( label.bounds.origin.x + 10.0f,
label.bounds.origin.y + (labelBackground.bounds.size.height - (label.bounds.size.height + 10.0f)),
label.bounds.size.width,
label.bounds.size.height
)];
label.tag = 6;
[labelBackground addSubview:label];
[view addSubview:labelBackground];
} else {
imageView = (UIImageView *)[view viewWithTag:1];
spinnerView = (UIActivityIndicatorView*)[imageView viewWithTag:11];
errorMessage = (UILabel*) [imageView viewWithTag:111];
positionLabel = (UILabel *)[view viewWithTag:2];
doneButton = (UIButton *)[view viewWithTag:3];
//titleLabel = (UILabel *)[view viewWithTag:4];
labelBackground = (UIView *)[view viewWithTag:5];
label = (UILabel*)[view viewWithTag:6];
}
//Start image loading
ImageSize* imSize = [gi getLargestImage];
[spinnerView startAnimating];
errorMessage.text = #"";
[imageView sd_setImageWithURL:[NSURL URLWithString:imSize.source] placeholderImage:nil options:SDWebImageContinueInBackground progress:^(NSInteger receivedSize, NSInteger expectedSize) {
//progress bar could be here
} completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, NSURL *imageURL) {
//remove progressbar
[spinnerView stopAnimating];
//Display error message
if(error)
{
//errorMessage.text = error.localizedDescription;
errorMessage.text = #"Cannot load image";
}
//Assign image
if(image)
{
[imageView setImage:image];
}
}];
positionLabel.text = [NSString stringWithFormat:#"%d of %ld", (index + 1), (long)[self.galleryItem.galleryImages count]];
UIView* v = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.carousel.frame.size.width, 30)];
v.backgroundColor = [UIColor blackColor];
v.alpha = 0.6;
[view addSubview:v];
[view addSubview:positionLabel];
// Done button
doneButton = [UIButton buttonWithType:UIButtonTypeCustom];
doneButton.frame = CGRectMake(0, 0 , 50, 30);
[doneButton setImage:[UIImage imageNamed:#"back_button_white"] forState:UIControlStateNormal];
doneButton.imageEdgeInsets = UIEdgeInsetsMake(0,5,0,5);
[doneButton setTintColor:[UIColor whiteColor]];
[doneButton addTarget:self action:#selector(back) forControlEvents:UIControlEventTouchUpInside];
[view addSubview:doneButton];
/*
* The description. Why are we setting the frame for the label multiple times?
* We need to do this so it can autoadjust its size and position according
* to the text contained within it.
*/
[label setText:gi.descriptionGalleryImage];
[label autoAdjustGalleryCaptionSize];
[label setFrame:CGRectMake( label.bounds.origin.x + 10.0f,
label.bounds.origin.y + (labelBackground.bounds.size.height - (label.bounds.size.height + 10.0f)),
label.bounds.size.width,
label.bounds.size.height
)];
labelBackground.hidden = self.hideLandscapeDetails;
label.hidden = self.hideLandscapeDetails;
doneButton.hidden = self.hideLandscapeDetails;
positionLabel.hidden = self.hideLandscapeDetails;
v.hidden =self.hideLandscapeDetails;
titleLabel.hidden = self.hideLandscapeDetails;
self.tap1 = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(showHideInfo:)];
[view addGestureRecognizer:self.tap1];
// scroll portrait view to same index
if (self.delegate) {
[self.delegate setReturnIndex:self.carousel.currentItemIndex];
}
id<GAITracker> tracker = [[GAI sharedInstance] defaultTracker];
[tracker send:[[GAIDictionaryBuilder createEventWithCategory:#"Image Gallery" // Event category (required)
action:#"Gallery Image Viewed" // Event action (required)
label:#"Picture swipe" // Event label
value:[NSNumber numberWithLong:(index + 1)]] build]]; // Event value
return view;
}
The label should be above the iCarousel to do what you want, not inside the views of the carousel.

How to improve UITableView scrolling performance? What I am I missing?

I know the basics, what not to do in cellForRowAtIndexPath:, that may cause scrolling performance to be hindered. And I believe I have followed those rules, which is why I have gotten this far. My UITableView is horrible at scrolling, does so very well, but there are times where it stutters for split seconds to seconds, noticeable when it starts to slow down a bit.
Without revealing too much of my code, what here am I doing that could be causing this. I think I have gone over everything and eliminated something that could be causing it, but the problem persists. I feel as if it is something that is obvious and I am overlooking. Help is tremendously appreciated.
Thank you.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *simpleTableIdentifier = #"tweetCell";
TweetCell *cell = (TweetCell *)[tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];
NSDictionary *tweet = _tweets[indexPath.row];
NSString *username = tweet[#"username"];
CGFloat tweetHeight = [tweet[#"contentHeight"] floatValue];
cell.tweet.frame = ({
CGRect frame = cell.tweet.frame;
frame.size.height = tweetHeight + 2;
frame.origin.y = cell.bounds.size.height / 2 - frame.size.height / 2;
frame;
});
cell.tweet.attributedText = tweet[#"attributedText"];
cell.imageView.image = [_profilePhotos[username] valueForKey:#"image"];
cell.date.text = tweet[#"dateString"];
if (tweet[#"media"]) {
cell.tweetImage.image = tweet[#"media"];
cell.tweetImage.hidden = NO;
} else {
cell.tweetImage.image = nil;
cell.tweetImage.hidden = YES;
}
NSMutableAttributedString *attributedText = [tweet[#"attributedText"] mutableCopy];
if ([[NSUserDefaults standardUserDefaults] boolForKey:#"useDynamicTextSize"]) {
UIFont *font = [UIFont preferredFontForTextStyle:UIFontTextStyleBody];
if (cell.tweet.font.pointSize != font.pointSize) {
cell.tweet.font = [UIFont fontWithName:#"HelveticaNeue-Light" size:font.pointSize];
cell.username.font = [UIFont fontWithName:#"HelveticaNeue-Light" size:font.pointSize];
cell.date.font = [UIFont fontWithName:#"HelveticaNeue-Light" size:font.pointSize];
[attributedText addAttribute:NSFontAttributeName value:[UIFont fontWithName:#"HelveticaNeue-Light" size:font.pointSize] range:NSMakeRange(0, attributedText.length)];
} else {
[attributedText addAttribute:NSFontAttributeName value:[UIFont fontWithName:#"HelveticaNeue-Light" size:cell.tweet.font.pointSize] range:NSMakeRange(0, attributedText.length)];
}
} else {
[attributedText addAttribute:NSFontAttributeName value:[UIFont fontWithName:#"HelveticaNeue-Light" size:17] range:NSMakeRange(0, attributedText.length)];
}
cell.tweet.attributedText = attributedText;
cell.username.adjustsFontSizeToFitWidth = YES;
NSString *color = [[NSUserDefaults standardUserDefaults] objectForKey:#"color"];
if ([color isEqualToString:#"automatic"]) {
color = ([[UIScreen mainScreen] brightness] <= .5) ? #"black" : #"white";
}
[cell.imageView.layer setMasksToBounds:YES];
[cell.imageView.layer setCornerRadius:5];
[cell.imageView.layer setBorderColor:[[UIColor colorWithRed:0/255.0 green:0/255.0 blue:0/255.0 alpha:0.55] CGColor]];
[cell.imageView.layer setBorderWidth:0.5];
[cell.tweetImage.layer setMasksToBounds:YES];
[cell.tweetImage.layer setCornerRadius:5];
[cell.tweetImage.layer setBorderColor:[[UIColor colorWithRed:0/255.0 green:0/255.0 blue:0/255.0 alpha:0.55] CGColor]];
[cell.tweetImage.layer setBorderWidth:0.65];
// [UIView beginAnimations:nil context:nil];
// [UIView setAnimationDuration:0.25];
// [UIView setAnimationDelegate:self];
if ([color isEqualToString: #"white"]) {
cell.tweet.textColor =[UIColor blackColor];
cell.date.textColor = [UIColor blackColor];
cell.username.textColor = [UIColor blackColor];
cell.backgroundColor = [UIColor whiteColor];
cell.tweet.linkTextAttributes = #{NSForegroundColorAttributeName:[UIColor blueColor]};
[tweet[#"attributedText"] addAttribute:NSForegroundColorAttributeName
value:[UIColor blackColor]
range:NSMakeRange(0, cell.tweet.attributedText.length)];
} else /*if ([color isEqualToString: #"black"])*/ {
cell.tweet.textColor = [UIColor whiteColor];
cell.date.textColor = [UIColor whiteColor];
cell.username.textColor = [UIColor whiteColor];
cell.backgroundColor = [UIColor colorWithRed:52/255.0 green:52/255.0 blue:52/255.0 alpha:1];
cell.tweet.linkTextAttributes = #{NSForegroundColorAttributeName: [UIColor colorWithRed:0.66 green:0.82 blue:1 alpha:1]};
[tweet[#"attributedText"] addAttribute:NSForegroundColorAttributeName
value:[UIColor whiteColor]
range:NSMakeRange(0, cell.tweet.attributedText.length)];
}
// [UIView commitAnimations];
if (_retweets[_tweets[indexPath.row][#"id"]]) {
if ([color isEqualToString: #"white"]) {
cell.backgroundColor = [UIColor colorWithRed:0.945 green:0.945 blue:0.945 alpha:1];
} else if ([color isEqualToString: #"black"]) {
cell.backgroundColor = [UIColor colorWithRed:0.114 green:0.114 blue:0.114 alpha:1];
}
// cell.separatorInset = UIEdgeInsetsMake(0, 0, 0, cell.bounds.size.width);
} else {
if ([color isEqualToString: #"white"]) {
[cell setBackgroundColor:[UIColor whiteColor]];
} else if ([color isEqualToString: #"black"]) {
cell.backgroundColor = [UIColor colorWithRed:52/255.0 green:52/255.0 blue:52/255.0 alpha:1];
}
// cell.separatorInset = UIEdgeInsetsMake(0, 80, 0, 0);
}
cell.tweet.tag = indexPath.row;
cell.tweetImage.userInteractionEnabled = YES;
cell.tweet.frame = ({
CGRect frame = cell.tweet.frame;
frame.size.height = tweetHeight + 2;
frame.origin.y = cell.bounds.size.height / 2 - frame.size.height / 2;
if (tweet[#"media"]) {
frame.size.width = 164;
} else {
frame.size.width = 224;
}
frame;
});
cell.tweet.delegate = self;
UITapGestureRecognizer *doubleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(retweetTweet:)];
doubleTap.numberOfTapsRequired = 2;
[cell addGestureRecognizer:doubleTap];
[cell.tweet addGestureRecognizer:doubleTap];
UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(showImage:)];
tap.numberOfTapsRequired = 1;
[cell.tweetImage addGestureRecognizer:tap];
UITapGestureRecognizer *tapToViewProfile = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(viewProfile:)];
tap.numberOfTapsRequired = 1;
[cell.imageView setUserInteractionEnabled:YES];
[cell.imageView addGestureRecognizer:tapToViewProfile];
return cell;
}
TweetCell.m:
//
// TweetCell.m
// Khabara
//
// Created by Isa Ranjha on 3/26/14.
// Copyright (c) 2014 Isa Ranjha. All rights reserved.
//
#import "TweetCell.h"
#implementation TweetCell
- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
if (self) {
// Initialization code
}
return self;
}
- (void)layoutSubviews {
[super layoutSubviews];
//self.imageView.frame = CGRectMake(self.imageView.frame.origin.x,self.imageView.frame.origin.y,45,45);
self.imageView.frame = ({
CGRect frame = self.imageView.frame;
frame.size = CGSizeMake(48, 48);
frame.origin.y = self.bounds.size.height / 2 - frame.size.height / 2;
frame;
});
self.imageView.backgroundColor = [UIColor whiteColor];
}
- (void)awakeFromNib
{
//_tweet.numberOfLines = 0;
_tweet.textContainerInset = UIEdgeInsetsZero;
_tweet.canCancelContentTouches = YES;
// _tweet.autoresizingMask = (UIViewAutoresizingFlexibleBottomMargin | UIViewAutoresizingFlexibleTopMargin);
}
- (void)setSelected:(BOOL)selected animated:(BOOL)animated
{
[super setSelected:selected animated:animated];
// Configure the view for the selected state
}
#end
Do not use the set layer corner radius on cell. It make the problem. Instead of this, you better create a frame .png image.

Can't remove a label programmatically

I am printing a label programmatically but I can't remove it from the screen. I have tried removeFromSuperview and lbl1.hidden = YES; and lbl1= nil; but non of them work. It stays on the screen all the time while I can see in the debug it pass from ELSE as in the code below.
Where would be my problem?
-(void)reloadData
lbl1 = [[UILabel alloc] initWithFrame:CGRectMake(320, 530, 550, 200)];
if (result1 > result2 && al == YES)
{
lbl1.userInteractionEnabled = YES;
lbl1.text = #" Warning!! ";
lbl1.tag = 30;
lbl1.font = [UIFont fontWithName:#"Helvetica" size:18.0];
lbl1.textColor = [UIColor redColor];
lbl1.backgroundColor = [UIColor clearColor];
lbl1.lineBreakMode = NSLineBreakByWordWrapping;
lbl1.numberOfLines = 2;
[self addSubview:lbl1];
[lbl1 release];
}
else{
//Non of them is removing the label.
[lbl1 removeFromSuperview];
lbl1= nil;
lbl1.hidden = YES;
}
Every time you go into reloadData, you are creating a new label, so if you go into reload and jump to the else, you are creating a label, and then removing it.
You need to save that label as an instance variable and remove it/add it in your reloadData.
#property(nonatomic, strong) UILabel *lbl1;
And in your code, do this only ONCE:
self.lbl1 = [[[UILabel alloc] initWithFrame:CGRectMake(320, 530, 550, 200)] autorelease];
And in your reloadData do:
-(void)reloadData
lbl1 = [[UILabel alloc] initWithFrame:CGRectMake(320, 530, 550, 200)];
if (result1 > result2 && al == YES)
{
self.lbl1.userInteractionEnabled = YES;
//Etc...
}
else{
[self.lbl1 removeFromSuperview];
}
Try like this:
-(void)reloadData
if(!lbl1)
lbl1 = [[UILabel alloc] initWithFrame:CGRectMake(320, 530, 550, 200)];
if (result1 > result2 && al == YES)
{
lbl1.userInteractionEnabled = YES;
lbl1.text = #" Warning!! ";
lbl1.tag = 30;
lbl1.font = [UIFont fontWithName:#"Helvetica" size:18.0];
lbl1.textColor = [UIColor redColor];
lbl1.backgroundColor = [UIColor clearColor];
lbl1.lineBreakMode = NSLineBreakByWordWrapping;
lbl1.numberOfLines = 2;
[self addSubview:lbl1];
[lbl1 release];
}
else{
//Non of them is removing the label.
[lbl1 removeFromSuperview];
lbl1= nil;
lbl1.hidden = YES;
}
Try to remove like this....
if (result1 > result2 && al == YES)
{
lbl1.userInteractionEnabled = YES;
lbl1.text = #" Warning!! ";
lbl1.tag = 30;
lbl1.font = [UIFont fontWithName:#"Helvetica" size:18.0];
lbl1.textColor = [UIColor redColor];
lbl1.backgroundColor = [UIColor clearColor];
lbl1.lineBreakMode = NSLineBreakByWordWrapping;
lbl1.numberOfLines = 2;
[self addSubview:lbl1];
[lbl1 release];
}
else{
//Non of them is removing the label.
[[self.view viewWithTag:30] removeFromSuperview];
lbl1= nil;
lbl1.hidden = YES;
}

iOS - Reuseable UIView Subclass

I am trying to build a few different UIView subclasses to display information to my app users.
One problem I am having is being able to call the view more than once.
Here is the testing call:
if(rewardsView==nil){
rewardsView = [[RewardsView alloc] init];
[rewardsView showRewardType:RewardsAttack withXP:100 withZBucks:10 isCritical:NO];
}else{
[rewardsView showRewardType:RewardsAttack withXP:200 withZBucks:20 isCritical:NO];
}
Here is the Code to initialize and display:
- (id)init
{
self = [super init];
if (self) {
// Initialization code
self.frame = CGRectMake(0, 0, DEVICE_WIDTH, DEVICE_HEIGHT);
//Z-Bucks
zBucksIcon = [UIImage imageNamed:#"rewards-z-bucks"];
zBucksView = [[UIImageView alloc] initWithFrame:CGRectMake(DEVICE_WIDTH/4-zBucksIcon.size.width/2, DEVICE_HEIGHT/2-zBucksIcon.size.height/2-12, zBucksIcon.size.width, zBucksIcon.size.height+25)];
[zBucksView setImage:zBucksIcon];
zBucksView.alpha = 0;
zBucksLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, zBucksIcon.size.height, zBucksIcon.size.width,25)];
zBucksLabel.backgroundColor = [UIColor clearColor];
zBucksLabel.textAlignment = NSTextAlignmentCenter;
zBucksLabel.font = [UIFont fontWithName:#"Open Sans Condensed" size:24];
zBucksLabel.textColor = [UIColor whiteColor];
zBucksLabel.shadowColor = [UIColor blackColor];
zBucksLabel.shadowOffset = CGSizeMake(1, 1);
[zBucksView addSubview:zBucksLabel];
//XP
xpIcon = [UIImage imageNamed:#"rewards-xp"];
xpView = [[UIImageView alloc] initWithFrame:CGRectMake(DEVICE_WIDTH/4*3-xpIcon.size.width/2, DEVICE_HEIGHT/2-xpIcon.size.height/2-12, xpIcon.size.width, xpIcon.size.height+25)];
[xpView setImage:xpIcon];
xpLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, xpIcon.size.height, xpIcon.size.width,25)];
xpLabel.backgroundColor = [UIColor clearColor];
xpLabel.textAlignment = NSTextAlignmentCenter;
xpLabel.font = [UIFont fontWithName:#"Open Sans Condensed" size:24];
xpLabel.textColor = [UIColor blackColor];
xpLabel.shadowColor = [UIColor whiteColor];
xpLabel.shadowOffset = CGSizeMake(1, 1);
[xpView addSubview:xpLabel];
xpView.alpha = 0;
[self addSubview:zBucksView];
[self addSubview:xpView];
}
return self;
}
-(void)showRewardType:(RewardsType)type withXP:(int)xp withZBucks:(int)zBucks isCritical:(BOOL)critical{
//Set Values
zBucksLabel.text = [NSString stringWithFormat:#"+%i",zBucks];
xpLabel.text = [NSString stringWithFormat:#"+%i",xp];
if(type==RewardsAttack){
}
if(type==RewardsHeal){
}
if(type==RewardsDaily){
}
UIWindow *window = [appDelegate window];
[window addSubview:self];
[self animateRewards];
}
Here is the code that dismisses (after some animation functions are called, not shown here):
-(void)dismissRewards{
[self removeFromSuperview];
//Reset Views
zBucksView.alpha = 0;
xpView.alpha = 0;
[zBucksView setFrame:CGRectMake(DEVICE_WIDTH/4-zBucksIcon.size.width/2, DEVICE_HEIGHT/2-zBucksIcon.size.height/2-12, zBucksIcon.size.width, zBucksIcon.size.height+25)];
[xpView setFrame:CGRectMake(DEVICE_WIDTH/4*3-xpIcon.size.width/2, DEVICE_HEIGHT/2-xpIcon.size.height/2-12, xpIcon.size.width, xpIcon.size.height+25)];
}

Resources