Adding uiTextView before uiScrollView - ios

I want the textview to be before the scrollview inside my tablecell like this, so that the scrollview scrolls and the titles stays put.
[Title goes here] - it does not move with the scrollview
---------------------
| |
| ScrollView |
| |
---------------------
This is my code, the textView appears behind the scrollview.
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:#"MainCell"];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:#"MainCell"];
}
NSInteger numberOfViews = 10;
UIScrollView *vwScroll = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, 1280, 200)];
for (int i = 0; i < numberOfViews; i++) {
CGFloat xOrigin = i * 250;
UITextView *titleView = [[UITextView alloc] initWithFrame:CGRectMake(0, 0, 1280, 20)];
titleView.text = #"Title";
[titleView setFont:[UIFont fontWithName:#"HelveticaBold" size:32]];
UIView *awesomeView = [[UIView alloc] initWithFrame:CGRectMake(xOrigin, 0, 250, 180)];
awesomeView.backgroundColor = [UIColor colorWithRed:0.5/i green:0.5/i/10 blue:0.5 alpha:1];
[awesomeView.layer setBorderColor:[UIColor whiteColor].CGColor];
[awesomeView.layer setBorderWidth:10.5f];
awesomeView.layer.shadowPath = [UIBezierPath bezierPathWithRect:awesomeView.bounds].CGPath;
[cell.contentView addSubview:titleView];
[vwScroll addSubview:awesomeView];
}
vwScroll.contentSize = CGSizeMake(250 * numberOfViews, 200);
[cell.contentView addSubview:vwScroll];
return cell;
}

Why cant you just set the frame of your titleView to, say, (0,0,width,titleHeight) and then set the frame of your scrollView to (0,titleHeight,width,cellHeight-titleHeight).
This will just place the title at the top part of the cell, setting it's height to whatever you make titleHeight to be, and will put the scrollView at the bottom part of the cell, setting it's height to the rest of the cell.

Related

Unable to create section border in tableview and i want to represent the ui as per the attached image?

how to create section border in tableview and i want to represent the ui as per the attached image ? i displayed spaces between sections but cannot set the border line for section.
For this you need to add and import
#import <QuartzCore/QuartzCore.h>
After that follow below steps
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
UIView *section = [[UIView alloc]init];
section.frame = CGRectMake(0, 0, tableview.frame.size.width, height);
UIImageView *imageView = [[UIImageView alloc] initWithImage:[UIImage imageWithName:#"imageName"]];
imageView.frame = CGRectMake(0, 0, YOUR_WIDTH, YOUR_HEIGHT); //set your frame
[section addSubview:imageView];
labelSection = [[UILabel alloc]init];
labelSection.textAlignment = NSTextAlignmentLeft;
labelSection.frame = CGRectMake(10, 5, tableview.frame.size.width, 20);
[labelSection setBackgroundColor:[UIColor clearColor]];
[labelSection setFont:[UIFont boldSystemFontOfSize:15]];
NSString *name = #"section title";
labelSection.text = name;
[labelSection setTextColor:[UIColor blackColor]];
[section addSubview:labelSection];
section.layer.borderWidth = 2.0f;
section.layer.cornerRadius = 1.0f;
section.layer.masksToBounds = YES;
section.layer.borderColor=[UIColor lightGrayColor].CGColor;
return section;
}
If you want to set row height
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
return 44; //It default size.If you want to change to other size you can change.
//OR
// return 90; //Set to your needed size
}

unwanted white space "under" tableView subview when tableView scrolled to its content limits

I have a UITableViewCell as a subview in a custom view controller. It works great except that when it scrolls to its top or bottom of its contentSize limit, it "keeps going" and leaves some white space exposed behind it. This is particularly irritating because I do have a UIView covering the entire screen behind the tableView, and that view is set to a non-white color. I also added a subview exactly underlaying the tableview with the same background color, again attempting to block the white. I also set the UIApplication Window background color to a non white color. None of this worked.
I would have thought that even if my TableView bounces around its origin frame, the "exposed" view should match the underlying view rather than be white. How can I fix my tableView so that it retains all its scroll properties but doesn't reveal white when it bounces around at the end of a scroll?
Here is a screen shot of the effect. The white appears the tableViewHeader and below a UISCrollView that occupies the top of the screen. This appears when I scroll the tableView all the way to one extreme. The white space appears at the bottom rather than the top of the tableView if I scroll all the way to the other end.
Here's the relevant code, quite vanilla I think:
#interface JudgeViewController () <UITextFieldDelegate, UITextViewDelegate, UINavigationControllerDelegate, UIGestureRecognizerDelegate, UITableViewDataSource, UITableViewDelegate, UIViewControllerRestoration, UIScrollViewDelegate>
#property (nonatomic) UITableView *tableView;
#end
functions to set tableViewCells
#pragma mark - tableview appearance and actions
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
StatsViewController *svc = [[StatsViewController alloc] init];
svc.user = self.object.answerUser[indexPath.row];
svc.fromJudge = YES;
[self.navigationController pushViewController:svc animated:YES];
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return [self.object.answerArray count];
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell;
UILabel *label = nil;
cell = [tableView dequeueReusableCellWithIdentifier:#"Cell"];
if(cell ==nil)
{
cell = [[UITableViewCell alloc] initWithFrame:CGRectZero];
label = [[UILabel alloc] initWithFrame:CGRectZero];
[label setLineBreakMode: UILineBreakModeWordWrap];
[label setMinimumFontSize:SMALL_FONT_SIZE];
[label setNumberOfLines:0];
[label setFont:[UIFont systemFontOfSize:SMALL_FONT_SIZE]];
[label setTag:1];
// [[label layer] setBorderWidth:2.0f];
[[cell contentView] addSubview:label];
}
CGFloat width = [[UIScreen mainScreen] bounds].size.width;
CGFloat height = [[UIScreen mainScreen] bounds].size.height;
NSString *text = self.object.answerArray[indexPath.row];
CGSize constraint = CGSizeMake(.8*CELL_CONTENT_WIDTH - (CELL_CONTENT_MARGIN)*2, 200000.0f);
CGSize size = [text sizeWithFont:[UIFont systemFontOfSize:SMALL_FONT_SIZE] constrainedToSize:constraint];
if(!label)
label = (UILabel *)[cell viewWithTag:1];
[label setText:text];
[label setFrame:CGRectMake(CELL_CONTENT_MARGIN, CELL_CONTENT_MARGIN, .8*CELL_CONTENT_WIDTH - (CELL_CONTENT_MARGIN)*2, MAX(size.height, 44.0f))];
UIButton *button1 = [UIButton buttonWithType:UIButtonTypeRoundedRect];
button1.frame = CGRectMake(.85*width, label.frame.size.height/2-2*CELL_CONTENT_MARGIN, .12*width, 20);
[button1 setTitle:#"UP" forState:UIControlStateNormal];
button1.titleLabel.font =[UIFont systemFontOfSize:SMALLEST_FONT_SIZE];
[button1 setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
[button1 addTarget:self action:#selector(upVoteA:) forControlEvents:UIControlEventTouchUpInside];
[cell.contentView addSubview:button1];
[cell.contentView bringSubviewToFront:button1];
UIButton *button2 = [UIButton buttonWithType:UIButtonTypeRoundedRect];
button2.frame = CGRectMake(.85*width, label.frame.size.height/2+2*CELL_CONTENT_MARGIN, .12*width, 20);
[button2 setTitle:#"DOWN" forState:UIControlStateNormal];
button2.titleLabel.font =[UIFont systemFontOfSize:SMALLEST_FONT_SIZE];
[button2 setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
[button2 addTarget:self action:#selector(downVoteA:) forControlEvents:UIControlEventTouchUpInside];
CGFloat moduloResult = indexPath.row % 2;
if(moduloResult>0)
{
cell.backgroundColor = [UIColor colorWithRed:1 green:0.647 blue:0 alpha:.6];
}
else
{
cell.backgroundColor = [UIColor colorWithRed:1 green:0.647 blue:0 alpha:.4];
}
cell.opaque = NO;
cell.alpha = 0.2;
[cell.contentView addSubview:button2];
[cell.contentView bringSubviewToFront:button2];
cell.selectionStyle = UITableViewCellSelectionStyleNone;
return cell;
}
-(void)keyboardToJudge
{
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
if(indexPath.row < [self.object.answerArray count])
{
NSString *text = self.object.answerArray[indexPath.row];
CGSize constraint = CGSizeMake(.8*CELL_CONTENT_WIDTH - (CELL_CONTENT_MARGIN)*2, 200000.0f);
CGSize size = [text sizeWithFont:[UIFont systemFontOfSize:FONT_SIZE-2.0f] constrainedToSize:constraint];
CGFloat height = MAX(size.height, 44.0f);
return height + (CELL_CONTENT_MARGIN)*2;
}
else
{
return 200.0f;
}
}
functions setting out layout:
-(void)viewDidLoad
{
...among other things setting up top scroll view (top part of view with gray background and orange text)...
if(self.navigationController.navigationBar.frame.size.height>0)
{
self.scroll = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 5, width, height*SCROLL_VIEW_OFFSET)];
}
else
{
self.scroll = [[UIScrollView alloc] initWithFrame:CGRectMake(0, statusBarHeight+5, width, height*SCROLL_VIEW_OFFSET)];
}
self.scroll.backgroundColor = BACKGROUND_COLOR;
self.scroll.contentSize =CGSizeMake(width, .5*height);
self.scroll.delegate = self;
self.scroll.contentInset = UIEdgeInsetsMake(30, 0, 30, 0);
[self.scroll setShowsHorizontalScrollIndicator:NO];
...adding buttons to self.scroll...
[self.view addSubview:self.scroll];
....
self.tableView = [[UITableView alloc] initWithFrame:frame];
self.tableView.delegate = self;
self.tableView.dataSource = self;
self.tableView.tableFooterView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, _width, _height*.1)];
self.tableView.tableFooterView.backgroundColor = BACKGROUND_COLOR;
self.tableView.tableHeaderView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, _width, _height*.1)];
self.tableView.tableHeaderView.backgroundColor = BACKGROUND_COLOR;
....tableView hidden state is changed to yes in another function if row count is zero but not usually...
self.tableView.hidden = NO;
[self.view addSubview:self.tableView];
...
}
Finally, I also call :
[self.tableView reloadData];
after reloading data from a webserver and depending on the results either set the tableView to hidden or not (not hidden if there are results to display). That should be every line of code that touches the tableView subview.
Add to your viewDidLoad
[self.tableView setBackgroundColor:BACKGROUND_COLOR];
Set the background color of your tableView and you'll be fine
Change this line:
self.scroll.contentInset = UIEdgeInsetsMake(30, 0, 30, 0);
To this:
self.scroll.contentInset = UIEdgeInsetsMake(0, 0, 0, 0);
You are adding a footer to your tableview with that line. The following image is from the IOS dev Library
Set the table view estimate to 0 (Uncheck Automatic) in the size inspector

UITableView center a UIImage and UILabel in viewForHeaderInSection

I wanna center vertically and horizontally an image inside of HeaderSection and a label inside of the image. But I don't have a clear idea about how make that. My code is:
- (UIView *) tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
UIImage *image = [UIImage imageNamed:#"bg_title_category"];
UIView *headerView = [[[UIView alloc] initWithFrame:CGRectMake(0, 0, image.size.width, image.size.height)] autorelease];
UILabel *sectionTitle = [[[UILabel alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 30)] autorelease];
UIImageView *sectionHeaderBG;
sectionTitle.text = #"Trial"; //[[tableDataSource objectAtIndex: section] objectForKey: #"Title"];
sectionTitle.textAlignment = NSTextAlignmentCenter;
sectionTitle.font = [UIFont fontWithName:#"Helvetica-Bold" size:14];
sectionTitle.textColor = [UIColor whiteColor];
sectionTitle.shadowColor = [UIColor colorWithWhite:0 alpha:0.4];
sectionTitle.shadowOffset = CGSizeMake(1, 1);
sectionTitle.backgroundColor = [UIColor colorWithWhite:0 alpha:0];
sectionHeaderBG = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, _tableView.frame.size.width/2, image.size.height)];
sectionHeaderBG.image = [UIImage imageNamed:#"bg_title_category"];
[headerView addSubview:sectionHeaderBG];
[headerView addSubview:sectionTitle];
return headerView;
}
But this code not center anything... Thanks in advance!
The problem is that you're creating headerView with origin (0,0) and it's size the same one as your image; then you're adding all of your views to headerView. When the table view adds this header view it will not be centered, rather it'll placed at (0,0).
What I'd suggest you to do is to create headerView with the same origin (0,0) but with the width of your tableView and the height depening on you. Let's just assume for now the height will be the same as your image plus 10px at the top and bottom just to give it some margin. Then you can add your UIImageView and UILabel inside headerView and center them with respect to it. It'd be something like this:
- (UIView *) tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
UIImage *image = [UIImage imageNamed:#"bg_title_category"];
// Create your headerView with the same width as the tableView and a little taller than the image
UIView *headerView = [[UIView alloc] initWithFrame:CGRectMake(0,0, tableView.bounds.size.width, image.size.height + 20)]; //10px top and 10px bottom. Just for illustration purposes.
// Create the image view
UIImageView *sectionHeaderBG = [[UIImageView alloc] initWithImage:image];
// Now center it and add it to headerView
sectionHeaderBG.center = CGPointMake(headerView.bounds.size.width/2, headerView.bounds.size.height/2);
[headerView addSubview: [sectionHeaderBG autorelease]];
// Now it's turn of the label. Again I suggest using the tableView's width
UILabel *sectionTitle = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, tableView.bounds.size.width, 30)];
// Now center it. You could even do this when creating it's frame
sectionTitle.center = CGPointMake(headerView.bounds.size.width/2, headerView.bounds.size.height/2);
// do the rest of the configuration for your label...
// and add it to headerView
[headerView addSubview: [sectionTitle autorelease]];
return [headerView autorelease];
}
Hope this helps!

