I currently have a -UIScrollView and I have a function that adds more information inside the scrollView, however inside the function it recalculates the size of the scrollView and I try to make it bigger but it stays the same.
Basically inside -viewDidLoad
it sets the "Default" size of the -UIScrollView
[self.xboxScrollView setContentSize:CGSizeMake(303, 2890)];
After some time the device adds a few other values to the -UIScrollView
- (void)addAnotherCheat:(NSString *)title :(NSString *)description index:(NSInteger)cheatID {
int titleLocation = 2744 + (cheatID * 101);
int titleDescription = 2766 + (cheatID * 101);
float lengthofscrollViews = 2866 + (cheatID * 101);
cheat1 = [[UILabel alloc] initWithFrame:CGRectMake(13, titleLocation, 287, 35)];
description1 = [[UILabel alloc] initWithFrame:CGRectMake(13, titleDescription, 287, 71)];
image1 = [[UIImageView alloc] initWithFrame:CGRectMake(0, titleLocation, 306, 91)];
UIImage *image = [UIImage imageNamed: #"backggroundcolor.png"];
[self.xboxScrollView setContentSize:CGSizeMake(303, lengthofscrollViews)];
[self.xboxScrollView setScrollEnabled:YES];
NSLog(#"Scrollview Size : %f", self.xboxScrollView.contentSize.height);
[self.xboxScrollView addSubview:image1];
[self.xboxScrollView addSubview:cheat1];
[self.xboxScrollView addSubview:description1];
image1.image = image;
cheat1.backgroundColor=[UIColor clearColor];
cheat1.textColor = [UIColor whiteColor];
description1.backgroundColor=[UIColor clearColor];
description1.textColor = [UIColor whiteColor];
description1.numberOfLines = 3;
[cheat1 setFont:[UIFont fontWithName:#"STHeitiTC-Medium" size:19.0f]];
[description1 setFont:[UIFont fontWithName:#"STHeitiTC-Light" size:16.0f]];
cheat1.text = title;
description1.text = description;
}
When I Log the size that it's supposed to be it prints out the correct height I want, however it doesn't change the height.
Basically I need a method to make the scrollView larger when I've already set the ContentSize.
Related
- (void)viewDidLoad
{
MyData *data = [MyData SharedData];
scrollerView.pagingEnabled = YES;
scrollerView.bounces = YES;
int numberOfViews = [data.placePhotos count];
for (int i = 0; i < numberOfViews; i++) {
CGFloat yOrigin = i * self.view.frame.size.width;
UIView *awesomeView = [[UIView alloc] initWithFrame:CGRectMake(yOrigin, 0, self.view.frame.size.width, self.view.frame.size.height)];
awesomeView.backgroundColor = [UIColor colorWithRed:0 green:0 blue:0 alpha:1];
UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(10, 0, 300 , 320)];
NSString * photourl = [[data.placePhotos objectAtIndex:i] stringByReplacingOccurrencesOfString:#"150x75" withString:#"440x440"];
[imageView setImageWithURL:[NSURL URLWithString:photourl]];
[awesomeView addSubview:imageView];
[scrollerView addSubview:awesomeView];
awesomeView = nil;
imageView =nil;
}
I added the UIScrollView in the .xib file , when i tried to pull the scroll view from left to right it does not move, i have 4 subviews added but why i can not view them ?
you should set the content size the scrollView
[self. scrollerView setContentSize:CGSizeMake(320, 1000)];
Give your width and heigth.
add scrollerView.contentSize = CGSizeMake(yOrigin + self.view.frame.size.width, scrollerView.frame.size.height); after the loop (even though I'm a little bit confused about why the x-origin is called yOrigin in your code...)
I am using https://github.com/alekseyn/EasyTableView to create a scrollable table view with images. It works very well as given int the demo.
But my custom requirement is to have 2 rows instead of 1 row. So i decided to add 2 image views instead of 1 for a single cell in the table.
- (UIView *)easyTableView:(EasyTableView *)easyTableView viewForRect:(CGRect)rect {
// Create a container view for an EasyTableView cell
UIView *container = [[UIView alloc] initWithFrame:rect];;
// Setup an image view to display an image
UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(1, 0, rect.size.width-2, rect.size.height/2)];
imageView.tag = IMAGE_TAG;
imageView.contentMode = UIViewContentModeScaleAspectFill;
[container addSubview:imageView];
// Setup a label to display the image title
CGRect labelRect = CGRectMake(10, rect.size.height/2-20, rect.size.width-20, 20);
UILabel *label = [[UILabel alloc] initWithFrame:labelRect];
label.textAlignment = UITextAlignmentCenter;
label.textColor = [UIColor colorWithWhite:1.0 alpha:0.5];
label.backgroundColor = [UIColor clearColor];
label.font = [UIFont boldSystemFontOfSize:14];
label.tag = LABEL_TAG;
[container addSubview:label];
// Setup an image view to display an image
UIImageView *imageView2 = [[UIImageView alloc] initWithFrame:CGRectMake(1, rect.size.height/2, rect.size.width-2, rect.size.height)];
imageView.tag = IMAGE_TAG2;
imageView.contentMode = UIViewContentModeScaleAspectFill;
[container addSubview:imageView2];
// Setup a label to display the image title
CGRect labelRect2 = CGRectMake(10, rect.size.height-20, rect.size.width-20, 20);
UILabel *label2 = [[UILabel alloc] initWithFrame:labelRect2];
label.textAlignment = UITextAlignmentCenter;
label.textColor = [UIColor colorWithWhite:1.0 alpha:0.5];
label.backgroundColor = [UIColor clearColor];
label.font = [UIFont boldSystemFontOfSize:14];
label.tag = LABEL_TAG2;
[container addSubview:label2];
return container;
}
// Second delegate populates the views with data from a data source
- (void)easyTableView:(EasyTableView *)easyTableView setDataForView:(UIView *)view forIndexPath:(NSIndexPath *)indexPath {
// Set the image title for the given index
UILabel *label = (UILabel *)[view viewWithTag:LABEL_TAG];
label.text = [self.imageStore.titles objectAtIndex:indexPath.row];
// Set the image for the given index
UIImageView *imageView = (UIImageView *)[view viewWithTag:IMAGE_TAG];
imageView.image = [self.imageStore imageAtIndex:indexPath.row];
// Set the image title for the given index
UILabel *label2 = (UILabel *)[view viewWithTag:LABEL_TAG2];
label2.text = [self.imageStore.titles objectAtIndex:indexPath.row];
// Set the image for the given index
UIImageView *imageView2 = (UIImageView *)[view viewWithTag:IMAGE_TAG2];
imageView2.image = [self.imageStore imageAtIndex:indexPath.row];
}
Initially for testing i used same image and label for both cells. But it is suspecious that it is showing images and rows on first row means for first image of any cell.
How can i accomplish my requirement.
I have a UITableViewCell that contains a UIScrollView. The UIScrollView contains a different number of UIImageViews for each row. Some rows have 5 images in the scroll view, some rows have 15 images in the scroll view. Since the number of UIImageViews in each row is different, I'm stuck allocating new UIImageViews when each row is drawn! This is obviously having a negative impact on scroll performance of my UITableView. How can I resolve this to improve UITableView scrolling?
Additionally, using Instruments' Time Profiler, I've noticed that the method call [tableView dequeueReusableCellWithIdentifier:MyIdentifier] is abnormally slow. Does that help point to the problem?
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *MyIdentifier = #"HuddleTableCell";
HuddleListItemTableViewCell *cellToReturn = [tableView dequeueReusableCellWithIdentifier:MyIdentifier];
if (cellToReturn == nil) {
[[NSBundle mainBundle] loadNibNamed:#"HuddleListItemTableViewCell" owner:self options:nil];
cellToReturn = cell;
UILabel *badgeLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 50, 50)];
badgeLabel.font = [UIFont boldSystemFontOfSize:16];
badgeLabel.numberOfLines = 2;
badgeLabel.textAlignment = UITextAlignmentCenter;
badgeLabel.textColor = [UIColor whiteColor];
badgeLabel.backgroundColor = [UIColor clearColor];
[cellToReturn.attendee4Image addSubview:badgeLabel];
cellToReturn.othersOverlayLabel = badgeLabel;
[badgeLabel release];
cellToReturn.heartHolderView.layer.cornerRadius = 5;
cellToReturn.heartHolderView.colors = [NSArray arrayWithObjects:(id)[[UIColor colorWithRed:.29 green:.39 blue:.57 alpha:1.0] CGColor],(id)[[UIColor colorWithRed:.19 green:.22 blue:.36 alpha:1.0] CGColor], (id)[[UIColor colorWithRed:.102 green:.122 blue:.17 alpha:1.0] CGColor], nil];
cellToReturn.heartHolderView.colors = [NSArray arrayWithObjects:(id)[UIColorFromRGB(0x4A59A8) CGColor], nil];
cellToReturn.heartHolderView.layer.cornerRadius = 5.0;
UIView *polaroidView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 37, 39)];
polaroidView.layer.shadowColor = [UIColor blackColor].CGColor;
polaroidView.layer.shadowOpacity = 0.3;
polaroidView.layer.shadowRadius = 1;
polaroidView.layer.shadowOffset = CGSizeMake(5.0f, 5.0f);
polaroidView.backgroundColor = [UIColor whiteColor];
UIImageView *img = [[UIImageView alloc] initWithFrame:CGRectMake(4, 4, 29, 29)];
[polaroidView addSubview:img];
[cellToReturn.organizerImage addSubview:polaroidView];
cellToReturn.polaroidView = polaroidView;
cellToReturn.polaroidImageView = img;
[img release];
[polaroidView release];
cellToReturn.gradientView.colors = [NSArray arrayWithObjects:(id)[[UIColor whiteColor] CGColor],(id)[[UIColor colorWithRed:.96 green:.96 blue:.96 alpha:1.0] CGColor],(id)[[UIColor colorWithRed:.85 green:.85 blue:.85 alpha:1.0] CGColor], nil];
cellToReturn.votingTimerLabel.textColor = UIColorFromRGB(0x616163);
cellToReturn.eventDateLabel.textColor = UIColorFromRGB(0x616163);
cellToReturn.eventNameLabel.textColor = UIColorFromRGB(0x616163);
cellToReturn.numInviteesLabel.textColor = UIColorFromRGB(0x616163);
cellToReturn.numVotesLabel.textColor = UIColorFromRGB(0x616163);
self.cell = nil;
}
NSString *organizerFbId = [[self.huddleList objectAtIndex:indexPath.row]objectForKey:#"userId"];
[cellToReturn.polaroidImageView setImageWithURL:[self.organizerImageUrls objectForKey:organizerFbId] placeholderImage:self.placeholderImage];
[[cellToReturn.imageScrollViewHolder subviews] makeObjectsPerformSelector:#selector(removeFromSuperview)];
if ([self.avatarScrollViews objectForKey:[[self.huddleList objectAtIndex:indexPath.row]objectForKey:#"id"]]) {
[cellToReturn.imageScrollViewHolder addSubview:[self.avatarScrollViews objectForKey:[[self.huddleList objectAtIndex:indexPath.row]objectForKey:#"id"]]];
} else {
UIScrollView *tempImageScrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, 200, 46)];
tempImageScrollView.autoresizingMask = UIViewAutoresizingFlexibleWidth;
NSArray *fbUids = [[self.huddleList objectAtIndex:indexPath.row] objectForKey:#"facebookInvitees"];
int i=0;
for (i=0;i < [fbUids count];i++) {
UIView *polaroidView = [[[UIView alloc] initWithFrame:CGRectMake((i * 45) + (3 * i), 0, 37, 39)] autorelease];
polaroidView.layer.shadowColor = [UIColor blackColor].CGColor;
polaroidView.layer.shadowOpacity = 0.3;
polaroidView.layer.shadowRadius = 1;
polaroidView.layer.shadowOffset = CGSizeMake(5.0f, 5.0f);
polaroidView.backgroundColor = [UIColor whiteColor];
UIImageView *img = [[[UIImageView alloc] initWithFrame:CGRectMake(4, 4, 29, 29)] autorelease];
[img setImageWithURL:[NSURL URLWithString:[NSString stringWithFormat:#"http://graph.facebook.com/%#/picture", [fbUids objectAtIndex:i]]] placeholderImage:self.placeholderImage];
[polaroidView addSubview:img];
[tempImageScrollView addSubview:polaroidView];
}
[tempImageScrollView setContentSize:CGSizeMake((i*45) + (3 * i), 46)];
[self.avatarScrollViews setObject:tempImageScrollView forKey:[[self.huddleList objectAtIndex:indexPath.row]objectForKey:#"id"]];
[cellToReturn.imageScrollViewHolder addSubview:tempImageScrollView];
[tempImageScrollView release];
}
return cellToReturn;
}
Even there was a fixed number of images in each cell's scroller, you'd be stuck allocating them as the cells are dequeued and reused. The way to get speed is to take up some more space, by keeping the cells' subviews in an array outside of the table.
Create another array that has the same count as your huddleList array. Fill that array with UIScrollViews containing the imageViews. All that code in your cellForRowAtIndexPath method can move to the initialization method for the new array and get replaced by a few simple lines:
// dequeue or build a cell
// tag the scroll views so you can find them
UIScrollView *oldScrollView = (UIScrollView *)[cell viewWithTag:kCELL_SCROLL_VIEW];
// this might be a reused cell, so remove the scroll view if it has one
if (oldScrollView) [oldScrollView removeFromSuperview];
// now add the one that you setup for this cell
// your init code that used to be in this method sets up the frame, the tag, etc
UIScrollView *currentScrollView = [self.cachedScrollViews objectAtIndex:indexPath.row];
[cell addSubview:currentScrollView];
// that's it
Don't forget to release the cachedScrollViews on memory warning
I want to dynamically assign width to a label depending upon the text length to be displayed. The labels are it self being added on uiview. I am using following code but still i am getting label with shorter width.
- (id)initWithFrame:(CGRect)frame OrangeText:(NSString*)orange WhiteText:(NSString*)white {
if ((self = [super initWithFrame:frame])) {
CGSize textSize = [orange sizeWithFont:[UIFont systemFontOfSize:14]];
OrangeLabel = [[UILabel alloc] initWithFrame:CGRectMake(30, 0, textSize.width, textSize.height+2)];
OrangeLabel.text = orange;
OrangeLabel.backgroundColor = [UIColor clearColor];
OrangeLabel.textColor = [UIColor orangeColor];
[self addSubview:OrangeLabel];
WhiteLabel = [[UILabel alloc] init];
CGSize whiteTextSize = [white sizeWithFont:[UIFont systemFontOfSize:14]];
WhiteLabel.frame = CGRectMake(OrangeLabel.frame.size.width+35, 5, whiteTextSize.width, whiteTextSize.height);
WhiteLabel.text = white;
WhiteLabel.backgroundColor = [UIColor clearColor];
WhiteLabel.textColor = [UIColor whiteColor];
[self addSubview:WhiteLabel]; // Initialization code
}
return self;
}
I think you are looking for this method
[myLabel sizeToFit];
This should resize the label frame to fit its contents.
I'm working on a UITableView whose cells contain an UIImageView subclass which gets data from a URL and cache images to the iphone disk.
Problem is, event with cached images the scrolling tends to be stuttering. So I searched a bit and found ABTableViewCell ( github.com/enormego/ABTableViewCell ) which is supposed to dramatically improve scrolling smoothness.
But, even with the example provided ( blog.atebits.com/2008/12/fast-scrolling-in-tweetie-with-uitableview ) I don't really get what I am supposed to do.
I tried to do this: I created a class which inherits ABTableViewCell, added some UILabels and the UIImageView as class properties, and implemented methods this way: allocate and initialize subviews (labels, image) in the initialize class method, storing them in static pointers, and then set class properties in - (void)drawContentView:(CGRect)r highlighted:(BOOL)highlighted along with background color setting shown in example. Here's the result:
static AsyncUIImageView* image = nil; // A subclass using ASIHTTPRequest for image loading
static UILabel* label1 = nil;
static UILabel* label2 = nil;
+ (void)initialize {
if (self == [ResultTableViewCell class]) {
image = [[[AsyncUIImageView alloc] initWithFrame:CGRectMake(0, 0, 80, 60)] retain];
label1 = [[[UILabel alloc] initWithFrame:CGRectMake(90, 5, 150, 30)] retain];
label1.font = [UIFont fontWithName:#"Helvetica-Bold" size:17];
label1.textColor = [UIColor purpleColor];
label1.backgroundColor = [UIColor clearColor];
label2 = [[[UILabel alloc] initWithFrame:CGRectMake(180, 8, 100, 25)] retain];
label2.font = [UIFont fontWithName:#"Helvetica-Bold" size:12.0];
label2.textColor = [UIColor grayColor];
label2.backgroundColor = [UIColor clearColor];
}
}
- (void)drawContentView:(CGRect)r highlighted:(BOOL)highlighted {
if (self.imageView == nil) {
self.imageView = image;
[self addSubview:image];
self.firstLabel = label1;
[self addSubview:label1];
self.secondLabel = label2;
[self addSubview:label2];
}
CGContextRef context = UIGraphicsGetCurrentContext();
UIColor *backgroundColor = [UIColor whiteColor];
UIColor *textColor = [UIColor blackColor];
if (self.selected || self.highlighted) {
backgroundColor = [UIColor clearColor];
textColor = [UIColor whiteColor];
}
[backgroundColor set];
[textColor set];
CGContextFillRect(context, r);
}
This gives me completely black cells, sometimes one has text and image set with correct colors, but its content changes as I scroll down.
Obviously I did not understand what I am supposed to do in drawContentView.
Could someone explain its purpose?
The whole idea is to not add subviews, but to draw the text instead.
Eg.
- (void)drawContentView:(CGRect)r highlighted:(BOOL)highlighted {
[someText drawInRect:r withFont:aFont];
}