help please. I have this code that shows me images in scrollview.:
- (void)viewDidLoad
{
[super viewDidLoad];
NSArray *imgNames = [[NSArray alloc] initWithObjects:#"ip1.jpg", #"ip2.jpg", #"ip3.jpg", #"ip4.jpg", #"ip5.jpg", #"ip6.jpg", #"ip7.jpg", #"ip8.jpg", #"ip9.jpg", #"ip10.jpg",#"ip11.jpg",#"ip12.jpg",#"ip13.jpg",#"ip14.jpg",#"ip15.jpg",#"ip16.jpg",#"ip17.jpg",#"ip18.jpg",#"ip19.jpg",#"ip20.jpg",#"ip21.jpg", nil];
// Setup the array of UIImageViews
NSMutableArray *imgArray = [[NSMutableArray alloc] init];
UIImageView *tempImageView;
for(NSString *name in imgNames) {
tempImageView = [[UIImageView alloc] init];
tempImageView.contentMode = UIViewContentModeScaleAspectFill;
tempImageView.image = [UIImage imageNamed:name];
[imgArray addObject:tempImageView];
}
CGSize pageSize = scrollViewBack.frame.size; // scrollView is an IBOutlet for our UIScrollView
NSUInteger page = 0;
for(UIView *view in imgArray) {
[scrollViewBack addSubview:view];
// This is the important line
view.frame = CGRectMake(pageSize.width * page++ + 40, 0, pageSize.width - 80, pageSize.height);
}
scrollViewBack.contentSize = CGSizeMake(pageSize.width * [imgArray count], pageSize.height);
}
Now, I want a UILabel, that will show me, Image name, when I will scroll. Help me please, I can't implement that. Thanks a lot.
In your second for loop you can acces to the indexes of the objects of imgArray and imgNames, so try this:
int idx = [imgArray indexOfObject:view];
NSString *strName = [imgNames objectAtIndex:idx];
//create the label
label.text=strName;
//add the label in the scrollView
If you want to show the label just when the new image is showed, keep the two arrays as iVars and use UIPageControl to track in wich page of the scrollview you are. (Sample code).
In your loop you could do something like this. Read the apple docs. Search google for how to make a label. Its not hard to learn this stuff if you just try.
for(NSString *name in imgNames) {
// ....
UILabel *label = [[UILabel alloc] initWithFrame:WHEREYOUWANTIT];
[label setText:name];
}
Related
I am using the following code to try and display an image in a UIScrollView.
[self.jarView addSubview:imgView];
It does not work. However, it works when I add the subview to my normal view. My UIScrollView is called jarView and imgView is a UIImageView.
Thanks
This works fine for me:
UIScrollView *scrollview;
scrollview= [[UIScrollView alloc] initWithFrame:CGRectMake(,,,)];// set scroll view size
view = [[UIView alloc] initWithFrame:CGRectMake(y1,x1,y2,x2)]; //set the view size
UIImage *image1 = [UIImage imageNamed:imageName];
image=[[UIImageView alloc]initWithImage:image1];// take image size according to view
[view addSubview:image];
[scrollview addSubview:view];
[view release];
At last define how much scrollview is scroll:
scrollview.contentSize = CGSizeMake(width,height);
Make sure that your scrollView.contentSize property value is set correctly (width and height should be > 0).
Here I am posting my answer. Try it.
You can load all the images to an array. And you can design a view having one image view and try the below code:
array name: examplearray and view name :exampleview
-(void)updateImagesInScrollView
{
int cnt = [examplearray count];
for(int j=0; j< cnt; ++j)
{
NSArray *nibContents = [[NSBundle mainBundle] loadNibNamed:#"exampleview"
owner:self
options:nil];
UIView *myView = [nibContents objectAtIndex:0];
exampleview * rview= (exampleview *)myView;
/* you can get your image from the array and set the image*/
rview.imageview.image = yourimage;
/*adding the view to your scrollview*/
[self.ScrollView addSubview:rview];
}
/* you need to set the content size of the scroll view */
self.ScrollView.contentSize = CGSizeMake(X, self.mHorizontalScrollView.contentSize.height);
}
I am giving you my sample code. In this code i add multiple images on scroll view and this work fine for me:
UIScrollView *scrollview;
- (void)anyFunction
{
scrollview = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 30, 320, 50)];
NSInteger viewcount = [cat_Products count]; // if you have four content your view count is 4..(four)..in my case i fetch it from database
CGFloat y2 = 0;
UIView *view;
UIImageView *imageView;
NSString *imageName;
for(int i = 0; i< viewcount; i++)
{
CGFloat y = i * 64;
y2 = y2 + 64;
view = [[UIView alloc] initWithFrame:CGRectMake(y, 0, y2, 50)];
imageName = [[NSString alloc] initWithFormat:#"c%i", (i + 1)]; // my image name is c1, c2, c3, c4 so i use this
UIImage *image1 = [UIImage imageNamed:imageName];
imageView = [[UIImageView alloc] initWithImage:image1];
[view addSubview:imageView];
[scrollview addSubview:view];
[view release];
[imageView release];
[imageName release];
}
scrollview.contentSize = CGSizeMake(viewcount*64,0); //viewcount*64 because i use image width 64..and second parameter is 0..because i not wants vertical scrolling...
[myView addSubview:scrollview];
[scrollview release];
}
I hope this helps.
I m parsing a json url and showing it in a grid view.The output in the simulator looks like a grid of images similar to imagepicker.on pressing the image it got to navigate to a new view...the code works fine and i am able to receive the data in the console when the image is clicked.the problem is i m not able to navigate to the next view..cud u guys help me out
- (void)viewDidLoad {
[super viewDidLoad];
jsonurl=[NSURL URLWithString:#"http://www.1040communications.net/sheeba/stepheni/iphone/stephen.json"];
jsonData=[[NSString alloc]initWithContentsOfURL:jsonurl];
jsonArray = [jsonData JSONValue];
items = [jsonArray objectForKey:#"items"];
// NSLog(#"hello:%#",items);
story = [[NSMutableArray array]retain];
media1 = [[NSMutableArray array]retain];
for (NSDictionary *item in items )
{
[story addObject:[item objectForKey:#"title"]];
[media1 addObject:[item objectForKey:#"media"]];
}
UIScrollView *view = [[UIScrollView alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
int row = 0;
int column = 0;
for(int i = 0; i < media1.count; i++) {
NSString *mel=[media1 objectAtIndex:i];
NSString *escapedURL = [mel stringByAddingPercentEscapesUsingEncoding:NSASCIIStringEncoding];
UIImage *thumb = [[UIImage alloc]initWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:escapedURL]]];
button = [UIButton buttonWithType:UIButtonTypeCustom];
button.frame = CGRectMake(column*100+24, row*80+10, 64, 64);
[button setImage:thumb forState:UIControlStateNormal];
[button addTarget:self
action:#selector(buttonClicked:)
forControlEvents:UIControlEventTouchUpInside];
button.tag = i;
[view addSubview:button];
if (column == 2) {
column = 0;
row++;
} else {
column++;
}
}
[view setContentSize:CGSizeMake(320, (row+1) * 80 + 10)];
self.view = view;
[view release];
}
- (IBAction)buttonClicked:(id)sender
{
UIImage *boy = [story objectAtIndex:button.tag];
Detailview *detailview=[[Detailview alloc]init];
[detailview initWithItem:boy];
self.detailviewController=detailview;
[self.navigationController pushViewController:self.detailviewController animated:YES];
}
Try this in your buttonClicked:
UIImage *boy = [story objectAtIndex:sender.tag];
You want the tag of sender, not button. button was set to the last one in your list when your buttons were created. sender is the ui object that caused this event.
hi looks like ur using old JSON parser. there is a much better parser for json.
thats JSONKIT.
It has just 2 files(.h and .m) and in just 2 lines of code u can get any data corresponding to the key. u dont hav to iterate through your items array. ur performance rate will definetly incr using JSONKit. if u tryin to use this let me i can post sample codes to parse json contents using jsonkit.
Secondly for the problem u have, u are putting the images in UIScrollview. ur uiscrollview doesnt have navigationcontrol to be pushed to the next view. either remove the scroll view and push from the rootviewcontroller or add navigationcontroller to uiscrollview. guess thats the prob. try it.
all the best.
I have problem with can't set new text in UITextView .I already link up the IBOutlet to textView in interface builder. Here is my code
- (void)viewDidLoad
{
[super viewDidLoad];
self.title = #"Detail";
[self setupTextView];
}
- (void) setupTextView
{
self.textDescription = [[[UITextView alloc] init] autorelease];
NSString *foo = #"blah blah blah";
textDescription.text = foo; //I try this and it didn't work
//here didn't work too.
//textDescription.text = [textDescription.text stringByAppendingString:foo];
/*Here is something for dynamic textview*/
CGRect frame;
frame = textDescription.frame;
frame.size.height = [textDescription contentSize].height;
textDescription.frame = frame;
/**/
}
Thank you for anu help :)
self.textDescription = [[[UITextView alloc] init] autorelease];
I think this is the problem. If textDescription is an IBOutlet you don't need to allocate it. Just remove this allocation.
How can I set a TTStyledTextLabel inside of a UITableView.
Each TTStyledTextLabel contains Some parsed HTML.
Heres what I have I realize its probably completely wrong.
TTStyledTextLabel* label = [[TTStyledTextLabel alloc] autorelease];
cell.textLabel.text = [TTStyledText textFromXHTML:tempString lineBreaks:YES URLs:YES];
App Crashes on launch. I think its because I am setting the .text property with something that is not text. However, I don't know what else to set.
The following code will do what you want. Unfortunately, however, I cannot figure out how to automatically set the height. If memory isn't an issue you could keep a seperate array of TTStyledTextLabels and reference their heights.
in your loadView:
CGRect cgRct2 = CGRectMake(0, 35, 320, 375); //define size and position of view
tblView = [[UITableView alloc] initWithFrame:cgRct2 style:UITableViewStylePlain];
tblView.dataSource = [self constructDataSource];
tblView.delegate = self;
//[tblView reloadData];
[myView addSubview:tblView];
in your class:
-(TTListDataSource *)constructDataSource {
NSLog(#"constructDataSource");
NSMutableArray * namesArray = [[NSMutableArray alloc] init];
//ADD ITEMS
[namesArray addObject:[TTStyledText textFromXHTML:[NSString stringWithString:#"some XHTML"]]];
TTListDataSource * dataSource = [[TTListDataSource alloc] init];
for (int i = 0; i < [namesArray count]; i++) {
TTStyledText * text = [namesArray objectAtIndex:i];
[dataSource.items addObject:[TTTableStyledTextItem itemWithText:text]];
}
[namesArray release];
return dataSource;
}
I have this code
NSArray *wordListArray = [[NSArray alloc] initWithArray:
[[NSString stringWithContentsOfFile:[[NSBundle mainBundle] pathForResource:#”sowpods” ofType:#”txt”]
encoding:NSMacOSRomanStringEncoding error:NULL] componentsSeparatedByString:#”\n”]];
My question is how can I extract this text and update a label in a Scrollview? I just need the user to be able to scroll down and read whats in the text file...
Sounds like what you want to do is something like:
CGRect appFrame = [[[UIApplication sharedApplication] keyWindow]frame];
UIScrollView* scrollView = [[[UIScrollView alloc] initWithFrame:appFrame] autorelease];
CGRect lblFrame = CGRectMake(0.0f, 0.0f, scrollView.frame.size.width, 20.0f);
for (NSString* word in wordListArray)
{
UILabel* lbl = [[[UILabel alloc] initWithFrame:lblFrame]autorelease];
lbl.text = word;
[scrollView addSubview:lbl];
lblFrame.origin.y += 20.0f;
}
scrollView.contentSize = CGSizeMake(lblFrame.size.width, lblFrame.origin.y);
[mainView addSubview:scrollView]; // mainView = up to you
(NB: I haven't tested this, could contain errors, but just want to give you ideas to work with.)