custom uitableheader view

I want to put a custom view inside my uitableviewheader, this custom view has an UILabel and a UITextView, the label have a fixed size while uitextview need to change depending the content of uitextview.
For doing it I use this code:
- (CGFloat) tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
return 100;
}
- (UIView *) tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
headerView = [[UIView alloc] initWithFrame:CGRectMake(0,0,320,100)];
headerView.backgroundColor = [UIColor blueColor];
NSString *userString = [user username];
UILabel *userLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 320, 40)];
[userLabel setTextAlignment:NSTextAlignmentCenter];
[userLabel setText:userString];
userLabel.backgroundColor = [UIColor redColor];
[headerView addSubview:userLabel];
UITextView *textView = [[UITextView alloc] initWithFrame:CGRectMake(0, 40, 320, 60)];
NSString *postString = [currentPost objectForKey:#"postTextKey"];
textView.text = postString;
[headerView addSubview:textView];
CGRect frame = textView.frame;
frame.size.height = textView.contentSize.height;
textView.frame = frame;
frame = headerView.frame;
frame.size.height = textView.frame.size.height+40;
headerView.frame = frame;
self.tableView.tableHeaderView.frame = headerView.frame;
return headerView;
}
the problem is that if the headerview have the correct height the tableviewheader is wrong. Where is the mistake?
1) tableHeaderView is different from section header view (headerViewForSection:)
2) If you want to change the height you specify it in the - (UIView *) tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section method and then call the reloadData method if the height changes.
Note: you don't want to do that too often.

