cellForRowAtIndexPath:
CGRect nameValueRect = CGRectMake(80, 5, 200, 15);
UILabel *nameValue = [[UILabel alloc] initWithFrame:
nameValueRect];
nameValue.tag = kNameValueTag;
[cell.contentView addSubview:nameValue];
[nameValue release];
...
NSUInteger row = [indexPath row];
NSDictionary *rowData = [self.computers objectAtIndex:row];
UILabel *name = (UILabel *)[cell.contentView viewWithTag:
kNameValueTag];
name.text = [rowData objectForKey:#"Name"];
This code is from a tutorial that I'm using which creates a subview within the cell to add some text.
However, what I want to do is instead of adding text I will place an image.
I tried UIImageView *posterValue = [[UIImageView alloc] initWith... oh there's no initWithFrame for UIImageView.
Perhaps, someone can explain the process for an image. Do I even need to set the frame size for a UIImageView? I would need to position it.
EDIT:
my new code:
UIImageView *posterValue = [[UIImageView alloc] initWithFrame:CGRectMake(0,0,105,175)];
posterValue.tag = kPosterValueTag;
[cell.contentView addSubView:posterValue];
[posterValue release];
addSubView is not working for UIImageView. Says UIView may not respond to addSubView
It's addSubview not addSubView - case matters.
Related
So my rootViewController contains a UICollectionView with a Custom UICollectionViewCell. I have a label on the cell for a header which shouldn't ever change and I have a multi-line Label making up the body of the rest of the cell. When I launch the app it calls the sizeToFit correctly and top aligns everything so it isn't centered. I click on one of the cells and go to the next view, then if I click the back button to go back to the rootViewController it does run the viewWillAppear method and reloads the data but the sizeToFit does not work and everything on the multi-line label becomes center aligned. It is still multiple lines but if there is only a couple of lines it sits in the center of the label and it doesn't look good. How can I keep this so it is consistant. I have a left menu that will reload the rootViewController which will position the label correctly again but once I hit the back button from the secondViewController it is no longer aligned. Is there a way to make it clear all of the cells and reload them. I have tried the [collectionView reloadData]; in the viewWillAppear and it doesn't work, it is currently called at the end of the connectionDidFinish which the network connection is called from the viewWillAppear method. Any assistance is appreciated.
This is the custom UICollectionViewCell code
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
}
return self;
}
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect
{
// Drawing code
[contentLabel setNumberOfLines:0];
[contentLabel sizeToFit];
}
RootViewController
-(UICollectionViewCell *)collectionView:(UICollectionView *)cv cellForItemAtIndexPath:
(NSIndexPath *)indexPath
{
NSString *cellIdentifier = #"LocationListCell";
LocationList *cell = [cv dequeueReusableCellWithReuseIdentifier:cellIdentifier
forIndexPath:indexPath];
[cell.titleBar setText:[value objectAtIndex:indexPath.row]];
NSArray *tempObject = [[NSArray alloc] initWithArray:[self getData:[key
objectAtIndex:indexPath.row]]];
NSMutableString *tempString = [[NSMutableString alloc] init];
for (int i = 0; i < [tempObject count]; i++)
{
[tempString appendString:[tempObject objectAtIndex:i]];
[tempString appendString:#"\r"];
}
//[cell.contentLabel sizeToFit];
//[cell.contentLabel setNumberOfLines:0];
[cell.contentLabel setText:tempString];
return cell;
}
Use this dynamic UILable with this custom method...
just add this bellow method in your .m file
-(float) calculateHeightOfTextFromWidth:(NSString*) text: (UIFont*)withFont: (float)width :(UILineBreakMode)lineBreakMode
{
[text retain];
[withFont retain];
CGSize suggestedSize = [text sizeWithFont:withFont constrainedToSize:CGSizeMake(width, FLT_MAX) lineBreakMode:lineBreakMode];
[text release];
[withFont release];
return suggestedSize.height;
}
and use it like bellow..
UILabel *yourLable = [[UILabel alloc]init];
[yourLable setFrame:CGRectMake(110, 31, 200, 50)];
[yourLable setText:tempString];
yourLable.lineBreakMode = UILineBreakModeWordWrap;
yourLable.numberOfLines = 0;
yourLable.font = [UIFont fontWithName:#"Helvetica" size:12];
yourLable.frame = CGRectMake(yourLable.frame.origin.x, yourLable.frame.origin.y,
200,[self calculateHeightOfTextFromWidth:yourLable.text :yourLable.font :200 :UILineBreakModeWordWrap] );
yourLable.textColor = [UIColor darkGrayColor];
cell.contentLabel = yourLable;
hope this help you...
Try these lines:
contentLabel.textAlignment = UITextAlignmentLeft;
contentLabel.lineBreakMode = UILineBreakModeWordWrap;
Try like that and declare label in .h file
contentLabel = [[UILabel alloc]initWithFrame:CGRectMake(10, 49, 202, 237)];
[contentLabel setNumberOfLines:0];
[contentLabel sizeToFit];
[cell.contentLabel setText:tempString];
I have UITableViewController with UIScrollView inside UIView as its header.
UIScrollView is used to display slideshow of images:
UITableView
-UITableViewHeaderView
-UIView
-UIScrollView
-UIImageView
...
-UIPageControl
Every image has a subview with its title and description:
UIImageView
-UIView
-UILabel
-UILabel
When I need to update my tableView a delegate method is called which reloads data in a tableView and calls addImages method:
- (void)eventsUpdated
{
if ([self respondsToSelector:#selector(setRefreshControl:)])
[self.refreshControl endRefreshing];
[self.tableView reloadData];
[self addImages];
}
Here is how I add my images:
- (void)addImages
[self.scrollView.subviews makeObjectsPerformSelector:#selector(removeFromSuperview)];
for (int i = 0; i < images.count; i++) {
CGRect frame;
frame.origin.x = self.scrollView.frame.size.width * i;
frame.origin.y = 0;
frame.size = self.scrollView.frame.size;
UIImageView *subview = [self createImageViewForEvent:[images objectAtIndex:i] inFrame:frame];
subview.frame = frame;
[self.scrollView addSubview:subview];
}
self.scrollView.contentSize = CGSizeMake(scrollView.frame.size.width*images.count, scrollView.frame.size.height);
pageControll.currentPage = 0;
pageControll.numberOfPages = images.count;
}
- (UIImageView *)createImageViewForEvent:(Event *)event inFrame:(CGRect)frame
{
UIImage *image;
NSString *imageName = [event.imageName lastPathComponent];
NSString *bundleFilePath = [[NSBundle mainBundle] pathForResource:[imageName stringByDeletingPathExtension] ofType:[imageName pathExtension]];
image = [UIImage imageWithContentsOfFile:bundleFilePath];
UIImageView *output = [[UIImageView alloc] initWithImage:image];
output.frame = frame;
UIView *descriptionView = [[UIView alloc] initWithFrame:CGRectMake(0, frame.size.height*0.7, frame.size.width, frame.size.height*0.3)];
descriptionView.backgroundColor = [UIColor colorWithRed:0 green:0 blue:0 alpha:0.5];
descriptionView.alpha = 1.0;
UILabel *header = [[UILabel alloc] initWithFrame:CGRectMake(10, 5, frame.size.width-20, 12)];
header.textColor = [UIColor colorWithRed:210/256.0 green:217/256.0 blue:231/256.0 alpha:1.0];
header.font = [UIFont fontWithName:#"Helvetica" size:17];
header.backgroundColor = [UIColor clearColor];
header.lineBreakMode = UILineBreakModeTailTruncation;
header.numberOfLines = 1;
UILabel *description = [[UILabel alloc] initWithFrame:CGRectMake(10, 22, frame.size.width-20, 28)];
description.textColor = [UIColor colorWithRed:255/256.0 green:255/256.0 blue:255/256.0 alpha:1.0];
description.font = [UIFont fontWithName:#"Helvetica" size:12];
description.backgroundColor = [UIColor clearColor];
description.lineBreakMode = UILineBreakModeTailTruncation;
description.numberOfLines = 2;
header.text = event.title;
[descriptionView addSubview:header];
description.text = event.shortDesription;
[descriptionView addSubview:description];
[output addSubview:descriptionView];
return output;
}
After first launch everything works fine, but if I try to reload my tableView and call addImages again, all UILabels disappear, only UIImageView and UIView are visible.
UPD:
What I have noticed, is that my UILabels appear after some time, approximately after 30 seconds. I have never experienced something similar before
UPD 2:
After I reload my tableView and scrollview, scrollView's content is not updated right away, only after I start scrolling
I reproduced a sample project from the code you posted, and didn't find any refreshing issues on the labels nor on any other component. This leads me to think the problem is not in the code you posted, but rather somewhere else.
Usually when you see such a delay, it is because some code that should not be running in parallel, is running in parallel.
I would suggest you check how and when you perform your call to eventsUpdated. You must make sure that call is performed on the main thread, and only once your array of events has finished updating. To check that, you can add add the following line in eventsUpdated:
NSLog(#"Current: %#, main: %#", [NSThread currentThread], [NSThread mainThread]);
Please post the log! :-)
I would suggest you also add to your question the code from the method that calls eventsUpdated, as it could also be of help.
At first, never do something like
[scrollView.subviews makeObjectsPerformSelector:#selector(removeFromSuperview)];
Scroll view has some internal views (scroll indicators, for example), so you're removing them which may dealloc them and cause strange crashes any time later.
As for your problem - try calling bringSubviewToFront: of your UIImageView to get your labels infront of everything else
I need to have a footer in my view that says "Powered by: {image_logo_of_my_company}" How do I achieve that?
Make a wrapper view of type UIView and add a UILabel and the logo in a UIImageView inside that view at the appropriate coordinates.
UIView *wrapper = [[UIView alloc] initWithFrame:footerFrame];
UILabel *label = [[UILabel alloc] initWithFrame:
CGRectMake(0, 0, footerFrame.size.width*0.6, footerFrame.size.height)];
UIImageView *imageView = [[UIImageView alloc] initWithFrame:
CGRectMake(footerFrame.size.width*0.6, 0,
footerFrame.size.width*(1.0-0.6), footerFrame.size.height)];
[wrapper addSubView:label];
[wrapper addSubView:imageView];
label.text = #"Powered by: ";
imageView.image = [UIImage imageNamed:#"logo"]; // assumes logo.png & logo#2x.png
[viewNeedingFooter addSubView:wrapper];
Assuming you need 60% of the width of the footer for the label and 40% for the logo.
I have the following code which draws a separator line and text for a UITableViewCell. It looks fine but when I scroll off screen then back, the separator line is gone but the text is still fine. Any ideas?
static NSString *aProgressIdentifier = #"CustomerCell";
UITableViewCell *aCustomerCell = [iTableView dequeueReusableCellWithIdentifier:aProgressIdentifier];
if (!aCustomerCell) {
aCustomerCell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:aProgressIdentifier] autorelease];
aCustomerCell.contentView.backgroundColor = [UIColor whiteColor];
UIImageView *aLine = [[UIImageView alloc] initWithFrame:CGRectMake(0, 72, 800, 1)];
aLine.backgroundColor = [UIColor colorWithWhite:0.9 alpha:1.0];
[aCustomerCell addSubview:aLine];
[aLine release];
}
CMACustomer *aCustomerObject = aCellObject;
aCustomerCell.textLabel.text = aCustomerObject.customerFullName;
aCustomerCell.detailTextLabel.text = nil;
aCell = aCustomerCell;
Try to add the "aLine" image view as subview of the contentView and not the whole table itself. Probably when the cell is reused and then layoutSubviews is called again, the contentView overlaps (white background) your aLine. Infact consider that iOS default cells have their subviews dynamically redrawn and resized each time they are displayed on screen.
So I would try this:
[aCustomerCell.contentView addSubview:aLine];
If this doesn't work, what can you do is to remove the contentView completely and add your own custom subviews (do this inside the if(!aCustomerCell) and not outside unless you will not get the benefits of the cell re-use):
if (!aCustomerCell) {
aCustomerCell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:aProgressIdentifier] autorelease];
[cell.subviews makeObjectsPerformSelector:#selector(removeFromSuperview)];
UIImageView *aLine = [[UIImageView alloc] initWithFrame:CGRectMake(0, 72, 800, 1)];
aLine.backgroundColor = [UIColor colorWithWhite:0.9 alpha:1.0];
[aCustomerCell.contentView addSubview:aLine];
[aLine release];
}
Finally another check is verify that the cell height is > 72 (it seems a trivial check but its often source of headaches!).
The table view is using a pool of cells, so you can't be sure which one you're getting for any given index path. You can use the cell or the content view, but be sure to add only one of your custom lines per cell.
UIImageView *aLine = (UIImageView *)[cell viewWithTag:64];
if (!aLine) {
// etc.
UIImageView *aLine = [[UIImageView alloc] initWithFrame:CGRectMake(0, 72, 800, 1)];
aLine.tag = 64;
[cell addSubview:aLine];
//
}
// other formatting logic here, you can also hide/show aLine based on biz logic
try adding it to the contentView
[aCustomerCell.contentView addSubview:aLine]
I'm new to programming, and I know how to add a static image behind a tableView in a .xib file, but not in code. I have tried the following code:
UIImage * image = [UIImage imageNamed:#"pic.png"];
[self.view addSubView: image];
but nothing happens when I write this!
To display an UIImage you have to use an UIImageView and put it under the UITableView using the same frame. Don't forget to set the table view's background to transparent.
UIImageView *iv = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"pic.png"]];
iv.frame = CGRectMake(0.0, 0.0, 300.0, 600.0);
UITableView *tv = [[UITableView alloc] initWithFrame:iv.frame style:UITableViewStylePlain];
tv.opaque = NO;
tv.backgroundColor = [UIColor clearColor];
[self.view addSubView: iv];
[self.view addSubView: tv];
[iv release];
[tv release];
You need to use a UIImageView, not a UIImage. Also, when adding a view programmatically, don't forget to set the frame.