I'm trying to add title3 above the picture and into the black box on the left side.
Such as this
I'm not sure why it's not working but any help would be greatly appreciated. I'm pretty new to Objective-C so if you see anything that I'm doing wrong or that could be done better don't hesitate to tell me.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
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];
CGRect imageFrame = CGRectMake(0, 0, 120, 90);
self.customImage = [[UIImageView alloc] initWithFrame:imageFrame];
[cell.contentView addSubview:self.customImage];
[self.customImage release];
// Title
CGRect contentFrame = CGRectMake(122, 2, 198, 45);
UILabel *title = [[UILabel alloc] initWithFrame:contentFrame];
title.tag = 0011;
title.numberOfLines = 2;
title.backgroundColor = [UIColor clearColor];
title.textColor = [UIColor whiteColor];
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.backgroundColor = [UIColor clearColor];
title2.textColor = [UIColor whiteColor];
title2.font = [UIFont italicSystemFontOfSize:14];
[cell.contentView addSubview:title2];
[title2 release];
//Here's where the fails come in
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){
cell.imageView.image = myview;
//Add Black fucking border and stuff here!!!
}else{
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
NSData* data = [NSData dataWithContentsOfURL:[currentFeed thumbnailURL]];
UIImage *thumbnail = [UIImage imageWithData:data];
cell.imageView.image = thumbnail;
[self.feedsTableView beginUpdates];
[self.feedsTableView reloadRowsAtIndexPaths:[NSArray arrayWithObjects:indexPath, nil]
withRowAnimation:UITableViewRowAnimationNone];
[self.feedsTableView endUpdates];
});
}
CGRect contentFrame6 = CGRectMake(0, 67, 35, 13);
UIImageView *imv5 = [[UIImageView alloc]initWithFrame:contentFrame6];
imv5.alpha = 1;
imv5.tag = 0014;
[cell.contentView addSubview:imv5];
imv5.image = [UIImage imageNamed:#"blackBorder.png"];
imv5.backgroundColor = [UIColor clearColor];
// 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]; convertedNumber = [formatter stringForObjectValue:firstNumber];
// Basic stuff
title.text = [currentFeed title];
title2.text = [NSString stringWithFormat:#"%# views", convertedNumber];
totalTime = [self timeFormatted:([currentFeed duration].intValue)-1];
NSString *word = #":00:";
NSString *word2 = #"00:00:";
if ([totalTime rangeOfString:word].location == NSNotFound || [totalTime rangeOfString:word2].location != NSNotFound) {
totalTime = [totalTime stringByReplacingOccurrencesOfString:#"00:00:" withString:#"0:"];
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:"];
}
CGRect contentFrame7 = CGRectMake(4, 65, 60, 15);
UILabel *title3 = [[UILabel alloc] initWithFrame:contentFrame7];
title3.backgroundColor = [UIColor clearColor];
title3.textColor = [UIColor whiteColor];
title3.tag = 0013;
if([totalTime length] >= 5) {
CGRect contentFrame7 = CGRectMake(4, 65, 60, 15);
UILabel *title3 = [[UILabel alloc] initWithFrame:contentFrame7];
title3.tag = 0013;
title3 = [[UILabel alloc] initWithFrame:contentFrame7];
title3.backgroundColor = [UIColor clearColor];
title3.textColor = [UIColor whiteColor];
title3.font = [UIFont boldSystemFontOfSize:11];
title3.text = totalTime;
[title3 release];
} else {
CGRect contentFrame7 = CGRectMake(6, 65, 60, 15);
UILabel *title3 = [[UILabel alloc] initWithFrame:contentFrame7];
title3 = [[UILabel alloc] initWithFrame:contentFrame7];
title3.tag = 0013;
title3.backgroundColor = [UIColor clearColor];
title3.textColor = [UIColor whiteColor];
title3.font = [UIFont boldSystemFontOfSize:11];
title3.text = totalTime;
}
title3.numberOfLines = 1;
[cell.contentView addSubview:title3];
[title3 release];
} else {
//Update while scrolling
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]; convertedNumber = [formatter stringForObjectValue:firstNumber];
// views and titles
UILabel *title = (UILabel *)[cell.contentView viewWithTag:0011];
UILabel *title2 = (UILabel *)[cell.contentView viewWithTag:0012];
title.text = [currentFeed title];
title2.text = [NSString stringWithFormat:#"%# views", convertedNumber];
totalTime = [self timeFormatted:([currentFeed duration].intValue)-1];
NSString *word = #":00:";
NSString *word2 = #"00:00:";
if ([totalTime rangeOfString:word].location == NSNotFound || [totalTime rangeOfString:word2].location != NSNotFound) {
totalTime = [totalTime stringByReplacingOccurrencesOfString:#"00:00:" withString:#"0:"];
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:"];
}
UILabel *title3 = (UILabel *)[cell.contentView viewWithTag:0013];
if([totalTime length] >= 5) {
CGRect contentFrame7 = CGRectMake(4, 65, 60, 15);
title3 = [[UILabel alloc] initWithFrame:contentFrame7];
title3.textColor = [UIColor whiteColor];
title3.backgroundColor = [UIColor clearColor];
title3.text = totalTime;
} else {
CGRect contentFrame7 = CGRectMake(6, 65, 60, 15);
title3 = [[UILabel alloc] initWithFrame:contentFrame7];
title3.textColor = [UIColor whiteColor];
title3.backgroundColor = [UIColor clearColor];
title3.text = totalTime;
}
NSLog(#"%# CHANGED TO : %#", [self timeFormatted:([currentFeed duration].intValue)-1], totalTime);
}
return cell;
}
I'm not certain it is the root of this particular problem, but you are attempting to update the UI from a background thread, and that is not allowed.
After you load your thumbnail in the background you need to marshal the UI update calls back to the main thread. Do this with another dispatch to the main queue (dispatch_get_main_queue()).
Related
Name count i get after parsing json is random i.e. it can display any number of values ranging from 1 to 100. I created that many number of labels as shown in the code below, but only the last iterated value is displayed in the label when i pass NSString *name to putLabelsInScrollView method. Can any one help me out to fix this logic to display different name in different created labels? I cannot create tableview which would have been easy and will rectify the cGRects of label and textfields later.
int i = 0;
for(NSDictionary *myJsonDictionary in myJsonArray)
{
//UILabel *label = (UILabel *)[arrayLabel objectAtIndex:i++];
//[label setText:myJsonDictionary[#"Name"]];
NSUserDefaults *defaultNames = [NSUserDefaults standardUserDefaults];
NSString *name = myJsonDictionary[#"Name"];
[defaultNames setObject:name forKey:#"QUESTIONNAME"];
NSLog(#"Value is %# \n", name);
i++;
}
NSLog(#"Number of cycles in for-each = %d", i);
[self putLabelsInScrollView:i];
- (void) putLabelsInScrollView:(int)numberOfLables
{
for(int i = 0 ; i < numberOfLables ; i++)
{
UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(10, yPosition_label, 261, 30)];
[label setFont:[UIFont fontWithName:#"Helvetica Neue" size:12.0f]];
label.numberOfLines = 2;
NSUserDefaults *defaultNameFetch = [NSUserDefaults standardUserDefaults];
NSString *fetchedString = [defaultNameFetch objectForKey:#"QUESTIONNAME"];
[label setText:[NSString stringWithFormat:#"%#", fetchedString]];
//[label setText:myJsonDictionary[#"Name"]];
[self.scroll addSubview:label];
yPosition_label += 80;
UITextField *text = [[UITextField alloc] initWithFrame:CGRectMake(10, yPosition_text, 261, 30)];
text.borderStyle = UITextBorderStyleRoundedRect;
text.textColor = [UIColor blackColor];
text.font = [UIFont systemFontOfSize:12.0];
text.backgroundColor = [UIColor clearColor];
text.keyboardType = UIKeyboardTypeDefault;
text.delegate = self;
[self.scroll addSubview:text];
yPosition_text += 100;
yPosition_result = yPosition_label + yPosition_text;
}
[self.scroll setContentSize:CGSizeMake(self.scroll.frame.size.width, yPosition_result)];
[self.view addSubview:self.scroll];
}
Just Try this...
for(NSDictionary *myJsonDictionary in myJsonArray)
{
NSString *name = myJsonDictionary[#"Name"];
[self putLabelsInScrollView:name];
NSLog(#"Value is %# \n", name);
}
[self.scroll setContentSize:CGSizeMake(self.scroll.frame.size.width, yPosition_result)];
[self.view addSubview:self.scroll];
- (void) putLabelsInScrollView:(NSString *)labelText
{
UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(10, yPosition_label, 261, 30)];
[label setFont:[UIFont fontWithName:#"Helvetica Neue" size:12.0f]];
label.numberOfLines = 2;
[label setText:labelText];
//[label setText:myJsonDictionary[#"Name"]];
[self.scroll addSubview:label];
yPosition_label += 80;
UITextField *text = [[UITextField alloc] initWithFrame:CGRectMake(10, yPosition_text, 261, 30)];
text.borderStyle = UITextBorderStyleRoundedRect;
text.textColor = [UIColor blackColor];
text.font = [UIFont systemFontOfSize:12.0];
text.backgroundColor = [UIColor clearColor];
text.keyboardType = UIKeyboardTypeDefault;
text.delegate = self;
[self.scroll addSubview:text];
yPosition_text += 100;
yPosition_result = yPosition_label + yPosition_text;
}
Replace your code with this:
Fetching results:
int i = 0;
NSMutableArray *texts = [NSMutableArray array];
for(NSDictionary *myJsonDictionary in myJsonArray)
{
//UILabel *label = (UILabel *)[arrayLabel objectAtIndex:i++];
//[label setText:myJsonDictionary[#"Name"]];
NSUserDefaults *defaultNames = [NSUserDefaults standardUserDefaults];
NSString *name = myJsonDictionary[#"Name"];
[texts addObject:name];
NSLog(#"Value is %# \n", name);
i++;
}
NSLog(#"Number of cycles in for-each = %d", i);
[self putLabelsInScrollView:i withTexts:texts];
And method for putting labels' texts
- (void) putLabelsInScrollView:(int)numberOfLables withTexts:(NSArray *)texts
{
for(int i = 0 ; i < numberOfLables ; i++)
{
UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(10, yPosition_label, 261, 30)];
[label setFont:[UIFont fontWithName:#"Helvetica Neue" size:12.0f]];
label.numberOfLines = 2;
NSUserDefaults *defaultNameFetch = [NSUserDefaults standardUserDefaults];
NSString *fetchedString = texts[i];
[label setText:fetchedString];
//[label setText:myJsonDictionary[#"Name"]];
[self.scroll addSubview:label];
yPosition_label += 80;
UITextField *text = [[UITextField alloc] initWithFrame:CGRectMake(10, yPosition_text, 261, 30)];
text.borderStyle = UITextBorderStyleRoundedRect;
text.textColor = [UIColor blackColor];
text.font = [UIFont systemFontOfSize:12.0];
text.backgroundColor = [UIColor clearColor];
text.keyboardType = UIKeyboardTypeDefault;
text.delegate = self;
[self.scroll addSubview:text];
yPosition_text += 100;
yPosition_result = yPosition_label + yPosition_text;
}
[self.scroll setContentSize:CGSizeMake(self.scroll.frame.size.width, yPosition_result)];
[self.view addSubview:self.scroll];
}
I'm trying to add a timer on the side of the thumbnail but the UIlabel for the timer is always duplicating when I scroll.
It ends up like this. Any help would be greatly appreciated.
This is how my method looks like
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
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];
CGRect imageFrame = CGRectMake(0, 0, 120, 90);
self.customImage = [[UIImageView alloc] initWithFrame:imageFrame];
[cell.contentView addSubview:self.customImage];
[self.customImage release];
// Title
CGRect contentFrame = CGRectMake(122, 2, 198, 45);
UILabel *title = [[UILabel alloc] initWithFrame:contentFrame];
title.tag = 0011;
title.numberOfLines = 2;
title.backgroundColor = [UIColor clearColor];
title.textColor = [UIColor whiteColor];
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.backgroundColor = [UIColor clearColor];
title2.textColor = [UIColor whiteColor];
title2.font = [UIFont italicSystemFontOfSize:14];
[cell.contentView addSubview:title2];
[title2 release];
//Here's where the fails come in
CGRect contentFrame6 = CGRectMake(0, 66, 33, 13);
CGRect contentFrame7 = CGRectMake(6, 65, 60, 15);
UIImageView *imv5 = [[UIImageView alloc]initWithFrame:contentFrame6];
UILabel *title3 = [[UILabel alloc] initWithFrame:contentFrame7];
imv5.alpha = 1;
title3.tag = 0013;
imv5.tag = 0014;
[cell.contentView addSubview:imv5];
imv5.backgroundColor = [UIColor clearColor];
}
// 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]; convertedNumber = [formatter stringForObjectValue:firstNumber];
// Basic stuff
UILabel *title = (UILabel *)[cell.contentView viewWithTag:0011];
UILabel *title2 = (UILabel *)[cell.contentView viewWithTag:0012];
UILabel *title3 = (UILabel *)[cell.contentView viewWithTag:0013];
title.text = [currentFeed title];
title2.text = [NSString stringWithFormat:#"%# views", convertedNumber];
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:"];}
if([totalTime length] >= 5) {
CGRect contentFrame7 = CGRectMake(4, 65, 60, 15);
title3 = [[UILabel alloc] initWithFrame:contentFrame7];
title3.textColor = [UIColor whiteColor];
title3.backgroundColor = [UIColor clearColor];
title3.font = [UIFont boldSystemFontOfSize:10];
} else {
CGRect contentFrame7 = CGRectMake(6, 65, 60, 15);
title3 = [[UILabel alloc] initWithFrame:contentFrame7];
title3.textColor = [UIColor whiteColor];
title3.backgroundColor = [UIColor clearColor];
title3.font = [UIFont boldSystemFontOfSize:10];
}
title3.text = totalTime;
[cell.contentView addSubview:title3];
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){
cell.imageView.image = myview;
//Add Black fucking border and stuff here!!!
}else{
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
NSData* data = [NSData dataWithContentsOfURL:[currentFeed thumbnailURL]];
UIImage *thumbnail = [UIImage imageWithData:data];
cell.imageView.image = thumbnail;
[self.feedsTableView beginUpdates];
[self.feedsTableView reloadRowsAtIndexPaths:[NSArray arrayWithObjects:indexPath, nil]
withRowAnimation:UITableViewRowAnimationNone];
[self.feedsTableView endUpdates];
});
}
return cell;
}
Obvious:
Don't add any views outside the (cell == nil) block.
Cells are reused, that means that if you add any label to a reused cell, there will be two labels... and more if the cell is reused again.
There are many other problematic parts. For example, starting cell reloading from inside this method is VERY strange.
Also, the code would be simplified a lot, if you used a xib for the cell design.
So I'm loading a YouTube channel inside a tableView and trying to add the correct duration but sometimes it doesn't show up at all and when it does show up there's another text behind it if you look closely.
When you look at image 1 you can see that there's no time on one video until I scroll over it.
When you look at image 2 you can see that the text label is duplicated at some places
Code
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
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];
totalTime = [self timeFormatted:([currentFeed duration].intValue)-1];
if([totalTime length] <= 4) {
CGRect contentFrame6 = CGRectMake(0, 66, 30, 13);
CGRect contentFrame7 = CGRectMake(6, 65, 60, 15);
UILabel *title3 = [[UILabel alloc] initWithFrame:contentFrame7];
UIImageView *imv5 = [[UIImageView alloc]initWithFrame:contentFrame6];
imv5.image=[UIImage imageNamed:#"blackBorder.png"];
imv5.alpha = 0.8;
[cell.contentView addSubview:imv5];
title3.tag = 0013;
title3.numberOfLines = 1;
title3.font = [UIFont boldSystemFontOfSize:10];
[cell.contentView addSubview:title3];
title3.text = totalTime;
title3.textColor = [UIColor whiteColor];
title3.backgroundColor = [UIColor clearColor];
imv5.backgroundColor = [UIColor clearColor];
[imv5 release];
[title3 release];
} else if([totalTime length] == 5) {
CGRect contentFrame6 = CGRectMake(87, 64, 30, 13);
CGRect contentFrame7 = CGRectMake(90, 63, 60, 15);
UILabel *title3 = [[UILabel alloc] initWithFrame:contentFrame7];
UIImageView *imv5 = [[UIImageView alloc]initWithFrame:contentFrame6];
imv5.image=[UIImage imageNamed:#"blackBorder.png"];
imv5.alpha = 0.8;
[cell.contentView addSubview:imv5];
title3.tag = 0013;
title3.numberOfLines = 1;
title3.font = [UIFont systemFontOfSize:10];
[cell.contentView addSubview:title3];
title3.text = totalTime;
title3.textColor = [UIColor whiteColor];
title3.backgroundColor = [UIColor clearColor];
imv5.backgroundColor = [UIColor clearColor];
[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"];
}
}
return cell;
}
May be change it like-
if (cell == nil)
{
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier] autorelease];
}
Then, the block -
totalTime = [self timeFormatted:([currentFeed duration].intValue)-1];
if([totalTime length] <= 4) {
CGRect contentFrame6 = CGRectMake(0, 66, 30, 13);
CGRect contentFrame7 = CGRectMake(6, 65, 60, 15);
UILabel *title3 = [[UILabel alloc] initWithFrame:contentFrame7];
UIImageView *imv5 = [[UIImageView alloc]initWithFrame:contentFrame6];
imv5.image=[UIImage imageNamed:#"blackBorder.png"];
imv5.alpha = 0.8;
[cell.contentView addSubview:imv5];
title3.tag = 0013;
title3.numberOfLines = 1;
title3.font = [UIFont boldSystemFontOfSize:10];
[cell.contentView addSubview:title3];
title3.text = totalTime;
title3.textColor = [UIColor whiteColor];
title3.backgroundColor = [UIColor clearColor];
imv5.backgroundColor = [UIColor clearColor];
[imv5 release];
[title3 release];
} else if([totalTime length] == 5) {
CGRect contentFrame6 = CGRectMake(87, 64, 30, 13);
CGRect contentFrame7 = CGRectMake(90, 63, 60, 15);
UILabel *title3 = [[UILabel alloc] initWithFrame:contentFrame7];
UIImageView *imv5 = [[UIImageView alloc]initWithFrame:contentFrame6];
imv5.image=[UIImage imageNamed:#"blackBorder.png"];
imv5.alpha = 0.8;
[cell.contentView addSubview:imv5];
title3.tag = 0013;
title3.numberOfLines = 1;
title3.font = [UIFont systemFontOfSize:10];
[cell.contentView addSubview:title3];
title3.text = totalTime;
title3.textColor = [UIColor whiteColor];
title3.backgroundColor = [UIColor clearColor];
imv5.backgroundColor = [UIColor clearColor];
[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"];
}
}
And then, - return cell;
The settings i.e. the second block shall be done every time a Cell is dequed, not only when a new is allocated.
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.
The image seems to overlay the text in the cell, I'm not sure why this is happening but I'd like the text to go over the image. Any help is greatly appreciated.
This is how my code looks like.
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil)
{
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier] autorelease];
CGRect imageFrame = CGRectMake(0, 0, 120, 90);
self.customImage = [[UIImageView alloc] initWithFrame:imageFrame];
[cell.contentView addSubview:self.customImage];
[self.customImage release];
CGRect contentFrame = CGRectMake(100, 2, 198, 30);
UILabel *title = [[UILabel alloc] initWithFrame:contentFrame];
title.tag = 0011;
title.numberOfLines = 2;
title.font = [UIFont boldSystemFontOfSize:12];
[cell.contentView addSubview:title];
[title release];
}
UILabel *title = (UILabel *)[cell.contentView viewWithTag:0011];
title.text = [currentFeed title];
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){
cell.imageView.image = myview;
}else{
NSData* imageDataTemp = [[NSData alloc] initWithContentsOfURL:[currentFeed thumbnailURL]];
if(imageDataTemp){
cell.imageView.image = [UIImage imageWithData:imageDataTemp];
}else{
cell.imageView.image = [UIImage imageNamed:#"youtubeLogo.png"];
}
}
return cell;
}
cell.imageView.image is not your self.customImage you added.Now it displayes the tableview cel default imageview in cell
self.customImage = [[UIImageView alloc] initWithFrame:imageFrame];
[cell.contentView addSubview:self.customImage];
[self.customImage setTag:12345];
[self.customImage release];
and get the added custom imageview as
UIImageView *imageVW=[cell viewWithTag:12345];
and use this imageview to set image later in the code instead of cell.imageview