How to show one by one alert at different Time and delay - objective-c

i have to show different type of alerts with different time delay, Display the alerts one by one.one alert show and stay some time then next alert show with delay ....one by one showing This is my code
for ( int j = 0; j < [[CMAppData appData].arrayDistanceAlert count]; j++)
{
CMBeaconsDisplayList * displayData = [[CMAppData appData].arrayDistanceAlert objectAtIndex:j];
if ([displayData.type isEqualToString:#"1"])
{
if ([displayData.type isEqualToString:#"1"] &&[displayData.displayForTime isEqualToString:#"0"]) {
[self performSelector:#selector(dismissAlertView:) withObject:displayData afterDelay:[displayData.displayForTime intValue]*60];
}
else{
double delayInSeconds =[displayData.displayForTime intValue] *10 ;
dispatch_time_t popTime = dispatch_time(DISPATCH_TIME_NOW, delayInSeconds * NSEC_PER_SEC);
dispatch_after(popTime, dispatch_get_main_queue(), ^(void){
NSLog(#"%f",delayInSeconds);
UIAlertView * alert =[[UIAlertView alloc] initWithTitle:displayData.title message:displayData.content delegate:self cancelButtonTitle:#"Cancel" otherButtonTitles:#"pause", nil];
alert.alertViewStyle =UIAlertViewStyleDefault;
[alert show];
[self performSelector:#selector(dismissUIAlertViewType:) withObject:alert afterDelay:15];
});
}
}
else {
if ([displayData.type isEqualToString:#"2"]&&[displayData.displayForTime isEqualToString:#"0"]) {
UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:displayData.title message:displayData.content delegate:self cancelButtonTitle:#"Cancel" otherButtonTitles:nil];
[alertView show];
}
else{
double delayInSeconds =[displayData.displayForTime intValue]*10;
dispatch_time_t popTime = dispatch_time(DISPATCH_TIME_NOW, delayInSeconds * NSEC_PER_SEC);
dispatch_after(popTime, dispatch_get_main_queue(), ^(void){
CustomIOSAlertView *alertView = [[CustomIOSAlertView alloc] init];
UIView *customView = [[UIView alloc] initWithFrame:CGRectMake(0.0, 0.0, [UIScreen mainScreen].bounds.size.width-20, [UIScreen mainScreen].bounds.size.height-100)];
customView.backgroundColor = [UIColor blueColor];
[alertView setContainerView:customView];
UIScrollView *MyScrollVw= [[UIScrollView alloc]initWithFrame:CGRectMake(0 ,0 ,[UIScreen mainScreen].bounds.size.width-20 ,[UIScreen mainScreen].bounds.size.height-100)];
MyScrollVw.delegate= self;
[MyScrollVw setShowsHorizontalScrollIndicator:NO];
[MyScrollVw setShowsVerticalScrollIndicator:YES];
MyScrollVw.scrollEnabled= YES;
MyScrollVw.userInteractionEnabled= YES;
MyScrollVw.contentSize= CGSizeMake([UIScreen mainScreen].bounds.size.width-20 ,1150);//(width,height)
[customView addSubview:MyScrollVw];
UILabel * lblTitle = [[UILabel alloc] initWithFrame:CGRectMake(0.0, 0.0, customView.frame.size.width, 30.0)];
lblTitle.text = displayData.title;
lblTitle.font = [UIFont fontWithName:#"Arial" size:15.0];
lblTitle.numberOfLines = 1;
lblTitle.baselineAdjustment = UIBaselineAdjustmentAlignBaselines; // or UIBaselineAdjustmentAlignCenters, or UIBaselineAdjustmentNone
lblTitle.adjustsFontSizeToFitWidth = YES;
lblTitle.minimumScaleFactor = 10.0f/12.0f;
lblTitle.clipsToBounds = YES;
lblTitle.backgroundColor = [UIColor clearColor];
lblTitle.textColor = [UIColor whiteColor];
lblTitle.textAlignment = NSTextAlignmentCenter;
[MyScrollVw addSubview:lblTitle];
UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(0.0, lblTitle.frame.origin.y+lblTitle.frame.size.height, customView.frame.size.width, 250)];
displayData.image = [displayData.image stringByReplacingOccurrencesOfString:#" " withString:#"%20"];
if(displayData.image && ![displayData.image isKindOfClass:[NSNull class]])
{
[imageView sd_setImageWithURL:[NSURL URLWithString:displayData.image] placeholderImage:nil options:/* DISABLES CODE */ (0) == 0 ? SDWebImageRefreshCached : 0];
}
if ([displayData.image length]==0)
{
[imageView setImage:[UIImage imageNamed:#""]];
}
[MyScrollVw addSubview:imageView];
NSString *videoString = displayData.video;
NSString *lastWord = [[videoString componentsSeparatedByString:#"="] lastObject];
self.playerView = [[YTPlayerView alloc]init];
self.playerView.frame = CGRectMake(0.0, imageView.frame.origin.y+imageView.frame.size.height, customView.frame.size.width, 250.0);
[self.playerView loadWithVideoId:lastWord playerVars:#{#"playsinline":#1}];
[MyScrollVw addSubview:self.playerView];
UIButton * btnPlayAudio = [[UIButton alloc] initWithFrame:CGRectMake(10.0,self.playerView .frame.origin.y+self.playerView .frame.size.height, customView.frame.size.width-20, 30)];
[ btnPlayAudio addTarget:self action:#selector(playAudio:) forControlEvents:UIControlEventTouchUpInside];
btnPlayAudio.backgroundColor = [UIColor redColor];
[btnPlayAudio setTitle:#"Play Audio" forState:UIControlStateNormal];
[btnPlayAudio setAccessibilityValue:displayData.audio];
[MyScrollVw addSubview:btnPlayAudio];
UILabel * lblDescription = [[UILabel alloc] initWithFrame:CGRectMake(10.0,btnPlayAudio .frame.origin.y + btnPlayAudio .frame.size.height, customView.frame.size.width-20, 100)];
lblDescription.text = displayData.content;
lblDescription.font = [UIFont fontWithName:#"Arial" size:15.0];
lblDescription.numberOfLines = 0;
[lblDescription sizeToFit];
lblDescription.baselineAdjustment = UIBaselineAdjustmentAlignBaselines; // or UIBaselineAdjustmentAlignCenters, or UIBaselineAdjustmentNone
lblDescription.adjustsFontSizeToFitWidth = YES;
lblDescription.minimumScaleFactor = 10.0f/12.0f;
lblDescription.clipsToBounds = YES;
lblDescription.backgroundColor = [UIColor clearColor];
lblDescription.textColor = [UIColor whiteColor];
lblDescription.textAlignment = NSTextAlignmentCenter;
[MyScrollVw addSubview:lblDescription];
[alertView show];
[self performSelector:#selector(dismissImageAlertView:) withObject:alertView afterDelay:15];
});
delayInSeconds = delayInSeconds * 10;
}
}
}

setTimeout: allows to run a function once after the interval of time.
alert('Before the Delay');
function afterDelay() {
alert('After the Delay');
}
setTimeout(afterDelay, 2000);

Related

Image not centered vertically in scrollview

What am I missing? The inner scrollview and the image view fill the entire screen. But somehow my image is not centered. The top left corner of the image starts in the center of the view, but I would like to have the image nicely centered. Also during zooming.
-(void)prepareScrollView
{
for(int i =0;i<[self.layoverPhotoAssets count];i++){
PHAsset *asset = self.layoverPhotoAssets[i];
FMImageZoomViewController *zoomController = [[FMImageZoomViewController alloc] init];
// UIImageView *imageView = [[UIImageView alloc] init];
int x = self.scrollView.frame.size.width * i;
zoomController.view.frame = CGRectMake(x, 0, self.scrollView.frame.size.width, self.scrollView.frame.size.height);
//zoomController.view.frame = CGRectMake(0,0,self.view.bounds.size.width,self.view.bounds.size.height);
[self.scrollView addSubview:zoomController.view];
zoomController.zoomScroller.delegate = self;
zoomController.imageView.tag = 1;
[self.zoomControllers addObject:zoomController];
PHImageRequestOptions *options = [[PHImageRequestOptions alloc] init];
options.resizeMode = PHImageRequestOptionsResizeModeFast;
options.deliveryMode = PHImageRequestOptionsDeliveryModeHighQualityFormat; //I only want the highest possible quality
options.synchronous = NO;
options.networkAccessAllowed = YES;
[[PHImageManager defaultManager] requestImageForAsset:asset targetSize:zoomController.zoomScroller.frame.size contentMode:PHImageContentModeAspectFill options:options resultHandler:^(UIImage *result, NSDictionary *info) {
dispatch_async(dispatch_get_main_queue(), ^{
if(result){
zoomController.imageView.image = result;
zoomController.imageView.backgroundColor = [UIColor redColor];
}
});
}];
//self.scrollView.contentSize= ;
}
[self.scrollView setContentSize:CGSizeMake(self.scrollView.frame.size.width * [self.layoverPhotoAssets count], 0)];
[self scrollToAsset:self.selectedAsset];
}
Consider:
zoomController.view.frame = CGRectMake(x, 0, self.scrollView.frame.size.width, self.scrollView.frame.size.height);
[self.scrollView addSubview:zoomController.view];
That cannot be right. If zoomController.view is to be a subview of self.scrollView, its frame within self.scrollView is in terms of the bounds of self.scrollView, not the frame of self.scrollView.
Solved it like this:
-(void)prepareScrollView
{
for(int i =0;i<[self.layoverPhotoAssets count];i++){
PHAsset *asset = self.layoverPhotoAssets[i];
FMImageZoomViewController *zoomController = [[FMImageZoomViewController alloc] init];
// UIImageView *imageView = [[UIImageView alloc] init];
int x = self.scrollView.frame.size.width * i;
zoomController.view.frame = CGRectMake(x, 0, self.scrollView.bounds.size.width, self.scrollView.bounds.size.height);
//zoomController.view.frame = CGRectMake(0,0,self.view.bounds.size.width,self.view.bounds.size.height);
[self.scrollView addSubview:zoomController.view];
zoomController.zoomScroller.delegate = self;
[self.zoomControllers addObject:zoomController];
PHImageRequestOptions *options = [[PHImageRequestOptions alloc] init];
options.resizeMode = PHImageRequestOptionsResizeModeExact;
options.deliveryMode = PHImageRequestOptionsDeliveryModeHighQualityFormat; //I only want the highest possible quality
options.synchronous = NO;
options.networkAccessAllowed = YES;
[[PHImageManager defaultManager] requestImageForAsset:asset targetSize:zoomController.zoomScroller.bounds.size contentMode:PHImageContentModeAspectFit options:options resultHandler:^(UIImage *result, NSDictionary *info) {
dispatch_async(dispatch_get_main_queue(), ^{
if(result){
zoomController.imageView = [[UIImageView alloc] initWithImage:result];
zoomController.imageView.frame = zoomController.zoomScroller.bounds;
[zoomController.imageView setContentMode:UIViewContentModeScaleAspectFit];
zoomController.imageView.clipsToBounds = YES;
[zoomController.imageView setCenter: self.scrollView.center];
zoomController.imageView.tag = 1;
[zoomController.zoomScroller addSubview:zoomController.imageView];
// zoomController.imageView.contentMode = UIViewContentModeCenter;
// if (zoomController.imageView.bounds.size.width > result.size.width && zoomController.imageView.bounds.size.height > result.size.height) {
// zoomController.imageView.contentMode = UIViewContentModeScaleAspectFit;
// }
}
});
}];
//self.scrollView.contentSize= ;
}
[self.scrollView setContentSize:CGSizeMake(self.scrollView.frame.size.width * [self.layoverPhotoAssets count], 0)];
[self scrollToAsset:self.selectedAsset];
}

UIImageView transparent black on a cell bug (Objective-C)

I have a UIImageView installed on my cell, it is transparent black, but when I scroll the CollectionView and I raise my UIImageView but there is always more to transprence.
See picture :
1 - I is not even scroll
2 - After a scroll
My code :
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"pictureCell";
MSContestListCollectionViewCell *cell = (MSContestListCollectionViewCell *)[self.collectionView dequeueReusableCellWithReuseIdentifier:CellIdentifier forIndexPath:indexPath];
cell.titleContest.adjustsFontSizeToFitWidth = YES;
cell.titleContest.minimumScaleFactor = 0.5;
cell.pictureImageView.layer.cornerRadius = 5;
cell.pictureImageView.clipsToBounds = YES;
cell.titleView.layer.cornerRadius = 5;
cell.titleView.clipsToBounds = YES;
switch (_segmentedControl.selectedSegmentIndex) {
case 0: {
NSDictionary *searchResult = [self.readArray objectAtIndex:indexPath.item];
NSString *stringImage = [searchResult objectForKey:#"featuredImage"];
NSString *image = [NSString stringWithFormat:#"https://srv.mediaswapp.com/%#", stringImage];
[cell.pictureImageView sd_setImageWithURL:[NSURL URLWithString:image]
placeholderImage:[UIImage imageNamed:#"placeholder.png"]];
cell.statusContest.text = [searchResult objectForKey:#"status"];
if ([[searchResult objectForKey:#"status"] isEqualToString:#"PAUSE"]) {
cell.titleContest.text = [NSString stringWithFormat:#"Concours en pause"];
/*
UIView *view = [[UIView alloc] initWithFrame:CGRectMake(cell.contentView.frame.origin.x, cell.contentView.frame.origin.y, cell.contentView.frame.size.width, cell.contentView.frame.size.height)];
view.backgroundColor = [[UIColor blackColor] colorWithAlphaComponent:0.8f];
view.layer.cornerRadius = 5;
view.clipsToBounds = YES;
[cell.contentView addSubview:view];
*/
UIImageView *imagecellPause = [[UIImageView alloc] initWithFrame:CGRectMake(cell.contentView.frame.origin.x, cell.contentView.frame.origin.y, cell.contentView.frame.size.width, cell.contentView.frame.size.height)];
UIImage *cellImage = [UIImage imageNamed:#"cell-pause.png"];
imagecellPause.image = cellImage;
[cell.contentView addSubview:imagecellPause];
UIImageView *imagePause = [[UIImageView alloc] initWithFrame:CGRectMake(69.5, 69.5, 25, 25)];
UIImage *image = [UIImage imageNamed:#"Pause Filled-50 (1).png"];
imagePause.image = image;
[cell.contentView addSubview:imagePause];
UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(10, 90, 150, 50)];
label.textColor = [UIColor whiteColor];
label.font = [UIFont fontWithName:#"Avenir-Book" size:12];
label.text = [searchResult objectForKey:#"description"];
label.adjustsFontSizeToFitWidth = YES;
label.minimumScaleFactor = 0.5;
[cell.contentView addSubview:label];
UILabel *label2 = [[UILabel alloc] initWithFrame:CGRectMake(10, 105, 150, 50)];
label2.textColor = [UIColor whiteColor];
label2.font = [UIFont fontWithName:#"Avenir-Black" size:15];
label2.text = #"Concours en pause";
[cell.contentView addSubview:label2];
cell.titleView.hidden = YES;
} else {
cell.titleContest.text = [searchResult objectForKey:#"description"];
}
break;
}
case 1: {
NSDictionary *searchResult2 = [self.readArrayWinner objectAtIndex:indexPath.item];
NSString *stringImage = [searchResult2 objectForKey:#"featuredImage"];
NSString *image = [NSString stringWithFormat:#"https://srv.mediaswapp.com/%#", stringImage];
[cell.pictureImageView sd_setImageWithURL:[NSURL URLWithString:image]
placeholderImage:[UIImage imageNamed:#"placeholder.png"]];
cell.titleContest.text = [searchResult2 objectForKey:#"description"];
cell.statusContest.text = [searchResult2 objectForKey:#"status"];
NSLog(#"le gagnant : %#", [searchResult2 valueForKeyPath:#"winners.name"]);
break;
}
case 2: {
NSDictionary *searchResult3 = [self.readArrayPhotos objectAtIndex:indexPath.item];
NSString *stringImage = [searchResult3 objectForKey:#"featuredImage"];
NSString *image = [NSString stringWithFormat:#"https://srv.mediaswapp.com/%#", stringImage];
[cell.pictureImageView sd_setImageWithURL:[NSURL URLWithString:image]
placeholderImage:[UIImage imageNamed:#"placeholder.png"]];
cell.titleContest.text = [searchResult3 objectForKey:#"description"];
cell.statusContest.text = [searchResult3 objectForKey:#"status"];
break;
}
default:
break;
}
[_activity stopAnimating];
_label1.hidden = YES;
return cell;
}
Your CellForRowAtIndexPath should never contain addSubView, since everytime the cell is reloaded it will add the view. Thats why it keeps getting darker, it just adds view onto view.
Instead you should subclass your UICollectionViewCell and add the view in the subclass.

tableView cells refreshing auto?

When I scroll in my tableview the cell seems to be refreshing because the duration in the left corner sometimes shows up and sometimes it doesn't. Does anyone know how I can make it stay at one place ?
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
// Getting XML data
static NSString *CellIdentifier = #"Cell";
ChanelFeeds *currentFeed = [[xmlParser feeds] objectAtIndex:indexPath.row];
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil)
{
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier] autorelease];
// Adding thumbnail
CGRect imageFrame = CGRectMake(0, 0, 120, 90);
self.customImage = [[UIImageView alloc] initWithFrame:imageFrame];
[cell.contentView addSubview:self.customImage];
[self.customImage release];
// Title Placement
CGRect contentFrame = CGRectMake(122, 2, 198, 45);
UILabel *title = [[UILabel alloc] initWithFrame:contentFrame];
title.tag = 0011;
title.numberOfLines = 2;
title.font = [UIFont boldSystemFontOfSize:14];
[cell.contentView addSubview:title];
[title release];
// Views Placement
CGRect contentFrame2 = CGRectMake(127, 70, 180, 15);
UILabel *title2 = [[UILabel alloc] initWithFrame:contentFrame2];
title2.tag = 0012;
title2.numberOfLines = 1;
title2.font = [UIFont italicSystemFontOfSize:14];
[cell.contentView addSubview:title2];
[title2 release];
// Dislike Bar
CGRect contentFrame4 = CGRectMake(123, 54, 190, 5);
UIImageView *imv2 = [[UIImageView alloc]initWithFrame:contentFrame4];
imv2.image=[UIImage imageNamed:#"redbar.png"];
[cell.contentView addSubview:imv2];
[imv2 release];
// Like Bar
CGRect contentFrame3 = CGRectMake(123, 54, 38 * [currentFeed rating].floatValue, 5);
UIImageView *imv = [[UIImageView alloc]initWithFrame:contentFrame3];
imv.image=[UIImage imageNamed:#"greenbar.png"];
[cell.contentView addSubview:imv];
[imv release];
// No Likes / Disabled rating
if([currentFeed rating].intValue == 0) {
UIImageView *imv3 = [[UIImageView alloc]initWithFrame:contentFrame4];
imv3.image=[UIImage imageNamed:#"graybar.png"];
[cell.contentView addSubview:imv3];
[imv3 release];}
// Formatting duration
totalTime = [self timeFormatted:([currentFeed duration].intValue)-1];
NSString *word = #":00:";
if ([totalTime rangeOfString:word].location == NSNotFound) {
totalTime = [totalTime stringByReplacingOccurrencesOfString:#"00:" withString:#""];
totalTime = [totalTime stringByReplacingOccurrencesOfString:#"01:" withString:#"1:"];
totalTime = [totalTime stringByReplacingOccurrencesOfString:#"02:" withString:#"2:"];
totalTime = [totalTime stringByReplacingOccurrencesOfString:#"03:" withString:#"3:"];
totalTime = [totalTime stringByReplacingOccurrencesOfString:#"04:" withString:#"4:"];
totalTime = [totalTime stringByReplacingOccurrencesOfString:#"05:" withString:#"5:"];
totalTime = [totalTime stringByReplacingOccurrencesOfString:#"06:" withString:#"6:"];
totalTime = [totalTime stringByReplacingOccurrencesOfString:#"07:" withString:#"7:"];
totalTime = [totalTime stringByReplacingOccurrencesOfString:#"08:" withString:#"8:"];
totalTime = [totalTime stringByReplacingOccurrencesOfString:#"09:" withString:#"9:"];}
// Adding time/Black border
if([totalTime length] <= 4) {
CGRect contentFrame6 = CGRectMake(0, 66, 30, 13);
CGRect contentFrame7 = CGRectMake(6, 65, 60, 15);
title3 = [[UILabel alloc] initWithFrame:contentFrame7];
imv5 = [[UIImageView alloc]initWithFrame:contentFrame6];
imv5.image=[UIImage imageNamed:#"blackBorder.png"];
imv5.alpha = 0.8;
title3.tag = 0013;
title3.numberOfLines = 1;
title3.font = [UIFont boldSystemFontOfSize:10];
//[cell.contentView addSubview:title3];
//[cell.contentView addSubview:imv5];
title3.text = totalTime;
title3.textColor = [UIColor whiteColor];
title3.backgroundColor = [UIColor clearColor];
imv5.backgroundColor = [UIColor clearColor];
[cell.contentView addSubview:imv5];
[cell.contentView addSubview:title3];
[imv5 release];
[title3 release];
} else if([totalTime length] == 5) {
CGRect contentFrame6 = CGRectMake(87, 64, 30, 13);
CGRect contentFrame7 = CGRectMake(90, 63, 60, 15);
title3 = [[UILabel alloc] initWithFrame:contentFrame7];
imv5 = [[UIImageView alloc]initWithFrame:contentFrame6];
imv5.image=[UIImage imageNamed:#"blackBorder.png"];
imv5.alpha = 0.8;
title3.tag = 0013;
title3.numberOfLines = 1;
title3.font = [UIFont systemFontOfSize:10];
title3.text = totalTime;
title3.textColor = [UIColor whiteColor];
title3.backgroundColor = [UIColor clearColor];
imv5.backgroundColor = [UIColor clearColor];
[cell.contentView addSubview:imv5];
[cell.contentView addSubview:title3];
[imv5 release];
[title3 release];
}
}
// Adding title/views
UILabel *title = (UILabel *)[cell.contentView viewWithTag:0011];
UILabel *title2 = (UILabel *)[cell.contentView viewWithTag:0012];
// Formatting views
NSNumberFormatter * f = [[NSNumberFormatter alloc] init];
[f setNumberStyle:NSNumberFormatterDecimalStyle];
NSNumber * formattedViews = [f numberFromString:[currentFeed views]];
[f release];
NSNumber *firstNumber = formattedViews;
NSNumberFormatter *formatter = [[NSNumberFormatter alloc] init];
[formatter setNumberStyle:NSNumberFormatterDecimalStyle];
NSString *convertNumber = [formatter stringForObjectValue:firstNumber];
// Reoloring / Adding value to text
title2.text = [NSString stringWithFormat:#"%# views", convertNumber];
title2.backgroundColor = [UIColor clearColor];
title2.textColor= [UIColor whiteColor];
title.text = [currentFeed title];
title.backgroundColor = [UIColor clearColor];
title.textColor = [UIColor whiteColor];
// Caching images and adding them as thumbnails
NSString *directoryPath = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) objectAtIndex:0];
NSURL *imgURL = [currentFeed thumbnailURL];
NSArray *parts = [[NSString stringWithFormat:#"%#", imgURL] componentsSeparatedByString:#"/"];
NSString *imageName = [parts objectAtIndex:[parts count]-2];
NSString *filePath = [directoryPath stringByAppendingPathComponent:imageName];
UIImage *myview = [UIImage imageWithContentsOfFile:filePath];
if(myview){
// Adding thumbnail
cell.imageView.image = myview;
//Adding duration time/blakhover
[cell.contentView addSubview:imv5];
[cell.contentView addSubview:title3];
}else{
NSData* imageDataTemp = [[NSData alloc] initWithContentsOfURL:[currentFeed thumbnailURL]];
if(imageDataTemp){
cell.imageView.image = [UIImage imageWithData:imageDataTemp];
}else{
cell.imageView.image = [UIImage imageNamed:#"youtubeLogo.png"];
}
//Adding duration time/blakhover over the image
[cell.contentView addSubview:imv5];
[cell.contentView addSubview:title3];
}
return cell;
}
EDIT:
After following you guys this happened
I tried using your solution and it ended up like this with this code, but when I scroll down everything goes back to normal. Any ideas ?
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
// Getting XML data
static NSString *CellIdentifier = #"Cell";
ChanelFeeds *currentFeed = [[xmlParser feeds] objectAtIndex:indexPath.row];
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil)
{
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier] autorelease];
// Adding thumbnail
CGRect imageFrame = CGRectMake(0, 0, 120, 90);
self.customImage = [[UIImageView alloc] initWithFrame:imageFrame];
[cell.contentView addSubview:self.customImage];
[self.customImage release];
// Title Placement
CGRect contentFrame = CGRectMake(122, 2, 198, 45);
UILabel *title = [[UILabel alloc] initWithFrame:contentFrame];
title.tag = 0011;
title.numberOfLines = 2;
title.font = [UIFont boldSystemFontOfSize:14];
[cell.contentView addSubview:title];
[title release];
// Views Placement
CGRect contentFrame2 = CGRectMake(127, 70, 180, 15);
UILabel *title2 = [[UILabel alloc] initWithFrame:contentFrame2];
title2.tag = 0012;
title2.numberOfLines = 1;
title2.font = [UIFont italicSystemFontOfSize:14];
[cell.contentView addSubview:title2];
[title2 release];
// Dislike Bar
CGRect contentFrame4 = CGRectMake(123, 54, 190, 5);
UIImageView *imv2 = [[UIImageView alloc]initWithFrame:contentFrame4];
imv2.image=[UIImage imageNamed:#"redbar.png"];
[cell.contentView addSubview:imv2];
[imv2 release];
// Like Bar
CGRect contentFrame3 = CGRectMake(123, 54, 38 * [currentFeed rating].floatValue, 5);
UIImageView *imv = [[UIImageView alloc]initWithFrame:contentFrame3];
imv.image=[UIImage imageNamed:#"greenbar.png"];
[cell.contentView addSubview:imv];
[imv release];
// No Likes / Disabled rating
if([currentFeed rating].intValue == 0) {
UIImageView *imv3 = [[UIImageView alloc]initWithFrame:contentFrame4];
imv3.image=[UIImage imageNamed:#"graybar.png"];
[cell.contentView addSubview:imv3];
[imv3 release];}
// Formatting views
NSNumberFormatter * f = [[NSNumberFormatter alloc] init];
[f setNumberStyle:NSNumberFormatterDecimalStyle];
NSNumber * formattedViews = [f numberFromString:[currentFeed views]];
[f release];
NSNumber *firstNumber = formattedViews;
NSNumberFormatter *formatter = [[NSNumberFormatter alloc] init];
[formatter setNumberStyle:NSNumberFormatterDecimalStyle];
NSString *convertNumber = [formatter stringForObjectValue:firstNumber];
// Reoloring / Adding value to text
title2.text = [NSString stringWithFormat:#"%# views", convertNumber];
title2.backgroundColor = [UIColor clearColor];
title2.textColor= [UIColor whiteColor];
title.text = [currentFeed title];
title.backgroundColor = [UIColor clearColor];
title.textColor = [UIColor whiteColor];
}
else
{
// Adding title/views
// Caching images and adding them as thumbnails
NSString *directoryPath = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) objectAtIndex:0];
NSURL *imgURL = [currentFeed thumbnailURL];
NSArray *parts = [[NSString stringWithFormat:#"%#", imgURL] componentsSeparatedByString:#"/"];
NSString *imageName = [parts objectAtIndex:[parts count]-2];
NSString *filePath = [directoryPath stringByAppendingPathComponent:imageName];
UIImage *myview = [UIImage imageWithContentsOfFile:filePath];
if(myview){
// Adding thumbnail
cell.imageView.image = myview;
//Adding duration time/blakhover
// Formatting duration
totalTime = [self timeFormatted:([currentFeed duration].intValue)-1];
NSString *word = #":00:";
if ([totalTime rangeOfString:word].location == NSNotFound) {
totalTime = [totalTime stringByReplacingOccurrencesOfString:#"00:" withString:#""];
totalTime = [totalTime stringByReplacingOccurrencesOfString:#"01:" withString:#"1:"];
totalTime = [totalTime stringByReplacingOccurrencesOfString:#"02:" withString:#"2:"];
totalTime = [totalTime stringByReplacingOccurrencesOfString:#"03:" withString:#"3:"];
totalTime = [totalTime stringByReplacingOccurrencesOfString:#"04:" withString:#"4:"];
totalTime = [totalTime stringByReplacingOccurrencesOfString:#"05:" withString:#"5:"];
totalTime = [totalTime stringByReplacingOccurrencesOfString:#"06:" withString:#"6:"];
totalTime = [totalTime stringByReplacingOccurrencesOfString:#"07:" withString:#"7:"];
totalTime = [totalTime stringByReplacingOccurrencesOfString:#"08:" withString:#"8:"];
totalTime = [totalTime stringByReplacingOccurrencesOfString:#"09:" withString:#"9:"];}
// Adding time/Black border
if([totalTime length] <= 4) {
CGRect contentFrame6 = CGRectMake(0, 66, 30, 13);
CGRect contentFrame7 = CGRectMake(6, 65, 60, 15);
title3 = [[UILabel alloc] initWithFrame:contentFrame7];
imv5 = [[UIImageView alloc]initWithFrame:contentFrame6];
imv5.image=[UIImage imageNamed:#"blackBorder.png"];
imv5.alpha = 0.8;
title3.tag = 0013;
title3.numberOfLines = 1;
title3.font = [UIFont boldSystemFontOfSize:10];
//[cell.contentView addSubview:title3];
//[cell.contentView addSubview:imv5];
title3.text = totalTime;
title3.textColor = [UIColor whiteColor];
title3.backgroundColor = [UIColor clearColor];
imv5.backgroundColor = [UIColor clearColor];
[cell.contentView addSubview:imv5];
[cell.contentView addSubview:title3];
[imv5 release];
[title3 release];
} else if([totalTime length] == 5) {
CGRect contentFrame6 = CGRectMake(87, 64, 30, 13);
CGRect contentFrame7 = CGRectMake(90, 63, 60, 15);
title3 = [[UILabel alloc] initWithFrame:contentFrame7];
imv5 = [[UIImageView alloc]initWithFrame:contentFrame6];
imv5.image=[UIImage imageNamed:#"blackBorder.png"];
imv5.alpha = 0.8;
title3.tag = 0013;
title3.numberOfLines = 1;
title3.font = [UIFont systemFontOfSize:10];
title3.text = totalTime;
title3.textColor = [UIColor whiteColor];
title3.backgroundColor = [UIColor clearColor];
imv5.backgroundColor = [UIColor clearColor];
[cell.contentView addSubview:imv5];
[cell.contentView addSubview:title3];
[imv5 release];
[title3 release];
}
}else{
NSData* imageDataTemp = [[NSData alloc] initWithContentsOfURL:[currentFeed thumbnailURL]];
if(imageDataTemp){
cell.imageView.image = [UIImage imageWithData:imageDataTemp];
}else{
cell.imageView.image = [UIImage imageNamed:#"youtubeLogo.png"];
}
//Adding duration time/blakhover over the image
[cell.contentView addSubview:imv5];
[cell.contentView addSubview:title3];
}
}
return cell;
}
You set your time label's text inside of if (cell == nil) block, which means that it stays the same when cell is reused.

UIButtons in UIScrollView stop working after 3 rows

I have a UIScrollView with a list of UIViews within it. Bit like a fancy tableview. One issue I am having is the buttons work ok for the first 3 'rows' in the scrollview, but none of the buttons after this respond when i scroll down. Looks like its working ok for the buttons that show on screen when the view has loaded but anything further down when i scroll will now respond at all...
code from within the uiview repeated within uiscrollview
-(void)addButtons
{
UIButton *visiteWebSite = [UIButton buttonWithType:UIButtonTypeCustom];
[visiteWebSite addTarget:self
action:#selector(visitSite:)
forControlEvents:UIControlEventTouchDown];
[visiteWebSite setTintColor:[UIColor colorWithRed:247 green:143 blue:30 alpha:1.0]];
visiteWebSite.frame = CGRectMake(440.0, 10.0, 120.0, 26.0);
if(![self IsPhone5]) {
visiteWebSite.frame = CGRectMake(350.0, 10.0, 120.0, 26.0);
}
//visiteWebSite.backgroundColor = [UIColor orangeColor]; //[UIColor colorWithRed:247 green:143 blue:30 alpha:1.0];
[visiteWebSite setBackgroundImage:[UIImage imageNamed:#"orangeBG"] forState:UIControlStateNormal];
[visiteWebSite setTitle:#"VISITE WEBSITE" forState:UIControlStateNormal];
visiteWebSite.titleLabel.font = [UIFont fontWithName:#"arial" size:12];
[self addSubview:visiteWebSite];
UIButton *getDirections = [UIButton buttonWithType:UIButtonTypeCustom];
[getDirections addTarget:self
action:#selector(getDirections:)
forControlEvents:UIControlEventTouchDown];
[getDirections setTitle:#"GET DIRECTIONS" forState:UIControlStateNormal];
getDirections.titleLabel.font = [UIFont fontWithName:#"arial" size:12];
getDirections.frame = CGRectMake(440.0, 46.0, 120.0, 26.0);
if(![self IsPhone5]) {
getDirections.frame = CGRectMake(350.0, 46.0, 120.0, 26.0);
}
[getDirections setBackgroundImage:[UIImage imageNamed:#"orangeBG"] forState:UIControlStateNormal];
[self addSubview:getDirections];
}
Code from Parent View containing the UIScrollView
-(void)performSearch
{
[self.loadinglabel removeFromSuperview];
NSString* searchTerm = txtSearch.text;
searchTerm = [searchTerm stringByReplacingOccurrencesOfString:#" " withString:#""];
NSData *urldata = [NSData dataWithContentsOfURL:[NSURL URLWithString:[NSString stringWithFormat:#"http://www.collectioncosmetics.co.uk/storelocatorapi?store=%#",searchTerm]]];
NSString *json = [[NSString alloc] initWithData:urldata encoding:NSUTF8StringEncoding];
SBJsonParser *jsonParser = [[SBJsonParser alloc] init];
NSArray *jsonObjects = [jsonParser objectWithString:json];
float y = 0;
float height = 84;
if([jsonObjects count] < 1) {
UIAlertView* alertView = [[UIAlertView alloc] initWithTitle:#"NO RESULTS" message:#"There are no stores near the postcode you searched" delegate:self cancelButtonTitle:#"Ok" otherButtonTitles:nil, nil];
[alertView show];
[self back:nil];
return;
}
for (int i = 0; i < [jsonObjects count]; i++)
{
NSDictionary *dict = [jsonObjects objectAtIndex:i];
CARStoreResult* result = [[CARStoreResult alloc] initWithFrame:CGRectMake(0, height*i, tv.frame.size.width, height)];
result.name = [dict objectForKey:#"name"];
result.street = [dict objectForKey:#"street"];
result.area = [dict objectForKey:#"area"];
result.county = [dict objectForKey:#"County"];
result.postcode = [dict objectForKey:#"PostCode"];
result.distance = [dict objectForKey:#"distance"];
result.usersPostCode = searchTerm;
result.y = y;
result.num = i;
y = y + height;
[result build];
[tv addSubview:result];
}
[tv setFrame:CGRectMake(tv.frame.origin.x, tv.frame.origin.y, tv.frame.size.width, height*[jsonObjects count])];
[self.scrollView setContentSize:tv.frame.size];
[Flurry logEvent:#"Store Locator"];
}
FIXED!
instead of adding the views to a view within the scrollview i added them just directly to the scrollview. with the scrollview having scrollView.canCancelContentTouches set to NO.
//[tv addSubview:result];
[self.scrollView addSubview:result];
}
//[tv setFrame:CGRectMake(tv.frame.origin.x, tv.frame.origin.y, tv.frame.size.width, height*[jsonObjects count])];
[self.scrollView setContentSize:CGSizeMake(320, height*[jsonObjects count])];
//[self.scrollView setContentSize:tv.frame.size];
[Flurry logEvent:#"Store Locator"];

How do I remove or stop a button in table view from working after registered amount of taps

I have a button in a table view that adds rows. After a maximum of five rows I want to stop the user adding anymore. Currently I show an alert after button recives 5 taps.
How can I stop the user from using the button past this point? Setting to hidden wont work as its a custom subclass and property hidden is not found on the class
- (void)viewDidLoad
{
[super viewDidLoad];
UILabel *titleLabel = [[UILabel alloc] initWithFrame:CGRectZero];
titleLabel.backgroundColor = [UIColor clearColor];
titleLabel.textColor = [UIColor whiteColor];
titleLabel.shadowColor = [UIColor darkGrayColor];
titleLabel.text = self.distributionBoard.dbRef;
titleLabel.font = [UIFont boldSystemFontOfSize:15.0f];
[titleLabel sizeToFit];
self.navigationItem.titleView = titleLabel;
self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:self action:#selector(doneButtonPressed:)];
// Add new appliance button to the table view's footer view
UIView *footerView = [[UIView alloc] initWithFrame:CGRectMake(10.0f, 0, 300.0f, 100.0f)];
footerView.backgroundColor = [UIColor clearColor];
UIButton *newBoardButton = [UIButton buttonWithType:UIButtonTypeContactAdd];
CGRect buttonFrame = newBoardButton.frame;
buttonFrame.origin.x = footerView.frame.size.width - buttonFrame.size.width;
newBoardButton.frame = buttonFrame;
[newBoardButton addTarget:self action:#selector(addCircuitButtonPressed:) forControlEvents:UIControlEventTouchUpInside];
[footerView addSubview:newBoardButton];
self.tableView.tableFooterView = footerView;
}
.....
////limit to five appliances
- (void)addCircuitButtonPressed:(id)sender {
LogCmd();
Circuit *circuit = [[ICCircuitManager manager] newCircuit];
circuit.distributionBoard = self.distributionBoard;
circuit.circuitReference = [NSString stringWithFormat:#"%d", [self.circuits count] + 1];
circuit.createdAt = [NSDate date];
circuit.modifiedAt = [NSDate date];
[self.distributionBoard addCircuitsObject:circuit];
[self loadData];
[self.tableView reloadData];
{
m_buttonTouchCount++;
if ( m_buttonTouchCount == 4)
{
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"iCertifi"
message:#"Maximum number of appliances reached"
delegate:nil
cancelButtonTitle:#"Ok"
otherButtonTitles:nil];
[alert show];
// m_buttonTouchCount = 0; // reset to 0 here if required.
}
}
}
Where you have AlertView you can type this code to disable button:
[(UIButton *)sender setEnabled:NO];
or to hide button:
[(UIButton *)sender setHidden:YES];