In one of my app's user interfaces i want my app display a scrolling view(the count of the scrolled pages is dynamically set in runtime). But i want the to add UIWebView and UIView elements to the same array, so that when i scroll i will be able to display both UIWebView(let's say a web page) and some other View?
EDIT
I received this answer which looks as if exactly ok but doesn't work(or i couldn't to)
here is the code:
NSArray *myArray;
// Fill the array with a bunch of UIWebView and UIView objects here
for (id theObject in myArray)
{
if ([theObject isKindOfClass:[UIWebView class]]) {
// do stuff
}
else if ([theObject isKindOfClass:[UIView class]]) {
// do other stuff
}
}
Can anyone please give a hend of help? I need to create a scrollable array of views(like the main page of the iPhone) but the first page(mean, view) must be a member of UIWebView(the others are UIView)
And below is all my code(i han't try to do anything here:)
CGRect frame;
[webView initWithFrame:frame];
NSArray *colors = [NSArray arrayWithObjects:webView, [UIColor blackColor],[UIColor blackColor], nil];
for (int i = 0; i < colors.count; i++) {
CGRect frame;
frame.origin.x = self.scrollView.frame.size.width * i;
frame.origin.y = 0;
frame.size = self.scrollView.frame.size;
UIView *subview = [[UIView alloc] initWithFrame:frame];
subview.backgroundColor = [colors objectAtIndex:i];
[subview setTag:i];
if([subview tag]==2){
UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
button.frame = CGRectMake(70.0f, 600.0f, 250.0f, 60);
[button setTitle:#"Read Messages" forState:UIControlStateNormal];
for(NSDictionary* buttonData in butDat) {
UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
NSString* buttonTitle = [buttonData objectForKey:#"name"];
NSString* buttonID=[buttonData objectForKey:#"order_number"];
[button setTitle:buttonTitle forState:UIControlStateNormal];
[button setTag:[buttonID intValue]];
button.titleLabel.font=[UIFont systemFontOfSize:28];
CGFloat yPosition = 60.0f;
const CGFloat buttonHeight = 85.0f;
if (button.tag==1) {
button.frame = CGRectMake(70.0f, yPosition, 250.0f, buttonHeight);
//[ma.view addSubview:button];
}
if (button.tag==2) {
button.frame = CGRectMake(440.0f, yPosition, 250.0f, buttonHeight);
//[ma.view addSubview:button];
}
if (button.tag==3) {
button.frame = CGRectMake(70.0f, 200.0f, 250.0f, buttonHeight);
//[ma.view addSubview:button];
}
if (button.tag==4) {
button.frame = CGRectMake(440.0f, 200.0f, 250.0f, buttonHeight);
//[menu_2.view addSubview:button];//****
}
if (button.tag==5) {
button.frame = CGRectMake(70.0f, 335.0f, 250.0f, buttonHeight);
//[menu_2.view addSubview:button];//****
}
if (button.tag==6) {
button.frame = CGRectMake(440.0f, 335.0f, 250.0f, buttonHeight);
//[menu_2.view addSubview:button];//****
}
[button addTarget:self action:#selector(secondAct:) forControlEvents:UIControlEventTouchUpInside];
[scrollView addSubview:button];
}
//
[button addTarget:self action:#selector(readM:) forControlEvents:UIControlEventTouchUpInside];
// [scrollView addSubview:button];
[self.view addSubview:button];
// [self.scrollView addSubview:button];
}
[self.scrollView addSubview:subview];
There's nothing stopping you from adding objects of type UIWebView and UIView to the same NSArray.
If your code needs to know if you're dealing with a UIWebView or a UIView, you can use the method isKindOfClass: to check:
NSArray *myArray;
// Fill the array with a bunch of UIWebView and UIView objects here
for (id theObject in myArray)
{
if ([theObject isKindOfClass:[UIWebView class]]) {
// do stuff
}
else if ([theObject isKindOfClass:[UIView class]]) {
// do other stuff
}
}
Furthermore, UIWebView is a subclasses of UIView and inherits many of the same methods, making view layout the same in both cases.
From a user interface standpoint, it's probably bad practice to have scroll views inside scroll views, so you're going to want to disable scrolling of your UIWebView objects. If you are targeting iOS 5 and above you can use the following:
webView.scrollView.scrollEnabled = NO;
webView.scrollView.bounces = NO;
and if you're on a version of iOS lower than 5.0, you can inject the following javascript:
<script type="text/javascript">
touchMove = function(event) {
event.preventDefault();
}
</script>
To implement the javascript injection, you can use UIWebView's stringByEvaluatingJavaScriptFromString: method inside webViewDidFinishLoad:
See here for more information on UIWebView javascript injection: http://iphoneincubator.com/blog/windows-views/how-to-inject-javascript-functions-into-a-uiwebview
Edit: Looking at your code, it seems as thought you're sticking a UIWebView and two UIColor objects into your array. Therefore you need to check if you're dealing with a UIWebView or a UIColor, or you'll crash on subview.backgroundColor = [colors objectAtIndex:i]; Also you set your UIWebView object's frame to CGRect frame; which is uninitialized.
You should populate your NSArray with your UIWebView and then two UIView objects with their background colors set:
UIWebView *webView = [[UIWebView alloc] init];
UIView *viewOne = [[UIView alloc] init];
[viewOne setBackgroundColor:[UIColor blackColor]];
UIView *viewTwo = [[UIView alloc] init];
[viewTwo setBackgroundColor:[UIColor blackColor]];
NSArray *viewArray = [NSArray arrayWithObjects:webView, viewOne, viewTwo, nil];
for (id theObject in viewArray)
{
if ([theObject isKindOfClass:[UIWebView class]]) {
// do stuff
}
else if ([theObject isKindOfClass:[UIView class]]) {
// do other stuff
}
}
Yes. UIWebViews descend from UIViews. You may use them wherever you would insert a UIView as a subview, including within a UIScrollView parent view, or as an object to be inserted into an array.
Your scroll view is not scrolling because you didn't set the contentsize property. First you have to find the content size appropriately, and then you have to set content size as
[scrollView setContentSize:CGSize];
at the end of code. ie; after
[self.scrollView addSubview:subview];
For a blind try, use
[scrollView setContentSize:CGSizeMake(1000,1000)];
this will definitely scroll the view!!!
Related
I have the following code to change the appearance of the UISearchBar in my application and it can be seen in the image below also:
for(int i = 0; i < [[searchBar subviews] count]; i++){
UIView *subView = [[searchBar subviews] objectAtIndex:i];
if([[NSString stringWithFormat:#"%#", [subView class]] isEqualToString:#"UINavigationButton"]){
UIButton *cancelButton = (UIButton *)subView;
CGRect buttonFrame = cancelButton.frame;
buttonFrame.size.height = 52;
[cancelButton setFrame:buttonFrame];
[cancelButton setTitle:#"Cancel" forState:UIControlStateNormal];
[cancelButton setBackgroundImage:[UIImage imageNamed:#"cancel.png"] forState:UIControlStateNormal];
[cancelButton setBackgroundImage:[UIImage imageNamed:#"cancel_pressed.png"] forState:UIControlStateHighlighted];
}
}
As you can see I am trying to change the height of the button contained in the UISearchBar with no luck. I do get reference to the button as I can change the text and the background Image is changed just not the height of the frame. I would just like the height of the button to be the same as the search box at 52px.
EDIT:
I have found a really hacky solution, but it's not very elegant of adding a UIButton as the subview of the Cancel Button. It does the job but as I say it's not very nice.
Actually you could use method below to access the cancel Button:
UIBarButtonItem *cancelButton = [UIBarButtonItem appearanceWhenContainedIn:[UISearchBar class], nil];
So I override -layoutSubViews with this, but theres 2 issues I have
I hate iterating through the views to find the matching view, and it messes up the animation a little bit
- (UIButton *)cancelButton {
for (UIView *v in self.subviews) {
if ([v isKindOfClass:[UIButton class]]) {
NSString *caption = [[((UIButton *)v) titleLabel] text];
if ([caption isEqualToString:#"Cancel"] )
return (UIButton *)v;
}
}
return nil;
}
- (BOOL)cancelButtonIsShowing {
return ([self cancelButton] != nil);
}
- (void)layoutSubviews {
[super layoutSubviews];
if ([self cancelButtonIsShowing]) {
UIButton *cancelButton = [self cancelButton];
UIImage *i = [UIImage imageNamed:#"Cancel_BTN"];
CGRect f = CGRectMake(267, 5, i.size.width, i.size.height);
cancelButton.frame = f;
}
}
I am looking for an example that show just vertical scrolling in iPhone.
The Apple supplied Scrolling example allows swiping of images left to
right.
Example Link
Example Link
I want to have my images scroll top to bottom like Instagram
App.
I research about it and found lots of example, but they all show
horizontal scrolling.
Example
Example
Example
Does anyone know how to do this?
I appreciate if you send me a link to a tutorial.
Hi i solved your problem with apple code,
just change the function with
- (void)layoutScrollImages
{
UIImageView *view = nil;
NSArray *subviews = [scrollView1 subviews];
// reposition all image subviews in a horizontal serial fashion
CGFloat curXLoc = 0;
for (view in subviews)
{
if ([view isKindOfClass:[UIImageView class]] && view.tag > 0)
{
CGRect frame = view.frame;
frame.origin = CGPointMake(0,curXLoc);
view.frame = frame;
curXLoc += (kScrollObjHeight);
}
}
// set the content size so it can be scrollable
[scrollView1 setContentSize:CGSizeMake(([scrollView1 bounds].size.width),kNumImages * kScrollObjHeight)];
}
or if you want sample then i will give you
For your purpose refer your link:
http://idevzilla.com/2010/09/16/uiscrollview-a-really-simple-tutorial/
There is a code like:
scroll.contentSize = CGSizeMake(self.view.frame.size.width * numberOfViews, self.view.frame.size.height);
For horizontal scrolling you need to set the width of contentSize property to a higher value than the scroll view's frame width. Likewise for vertical scrolling you need to set the height of contentSize property to a higher value than the scroll view's frame height.
Reference
In the above code it can be done like:
scroll.contentSize = CGSizeMake(self.view.frame.size.width, numberOfViews * self.view.frame.size.height);
You can use the following code to scroll images from top to bottom and vice versa.!!
- (void)viewDidLoad
{
//....
UISwipeGestureRecognizer *recognizer;
//for scrolling up
recognizer = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:#selector(SwipeUp)]; // calling method SwipeUp
[recognizer setDirection:(UISwipeGestureRecognizerDirectionUp)];
[[self view] addGestureRecognizer:recognizer];
//for scrolling down
UISwipeGestureRecognizer *recognizer1;
recognizer1 = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:#selector(SwipeDown)]; //calling method SwipeDown
[recognizer1 setDirection:(UISwipeGestureRecognizerDirectionDown)];
[[self view] addGestureRecognizer:recognizer1];
}
//Initialise and synthesise IBOutlet UIImageView *bgImage;
-(IBAction)SwipeUp
{
[bgImage setImage:[UIImage imageNamed:#"yourImage1"] ];
bgImage.opaque=YES;
[self.view addSubview:bgImage];
}
-(IBAction)SwipeDown
{
[bgImage setImage:[UIImage imageNamed:#"yourImage2"] ];
bgImage.opaque=YES;
[self.view addSubview:bgImage];
}
I don't know if I understood correctly what you want to accomplish but basically you can set the position of you scrollView using setContentOffset.
CGPoint bottomOffset = CGPointMake(0, scrollView.contentSize.height - self.scrollView.bounds.size.height);
[scrollView setContentOffset:bottomOffset animated:YES];
There are several ways of doing this for example:
Using the UITableView with Custom cell
Adding UIScrollingView & adding the content/images & increasing the
height of scrollview according to number of objects in array.
Here is the code snippet which creates a 2*2 Grid for images :
//Over here adding the image names into the Array
NSMutableArray *imageArray=[[NSMutableArray alloc]init];
[Arr addObject:[UIImage imageNamed:#"image_1.png"]];
[Arr addObject:[UIImage imageNamed:#"image_2.png"]];
[Arr addObject:[UIImage imageNamed:#"image_3.png"]];
[Arr addObject:[UIImage imageNamed:#"image_4.png"]];
[Arr addObject:[UIImage imageNamed:#"image_5.png"]];
// Initlaizing the ScrollView & setting the frame :
scrollView = [[UIScrollView alloc]initWithFrame:CGRectMake(0, 0,320, 460)];
[scrollView setBackgroundColor:[UIColor clearColor]];
scrollView.showsHorizontalScrollIndicator = NO;
scrollView.showsVerticalScrollIndicator = NO;
int row = 0;
int column = 0;
for(int i = 0; i < imageArray.count; i++) {
UIButton * button = [UIButton buttonWithType:UIButtonTypeCustom];
button.backgroundColor=[UIColor clearColor];
button.frame = CGRectMake(column*160+0,row*210+10, 160, 190);
[button setImage:[imageArray objectAtIndex:i] forState:UIControlStateNormal];
[button setBackgroundColor:[UIColor clearColor]];
[button addTarget:self action:#selector(chooseCustomImageTapped:) forControlEvents:UIControlEventTouchUpInside];
button.tag = i;
[view1 addSubview:button];
if (column == 1) {
column = 0;
row++;
} else {
column++;
}
}
[scrollView setContentSize:CGSizeMake(320, (row+1) * 210)];
[self.view addSubview:view1];
//Here is the method used for Tapping :
- (IBAction)chooseCustomImageTapped:(id)sender
{
}
Now,once the scrollView is ready for Vertical scrolling, you can do your own customization accordingly. I hope this would work for you.
I have defined a uiscrollview and used a for loop to add buttons to it, but whenever I run the program, I come up with a blank screen.
Here is my viewDidLoad, this is all the code i have relating to this.
- (void)viewDidLoad
{
[super viewDidLoad];
scrollView = [[UIScrollView alloc]initWithFrame:CGRectMake(0, 0, 320, 1000)];
scrollView.hidden = NO;
names = [[NSMutableArray alloc]initWithObjects:#"Excavations",#"Concrete",#"Framing",#"Insulation",#"Drywall",#"Finish Carpentry",#"Paint",#"Electrical",#"HVAC",#"Plumbing",#"Roofing",#"Tile",#"Doors",#"Windows",#"Flooring",#"Countertops",#"Landscape",#"Hardscape", nil];
for (int i = 0; i < 18; i++) {
UIButton *button = [[UIButton alloc]init];
button.frame = CGRectMake(0, 50*i, 320, 50);
button.tag = i;
[button addTarget:self action:#selector(NewView:) forControlEvents:UIControlEventTouchUpInside];
button.titleLabel.text = [names objectAtIndex:i];
button.hidden= NO;
[scrollView addSubview:button];
}
// Do any additional setup after loading the view, typically from a nib.
}
You need to set up the type for button manually, or just initialize with it:
UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
Edit: you also forgot to add your scrollview to main view
[self.view addSubview:scrollView];
You have to add the UIScrollView to your view. Try
...
scrollView = [[UIScrollView alloc]initWithFrame:CGRectMake(0, 0, 320, 1000)];
scrollView.hidden = NO;
[self.view addSubview:scrollView];
...
i have an iPhone app in which i have several images. These images add in image view. That image view add sub view with scroll view. Now i want to add a transparent button on every image. How can i do that? I have shown my code below:
- (void)layoutScrollImages{
UIImageView *view = nil;
NSArray *subviews = [scrollView1 subviews];
// reposition all image subviews in a horizontal serial fashion
CGFloat curXLoc = 0;
for (view in subviews)
{
if ([view isKindOfClass:[UIImageView class]] && view.tag > 0)
{
CGRect frame = view.frame;
frame.origin = CGPointMake(curXLoc, 0);
view.frame = frame;
curXLoc += (kScrollWidth);
}
}
// set the content size so it can be scrollable
[scrollView1 setContentSize:CGSizeMake((kNoImages * 500), 700)];
}
- (void)viewDidLoad{
self.view.backgroundColor = [UIColor viewFlipsideBackgroundColor];
// 1. setup the scrollview for multiple images and add it to the view controller
//
// note: the following can be done in Interface Builder, but we show this in code for clarity
[scrollView1 setBackgroundColor:[UIColor blackColor]];
[scrollView1 setCanCancelContentTouches:NO];
scrollView1.indicatorStyle = UIScrollViewIndicatorStyleWhite;
scrollView1.clipsToBounds = YES; // default is NO, we want to restrict drawing within our scrollview
scrollView1.scrollEnabled = YES;
//imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"image0.jpg"]];
[scrollView1 addSubview:imageView];
[scrollView1 setContentSize:CGSizeMake(500,700)];
scrollView1.minimumZoomScale = 1;
scrollView1.maximumZoomScale = 3;
scrollView1.delegate = self;
[scrollView1 setScrollEnabled:YES];
// pagingEnabled property default is NO, if set the scroller will stop or snap at each photo
// if you want free-flowing scroll, don't set this property.
scrollView1.pagingEnabled = YES;
// load all the images from our bundle and add them to the scroll view
NSUInteger i;
for (i = 1; i <= kNoImages; i++)
{
NSString *imageName = [NSString stringWithFormat:#"page-%d.jpg", i];
UIImage *image = [UIImage imageNamed:imageName];
UIImageView *ImageView = [[UIImageView alloc] initWithImage:image];
// setup each frame to a default height and width, it will be properly placed when we call "updateScrollList"
CGRect rect = ImageView.frame;
rect.size.height = kScrollHeight;
rect.size.width = kScrollWidth;
ImageView.frame = rect;
ImageView.tag = i; // tag our images for later use when we place them in serial fashion
UIButton *btnView2= [UIButton buttonWithType:UIButtonTypeCustom];
[btnView2 setTitle:#"view" forState:UIControlStateNormal];
[btnView2 addTarget:self action:#selector(View:)forControlEvents:UIControlEventTouchDown];
[btnView2 setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
btnView2.frame=CGRectMake(0,0,460,460 );
[scrollView1 addSubview:btnView2];
scrollView1.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleRightMargin;
[scrollView1 addSubview:ImageView];
[ImageView release];
}
[self layoutScrollImages]; // now place the photos in serial layout within the scrollview
}
-(IBAction)View:(id)sender{
NSURL *imgUrl=[[NSURL alloc] initWithString:#"http://farm4.static.flickr.com/3567/3523321514_371d9ac42f.jpg"];
NSData *imgData = [NSData dataWithContentsOfURL:imgUrl];
UIImage *img = [UIImage imageWithData:imgData];
UIImageView *imgView = [[UIImageView alloc] initWithImage:img];
[self.view addSubview:imgView];
//[self.navigationController pushViewController:ivc animated:YES];
[imgUrl release];
}
Instead of adding Imageview and transparent button on top of Imageview. You can just add UIButton customtype and set button image to your image in your scrollview. There is no need to take imageview and UIButton both. Just take UIButton and setImage and you will be fine. I tried to give you sample code below from my app. Please modify it as per your need. I think it is what you need.
First of Take all constant in one of global file. I took it in en.lproj. like below.
"TotalThumbnailCount"="6";
"Coloumn"="2";
"ThumbnailHeight"="151";
"ThumbnailWidth"="151";
//Marging between two images
"MarginX" = "6";
"MarginY" = "6";
Then initialize all your local varaibles from global variables. like below
-(void)PopulateVariables{
TotalThumbnail = [NSLocalizedString(#"TotalThumbnailCount",nil) intValue];
Colomn = [NSLocalizedString(#"Coloumn",nil) intValue];
ThumbnailHeight = [NSLocalizedString(#"ThumbnailHeight",nil) intValue];
ThumbnailWidth = [NSLocalizedString(#"ThumbnailWidth",nil) intValue];
MarginX = [NSLocalizedString(#"MarginX",nil) intValue];
MarginY= [NSLocalizedString(#"MarginY",nil) intValue];
}
Now, you should initiate your thumbnail images using UIButton from below function.
-(void)PopulateThumbnails
{
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
XCordinate=MarginX;
YCordinate = MarginY;file:
for(int i=1; i <=TotalThumbnail;i++){
UIButton *btnMenu = [UIButton buttonWithType:UIButtonTypeCustom];
//NSData *data =UIImageJPEGRepresentation(, 1);
[btnMenu setBackgroundImage:[UIImage imageNamed:[NSString stringWithFormat:#"%d.png",i]] forState:UIControlStateNormal];
CGRect frame = btnMenu.frame;
frame.size.width=ThumbnailWidth;
frame.size.height=ThumbnailHeight;
frame.origin.x=XCordinate;
frame.origin.y=YCordinate;
btnMenu.frame=frame;
btnMenu.tag=i;
btnMenu.alpha = 1;
[btnMenu addTarget:self action:#selector(btnSelected:) forControlEvents:UIControlEventTouchUpInside];
[scrollView addSubview:btnMenu];
XCordinate = btnMenu.frame.origin.x + btnMenu.frame.size.width + MarginX;
if(i%Colomn==0)
{
XPosition = XCordinate;
YCordinate = btnMenu.frame.origin.y + btnMenu.frame.size.height + MarginY;
XCordinate = MarginX;
}
}
[pool release];
}
And then set your scrollview contentsize
scrollView.contentSize = CGSizeMake(XPosition, YCordinate);
When some one tap on any image it will goes in below event.
-(IBAction)btnSelected:(id)sender{
UIButton *btnSelected = sender;
switch (btnSelected.tag) {
}
Let me know if I miss anything and if you don't understand.. Hope this help.
You can add UITapGestureRecognizer to each image:
UITapGestureRecognizer *tapGesture = [[[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(tapAction:)] autorelease];
[imageView addGestureRecognizer:tapGesture];
[self.view addSubview:imageView];
When you create your imageview, create a uibutton and add it as a subview to the imageview.
Set the properties of the button to be custom type. Also set the frame of the button same as that of the imageview.
button = [UIButton buttonWithType:UIButtonTypeCustom];
Google how to create a uibutton programatically. You can then add the necessary selectors to handle button press events.
Alternatively you can create a element in Interfacebuilder. A custom class which has a uiimageview and clear uibutton. You can then use this element to add to your uiscrollview.
Okay, a short breif of my app before i explain the problem.
My app has two views for it's custom TableViewCell, one frontview, and one backview(which is revealed once you swipe your finger across the cell, much like the twitter-app).
Anywho, i wanted to have some buttons on the backview. I did this in the cellForRowAtIndexPath-method
The first you'll see here is how i assign labels to the cells. The secondary thing you'll see is the button. It's working fine.
UILabel *nextArtist = [UILabel alloc];
nextArtist.text = #"Rihanna";
nextArtist.tag = 4;
[cell setNextArtist:nextArtist];
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
button.frame = CGRectMake(6 ,31, 110, 20);
[button setImage:[UIImage imageNamed:#"radionorge.png"] forState:UIControlStateNormal];
[button addTarget:self action:#selector(touched:) forControlEvents:UIControlEventTouchUpInside];
[cell.backView addSubview:button];
But, it's in the next method the problem occours.
-(void)touched:(id)sender {
// Here i want to get the UILabels for each cell. Such as nextArtist.
if ([sender isKindOfClass:[UIButton class]]) {
UIButton *button = (UIButton *)sender;
UIView *contentView = button.superview;
UIView *viewWithTag4 = [contentView viewWithTag:4];
if ([viewWithTag1 isKindOfClass:[UILabel class]]) {
UILabel *titleLabel = (UILabel *)viewWithTag4;
NSLog(#"Label: ",titleLabel.text);
}
}
}
So, i realised that i cannot just go to the superview of the button and find my labels there, because their in another subview. I have scanned all my views, but still cannot find the label.
I am very new at this, and the subclassing of the TableView-cell is something i implemented from someone who posted their code.
But, my assumption is that there are noe UILabels in my view, because i am not adding them as views, only drawing them, with the drawTextInRect-function.
[nextArtist drawTextInRect:CGRectMake(boundsX+200 ,46, 110, 15)];
I have tried to adding them as subviews, but with no luck. Could anyone help me?
// Some more code you may need to solve the puzzle(this is where the cell-views are made)
#implementation RadioTableCellView
- (void)drawRect:(CGRect)rect {
if (!self.hidden){
[(RadioTableCell *)[self superview] drawContentView:rect];
}
else
{
[super drawRect:rect];
}
}
#end
#implementation RadioTableCellBackView
- (void)drawRect:(CGRect)rect {
if (!self.hidden){
[(RadioTableCell *)[self superview] drawBackView:rect];
}
else
{
[super drawRect:rect];
}
}
#end
#interface RadioTableCell (Private)
- (CAAnimationGroup *)bounceAnimationWithHideDuration:(CGFloat)hideDuration initialXOrigin:(CGFloat)originalX;
#end
#implementation RadioTableCell
#synthesize contentView;
#synthesize backView;
#synthesize contentViewMoving;
#synthesize selected;
#synthesize shouldSupportSwiping;
#synthesize shouldBounce;
- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
if ((self = [super initWithStyle:style reuseIdentifier:reuseIdentifier])) {
[self setBackgroundColor:[UIColor clearColor]];
RadioTableCellView * aView = [[RadioTableCellView alloc] initWithFrame:CGRectZero];
[aView setClipsToBounds:YES];
[aView setOpaque:YES];
[aView setBackgroundColor:[UIColor clearColor]];
[self setContentView:aView];
[aView release];
RadioTableCellBackView * anotherView = [[RadioTableCellBackView alloc] initWithFrame:CGRectZero];
[anotherView setOpaque:YES];
[anotherView setClipsToBounds:YES];
[anotherView setHidden:YES];
[anotherView setBackgroundColor:[UIColor clearColor]];
[self setBackView:anotherView];
[anotherView release];
// Backview must be added first!
// DO NOT USE sendSubviewToBack:
[self addSubview:backView];
[self addSubview:contentView];
[self setContentViewMoving:NO];
[self setSelected:NO];
[self setShouldSupportSwiping:YES];
[self setShouldBounce:YES];
[self hideBackView];
}
return self;
}
Please help me, or at least point me in a direction or two!
//////////////////////////
Updated with some new code:
This is inside my RadioCustomCell, a UIView-subclass. It's here the UILabels are drawn
#import "RadioCustomCell.h"
#implementation RadioCustomCell
#synthesize nowTitle,nowArtist,nextTitle,nextArtist,ChannelImage;
// Setting the variables
- (void)setNowTitle:(UILabel *)aLabel {
if (aLabel != nowTitle){
[nowTitle release];
nowTitle = [aLabel retain];
[self setNeedsDisplay];
}
}
- (void)setNowArtist:(UILabel *)aLabel {
if (aLabel != nowArtist){
[nowArtist release];
nowArtist = [aLabel retain];
[self setNeedsDisplay];
}
}
- (void)setNextTitle:(UILabel *)aLabel {
if (aLabel != nextTitle){
[nextTitle release];
nextTitle = [aLabel retain];
[self setNeedsDisplay];
}
}
- (void)setNextArtist:(UILabel *)aLabel {
if (aLabel != nextArtist){
[nextArtist release];
nextArtist = [aLabel retain];
[self setNeedsDisplay];
}
}
- (void)setChannelImage:(UIImage *)aImage {
if (aImage != ChannelImage){
[ChannelImage release];
ChannelImage = [aImage retain];
[self setNeedsDisplay];
}
}
- (void)drawContentView:(CGRect)rect {
CGContextRef context = UIGraphicsGetCurrentContext();
//UIColor * backgroundColour = [UIColor whiteColor];
UIColor *backgroundColour = [[UIColor alloc] initWithPatternImage:[UIImage imageNamed:#"CellBackground.png"]];
[backgroundColour set];
CGContextFillRect(context, rect);
CGRect contentRect = self.contentView.bounds;
CGFloat boundsX = contentRect.origin.x;
[ChannelImage drawInRect:CGRectMake(boundsX+120 ,25, 75, 35)];
nowTitle.enabled = YES;
nowTitle.textAlignment = UITextAlignmentCenter;
nowTitle.font = [UIFont fontWithName:#"HelveticaNeue-Bold" size: 14.0];
nowTitle.textColor = [UIColor blackColor];
nowTitle.backgroundColor = [UIColor clearColor];
//[nowTitle drawTextInRect:CGRectMake(boundsX+6 ,31, 110, 20)];
// Trying to add a subview instead of drawing the text
nowTitle.frame = CGRectMake(boundsX+6 ,31, 110, 20);
[self addSubview:nowTitle];
// I have also tried adding it to super, no effect.
nowArtist.enabled = YES;
nowArtist.textAlignment = UITextAlignmentCenter;
nowArtist.font = [UIFont fontWithName:#"HelveticaNeue" size: 10.0];
nowArtist.textColor = [UIColor blackColor];
nowArtist.backgroundColor = [UIColor clearColor];
[nowArtist drawTextInRect:CGRectMake(boundsX+6 ,46, 110, 15)];
nextTitle.enabled = NO;
nextTitle.textAlignment = UITextAlignmentCenter;
nextTitle.font = [UIFont fontWithName:#"HelveticaNeue-Bold" size: 12.0];
[nextTitle drawTextInRect:CGRectMake(boundsX+200 ,31, 110, 20)];
nextArtist.enabled = NO;
nextArtist.textAlignment = UITextAlignmentCenter;
nextArtist.font = [UIFont fontWithName:#"HelveticaNeue" size: 9.0];
[nextArtist drawTextInRect:CGRectMake(boundsX+200 ,46, 110, 15)];
}
You just forgot to init your UILabel in your very first line of code. :)
At the first look, I can be sure that if you draw the text (and the text is not the label), then there is no UILabel at all in your cell. You can ignore that way.
So, if you don't have the UILabel, how you can get the text. Because your contentView is indeed a RadioTableCellView, then it is not hard. In your class, just public a property called nextArtist. When your button is tapped, look up for the contentView (by calling some superview), cast it to RadioTableCellView then get the nextArtist out
Without writing a bunch of code, here's my best guess.
You're going to need to assign tags to aView and anotherView in your initWithStyle: method. I'm going to assume you have a couple constants: BACK_VIEW_TAG and FRONT_VIEW_TAG.
In your touched: method, walk up the view hierarchy until you find your UITableViewCell.
UIView *currentView = button.superView;
while (![currentView isKindOfClass:UITableViewCell] && currentView != nil) {
currentView = currentView.parent;
}
Get the front view and back view (or which ever you want) from the table cell's contentView using the tags.
if (currentView != nil) {
UITableViewCell *cellView = (UITableViewCell *)currentView)
UIView *cellContentView = cellView.contentView;
UIView *backView = [cellContentView viewWithTag:BACK_VIEW_TAG];
UIView *frontView = [cellContentView viewWithTag:FRONT_VIEW_TAG];
// Get other views from frontView and backView using viewWithTag:.
}
Note that you should be adding views to your UITableViewCell subclass's contentView, not to the UITableViewCell directly. See Programmatically Adding Subviews to a Cell’s Content View for details.