UIView frame not updating size on time for creating scrolling text animation in UILabel

I'm doing the usual "add your own labels/images to cell.contentview" from within a controller's cellForRowAtIndexPath. And I'm trying to have one of the labels have animated scrolling text: that is, if the text in the label is overflows the viewCell width, the label in the cell should scroll the text (animated - not manual). Also, this whole tableview + scrolling text should work for both iPhone and iPad (so everything needs to autoresize accordingly).
Cutting to the chase, the question is:
How do I get a a subview of cell.contentView to update it's frame size on time for testing if the text inside this subview overflows or not.
Or perhaps, is this even the right way to go about it?
Here's an image of what I mean (secondLabel,etc are explained below):
Now you can keep reading for the details:
The way I tried going about it is having cellForRowAtIndexPath add a custom UISCrollLabelView instance to a cell's contentView. Where this custom class is really a UIView that manages internally a UILabel.
UISCrollLabelView should autoresize to it doesn't overflow the table cell (should work for both iPhone and iPad). And then, depending on the length of the text passed in to it by cellForRowAtIndexPath, it should autoresize its internal label to fit all the text and if the label ends up overflowing the view (self), animate the scrolling of the UILabel.
I have a couple problems with this, but the main one being: UIScrollableView triggers (or not) the animation based on comparing its internal label's frame.size.width with self.frame.size.width. But apparently, it takes some time for self's frame width to update from 0 to the size it ends up resizing to after inserted in the cell.contenView. Which means that when my custom UIScrollLabelView tests (label.frame.size.width > self.frame.size.width) this is always true and text that does not overflow still scrolls. A second or so later, self.frame does update to the correct size.
Here's cellForRowAtIndexPath (secondLabel is the scrollView here):
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UIImageSmartView *cachedImage;
UILabel *mainLabel;
UIScrollLabelView *secondLabel;
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil)
{
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault
reuseIdentifier:CellIdentifier] autorelease];
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
cell.selectionStyle = UITableViewCellSelectionStyleGray;
mainLabel = [[[UILabel alloc] initWithFrame:CGRectMake(70, 0, 0, 20)] autorelease];
mainLabel.font = [UIFont boldSystemFontOfSize:18];
mainLabel.textAlignment = UITextAlignmentLeft;
mainLabel.textColor = [UIColor blackColor];
mainLabel.backgroundColor = [UIColor clearColor];
mainLabel.autoresizingMask = (UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleRightMargin);
mainLabel.tag = MAINLABEL_TAG;
secondLabel = [[[UIScrollLabelView alloc] initWithFrame:CGRectMake(70, 40, 0, 20)] autorelease];
secondLabel.autoresizingMask = (UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleRightMargin);
//secondLabel.backgroundColor = [UIColor clearColor];
secondLabel.tag = SECONDLABEL_TAG;
cachedImage = [[UIImageSmartView alloc] initWithFrame:CGRectMake(5, 5, 57, 80)];
cachedImage.tag = PHOTO_TAG;
[cell.contentView addSubview:cachedImage];
[cell.contentView addSubview:mainLabel];
[cell.contentView addSubview:secondLabel];
}
else
{
cachedImage = (UIImageSmartView*)[cell.contentView viewWithTag:PHOTO_TAG];
mainLabel = (UILabel*)[cell.contentView viewWithTag:MAINLABEL_TAG];
secondLabel = (UIScrollLabelView*)[cell.contentView viewWithTag:SECONDLABEL_TAG];
}
// Configure the cell...
NSString *ImageName = [[dbData objectAtIndex:indexPath.row] objectAtIndex:2];
NSString *imageURL = [NSString stringWithFormat:#"http://someserver", referencingTable];
[cachedImage loadAndCacheImageFromFile:ImageName fromURL:imageURL inSize:CGSizeMake(57, 80) withBorderWidth:4];
mainLabel.text = [[dbData objectAtIndex:indexPath.row] objectAtIndex:0];
// -> At this point I load the text into the "scrolling label"
// (actually a UIView with a label)
[secondLabel setScrollingText:[[dbData objectAtIndex:indexPath.row] objectAtIndex:1]];
return cell;
}
And my UIScrollLabelView implementation looks like this so far:
#implementation UIScrollLabelView
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self)
{
// Initialization code
isScrolling = NO;
self.clipsToBounds = YES;
UILabel *label = [[[UILabel alloc] init] autorelease];
label.font = [UIFont systemFontOfSize:14.0];
label.textAlignment = UITextAlignmentLeft;
label.textColor = [UIColor darkGrayColor];
label.backgroundColor = [UIColor greenColor];
self.backgroundColor = [UIColor redColor];
//label.backgroundColor = [UIColor clearColor];
[self addSubview: label];
}
return self;
}
- (void)setScrollingText:(NSString*)text
{
[self setNeedsLayout];
UILabel *label = [[self subviews ] objectAtIndex:0];
label.text = text;
[label sizeToFit];
if(label.frame.size.width > self.frame.size.width)
[self scrollText];
}
- (void)scrollText
{
if(isScrolling)
return;
isScrolling = YES;
UILabel *label = [[self subviews ] objectAtIndex:0];
[UIView beginAnimations:#"scroll" context:nil];
[UIView setAnimationBeginsFromCurrentState:YES];
[UIView setAnimationDelegate:self];
[UIView setAnimationCurve:UIViewAnimationCurveLinear];
[UIView setAnimationDidStopSelector:#selector(scrollDidComplete)];
[UIView setAnimationDuration: label.frame.size.width/(float)100];
CGRect frame = label.frame;
if(frame.origin.x == 0)
frame.origin.x = frame.origin.x - (frame.size.width - self.frame.size.width);
else
frame.origin.x = 0;
label.frame = frame;
[UIView commitAnimations];
}
- (void)scrollDidComplete
{
isScrolling = NO;
[NSTimer scheduledTimerWithTimeInterval:1 target:self selector:#selector(scrollText) userInfo:nil repeats:NO];
}
#end
You are setting the label size when the size of the cell is not defined, yet. And then you never check for the actual size again.
You need to override setFrame and setBounds to catch frame size changes.

Resources