UIScrollView not showing first object - uiscrollview

I currently have a scroll view loading images from an array, here's my code:
icons = [[NSMutableArray alloc]initWithObjects:[UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:#"one" ofType:#"png"]], [UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:#"two" ofType:#"png"]], [UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:#"three" ofType:#"png"]],nil];
int numberOfItems = [icons count];
NSLog(#"Number of objects: %i",numberOfItems);
for (int i = 0; i < numberOfItems; i++) {
if (i <= 3) {
finalFrame = CGRectMake( i*(80), 12, 80, 80);
}
UIButton *iconButton = [UIButton buttonWithType:UIButtonTypeCustom];
iconButton = [UIButton buttonWithType:UIButtonTypeCustom];
iconButton.frame = CGRectMake(0, 0, 57, 57);
iconButton.center = CGPointMake(40, 29);
iconButton.imageView.contentMode = UIViewContentModeScaleToFill;
[iconButton addTarget:self action:#selector(selected:) forControlEvents:UIControlEventTouchUpInside];
iconButton.tag = i;
dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0ul);
dispatch_async(queue, ^{
UIImage *icon = [icons objectAtIndex:i];
dispatch_sync(dispatch_get_main_queue(), ^{
[iconButton setImage:icon forState:UIControlStateNormal];
});
});
[sub addSubview:iconButton];
sub = [[UIView alloc] initWithFrame:finalFrame];
[scrollView addSubview:sub];
}
The NSLog reads 3 objects, but my scroll view is only showing 2 of them, what am I doing wrong?

I think in it is in for loop for it should be like this
(int i = 0; i <= numberOfItems; i++)

Related

TextField Custom for Validate

Hello everyone I'm trying to embed images for each textfield ...
When I go to insert the text into the first textfield, the image also has an impact on the second textfield. I would that images visualizzassero each textfield that the user selects and not directly to all ... Where am I doing wrong?
Thanks to all
-(BOOL)textFieldShouldEndEditing:(UITextField *)textField {
NSString *nomecognome = self.FF_NomeCognomeTexfield.text ;
NSString *user = self.FF_UsernameTextField.text ;
if ([nomecognome length] == 0 || [user length] == 0 ){
FF_NomeCognomeTexfield.leftViewMode = UITextFieldViewModeAlways;
UIImageView* imageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 32, 32)];
imageView.image = [UIImage imageNamed:#"FF_CheckOFF"];
imageView.contentMode = UIViewContentModeScaleAspectFit;
FF_NomeCognomeTexfield.leftView = imageView;
FF_UsernameTextField.leftViewMode = UITextFieldViewModeAlways;
UIImageView* imageView1 = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 32, 32)];
imageView1.image = [UIImage imageNamed:#"FF_CheckOFF"];
imageView1.contentMode = UIViewContentModeScaleAspectFit;
FF_UsernameTextField.leftView = imageView1;
} else {
FF_NomeCognomeTexfield.leftViewMode = UITextFieldViewModeAlways;
UIImageView* imageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 32, 32)];
imageView.image = [UIImage imageNamed:#"FF_CheckON"];
imageView.contentMode = UIViewContentModeScaleAspectFit;
FF_NomeCognomeTexfield.leftView = imageView;
FF_UsernameTextField.leftViewMode = UITextFieldViewModeAlways;
UIImageView* imageView1 = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 32, 32)];
imageView1.image = [UIImage imageNamed:#"FF_CheckON"];
imageView1.contentMode = UIViewContentModeScaleAspectFit;
FF_UsernameTextField.leftView = imageView1;
}
return YES;
}
All textFields trigger this method. You should use the argument 'textField' in an if-statement to determine the identity of the sender.
if( [textField isEqual: FF_NomeCognomeTexfield] == YES){
...
}

Collapse UITable sections

I have a UITable that can collapse on header section click.
I need the header sections to have unique text in them.
I can use cell.textlabel.text = #""; and this works fine, however I want to add more labels to the cell and position these i.e
lbl123 = [[UILabel alloc] initWithFrame:CGRectMake(10, 0, 290, 20)];
lbl123.font = [UIFont fontWithName:#"Trebuchet MS" size:12.0f];
lbl123.textColor = [UIColor blackColor];
lbl123.backgroundColor = [UIColor clearColor];
lbl123.highlightedTextColor = [UIColor whiteColor];
lbl123.opaque = NO;
lbl123.adjustsFontSizeToFitWidth = TRUE;
lbl123.numberOfLines = 0;
lbl123.lineBreakMode = UILineBreakModeWordWrap;
[cell addSubview:lbl123];
lbl123.text = #"test";
but when I add this to the section on "cellforindexpath" It replicates itself on cells further down the table.
any ideas why this is happening??
full code below:-
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
NSString *cellValue = 0;
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:#"MyIdentifier"];
if (cell == nil)
{
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault
reuseIdentifier:#"MyIdentifier"];
}
if (tableView.tag == 2)
{
UILabel *lbl123;
if ([self tableView:tableView canCollapseSection:indexPath.section])
{
cell.textLabel.font = [UIFont fontWithName:#"Trebuchet MS" size:12.0f];
if (!indexPath.row)
{
cell.textLabel.backgroundColor = [UIColor clearColor];
UIView *bgView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 60)];
[bgView setBackgroundColor:[UIColor blackColor]];
[bgView setAlpha:0.3];
[cell setBackgroundView:bgView];
lbl123 = [[UILabel alloc] initWithFrame:CGRectMake(10, 0, 290, 20)];
lbl123.font = [UIFont fontWithName:#"Trebuchet MS" size:12.0f];
lbl123.textColor = [UIColor blackColor];
lbl123.backgroundColor = [UIColor clearColor];
lbl123.highlightedTextColor = [UIColor whiteColor];
lbl123.opaque = NO;
lbl123.adjustsFontSizeToFitWidth = TRUE;
lbl123.numberOfLines = 0;
lbl123.lineBreakMode = UILineBreakModeWordWrap;
[cell addSubview:lbl123];
lbl123.text = #"";
if(indexPath.section == 1)
{
cell.textLabel.font = [UIFont fontWithName:#"Trebuchet MS-Bold" size:15.0f];
cell.textLabel.text = [NSString stringWithFormat:#"My Results Overview:"];
cell.textLabel.textColor = [UIColor whiteColor];
cell.textLabel.numberOfLines = 3;
cell.textLabel.opaque = YES;
UIImageView *av = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 294, 80)];
av.backgroundColor = [UIColor clearColor];
av.opaque = NO;
av.image = [UIImage imageNamed:#"Blue bar 720x115.png"];
cell.backgroundView = av;
}
if(indexPath.section == 2)
{
cell.textLabel.font = [UIFont fontWithName:#"Trebuchet MS-Bold" size:15.0f];
cell.textLabel.text = [NSString stringWithFormat:#"Result1:"];
cell.textLabel.textColor = [UIColor blackColor];
cell.textLabel.numberOfLines = 3;
cell.textLabel.opaque = YES;
UIImageView *av = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 294, 120)];
av.backgroundColor = [UIColor clearColor];
av.opaque = NO;
if ([confidenceCOL isEqualToString:#"Red"])
{
av.image = [UIImage imageNamed:#"redPanel.png"];
}
if ([confidenceCOL isEqualToString:#"Green"])
{
av.image = [UIImage imageNamed:#"greenPanel.png"];
}
if ([confidenceCOL isEqualToString:#"Purple"])
{
av.image = [UIImage imageNamed:#"purplePanel.png"];
}
cell.backgroundView = av;
lbl123.text = #"Test";
}
if(indexPath.section == 3)
{
cell.textLabel.font = [UIFont fontWithName:#"Trebuchet MS-Bold" size:15.0f];
cell.textLabel.text = [NSString stringWithFormat:#"Result2:"];
cell.textLabel.textColor = [UIColor blackColor];
cell.textLabel.numberOfLines = 3;
cell.textLabel.opaque = YES;
UIImageView *av = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 294, 120)];
av.backgroundColor = [UIColor clearColor];
av.opaque = NO;
if ([ambitionCOL isEqualToString:#"Red"])
{
av.image = [UIImage imageNamed:#"redPanel.png"];
}
if ([ambitionCOL isEqualToString:#"Green"])
{
av.image = [UIImage imageNamed:#"greenPanel.png"];
}
if ([ambitionCOL isEqualToString:#"Purple"])
{
av.image = [UIImage imageNamed:#"purplePanel.png"];
}
cell.backgroundView = av;
lbl123.text = #"TestING!";
}
if ([expandedSections containsIndex:indexPath.section]){}
else{}
}
else
{
if(indexPath.section == 2)
{
cellValue = Result1Moretext;
}
if(indexPath.section == 3)
{
cellValue = Result2Moretext;
}
static NSUInteger const k1 = 1;
// Declare references to the subviews which will display the data.
UILabel *lbl1 = nil;
static NSString *kCellID = #"CellID";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:nil];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:kCellID];
lbl1 = [[UILabel alloc] initWithFrame:CGRectMake(10, 0, 290, 300)];
lbl1.font = [UIFont fontWithName:#"Trebuchet MS" size:12.0f];
lbl1.tag = k1;
lbl1.textColor = [UIColor blackColor];
lbl1.backgroundColor = [UIColor clearColor];
lbl1.highlightedTextColor = [UIColor whiteColor];
lbl1.opaque = NO;
lbl1.adjustsFontSizeToFitWidth = TRUE;
lbl1.numberOfLines = 0;
lbl1.lineBreakMode = UILineBreakModeWordWrap;
[cell.contentView addSubview:lbl1];
}else {
lbl1 = (UILabel *)[cell.contentView viewWithTag:k1];
}
cell.accessoryView.tag = indexPath.row;
lbl1.text = [NSString stringWithFormat:#"%#", cellValue];
return cell;
}
}
else
{
}
}
Thanks
Don't forget that cells are reused in UITableViews to keep memory usage low. So what's happening is you are making a cell, and adding the label to it, then after that cell scrolls of the screen it goes into a reuse pool, gets re-added as a new cell further down and still has the label in it.
What you should do is create all labels etc only when you make a new cell (in the cell==nil section, that's where a cell was not dequeued), and set all the labels in the rest of the code.
You can use view tags to find the label views again later.
eg.
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:#"MyIdentifier"];
if (cell == nil)
{
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault
reuseIdentifier:#"MyIdentifier"];
lbl123 = [[UILabel alloc] initWithFrame:CGRectMake(10, 0, 290, 20)];
lbl123.font = [UIFont fontWithName:#"Trebuchet MS" size:12.0f];
lbl123.textColor = [UIColor blackColor];
lbl123.backgroundColor = [UIColor clearColor];
lbl123.highlightedTextColor = [UIColor whiteColor];
lbl123.opaque = NO;
lbl123.adjustsFontSizeToFitWidth = TRUE;
lbl123.numberOfLines = 0;
lbl123.lineBreakMode = UILineBreakModeWordWrap;
[cell addSubview:lbl123];
lbl123.text = #"";
lbl123.tag=123;
} else {
lbl123 = (UILabel *)[cell viewWithTag:123];
}
if(indexPath.section == 2)
{
lbl123.text = #"Test";
}

scrollview stacks images on top of each other -xcode

I have a scrollview with subview as images.. when I made the pictures center to the scrollview they all appear on top of each other as a stack.. I need them to scroll horizontally.. how do I fix that? this is my code so far..
- (void)viewDidLoad
{
[super viewDidLoad];
for (int i = 1; i < 18; i++) {
UIImageView *images = [[UIImageView alloc] initWithImage:[UIImage imageNamed: [NSString stringWithFormat:#"%d.jpg", i]]];
images.frame = CGRectMake((i-1)*320, 0, 320, 330);
[scroller setContentOffset:CGPointMake(0, 0)];
[scroller addSubview:images];
[images setContentMode:UIViewContentModeScaleAspectFill];
[images sizeToFit];
//center image
CGSize boundsSize = scroller.bounds.size;
CGRect frameToCenter = images.frame;
// center horizontally
if (frameToCenter.size.width < boundsSize.width) {
frameToCenter.origin.x = (boundsSize.width - frameToCenter.size.width) / 2;
NSLog(#"%f", frameToCenter.origin.x);
}
else
frameToCenter.origin.x = 0;
// center vertically
if (frameToCenter.size.height < boundsSize.height)
frameToCenter.origin.y = (boundsSize.height - frameToCenter.size.height) / 2;
else
frameToCenter.origin.y = 0;
images.frame = frameToCenter;
scroller.pagingEnabled = NO;
}
[scroller setContentMode:UIViewContentModeScaleAspectFit];
scroller.delegate =self;
scroller.contentSize = CGSizeMake(320*17, 330);
scroller.backgroundColor = [UIColor blackColor];
pageControl.numberOfPages =17;
pageControl.currentPage = 0;
To center the images horizontally just use the code below
float scrollHeight = 0;
for (int col = 0; col < index; ++col)
{
CGRect selectFrame = CGRectMake(0,0,kOvrW,kOvrH);
selectionIndicator = [[UIImageView alloc] initWithFrame:selectFrame];
selectionIndicator.contentMode = UIViewContentModeBottomRight;
[selectionIndicator setImage:[UIImage imageNamed:#"checked"]];
CGRect frame = CGRectMake(5,kOvrPreY+col*(kOvrPreH+kOvrH),kOvrW,kOvrH);
UIView *fr = [[UIView alloc] initWithFrame:frame];
CGRect imgFrame = CGRectMake(5, 4, 68, 55);
UIImageView *imgView = [[UIImageView alloc]initWithFrame:imgFrame];
imgView.image = [UIImage imageNamed:[[subCategories objectAtIndex:col] valueForKey:#"add"]];
UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc]initWithTarget:self action:#selector(tapAction:)];
tapGesture.numberOfTapsRequired = 1;
tapGesture.numberOfTouchesRequired = 1;
[fr setBackgroundColor:[UIColor colorWithPatternImage:[UIImage imageNamed:#"small-thumb"]]];
[fr addGestureRecognizer:tapGesture];
[fr addSubview:imgView];
fr.tag = col;
[subCatScroll addSubview:fr];
[subCatScroll bringSubviewToFront:fr];
scrollHeight += kOvrPreH+kOvrH;
}
subCatScroll.contentSize = CGSizeMake(subCatScroll.frame.size.width, scrollHeight+kOvrPreH);

How to put programmatically iCarousel in View?

I want four ScrollViews like iCarousel's CoverFlow in View programmatically. My Code is
// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad
{
[super viewDidLoad];
//set up data
wrap = YES;
self.items = [NSMutableArray array];
for (int i = 0; i < NUMBER_OF_ITEMS; i++)
{
[items addObject:[NSNumber numberWithInt:i]];
}
carousel1 = [[carousel1 alloc] init];
carousel2 = [[carousel2 alloc] init];
carousel3 = [[carousel3 alloc] init];
carousel4 = [[carousel4 alloc] init];
carousel1.frame = CGRectMake(0, 0, 320, 120);
carousel2.frame = CGRectMake(0, 120, 320, 120);
carousel3.frame = CGRectMake(0, 240, 320, 120);
carousel4.frame = CGRectMake(0, 360, 320, 120);
carousel1.delegate = self;
carousel2.delegate = self;
carousel3.delegate = self;
carousel4.delegate = self;
carousel1.dataSource = self;
carousel2.dataSource = self;
carousel3.dataSource = self;
carousel4.dataSource = self;
[self.view addSubview:carousel1];
[self.view addSubview:carousel2];
[self.view addSubview:carousel3];
[self.view addSubview:carousel4];
//configure carousel
carousel1.type = iCarouselTypeCoverFlow2;
carousel2.type = iCarouselTypeCoverFlow2;
carousel3.type = iCarouselTypeCoverFlow2;
carousel4.type = iCarouselTypeCoverFlow2;
}
but fail with four warnings with out placing the carousels view on the View. Please help me to overcome this problem. Thanks in advance.
Shouldn't you be initialising them with [[iCarousel alloc] init] instead of [[carousel1 alloc] init]?

how set the uiimageview on uiscroll easily?

i use the following code for adding more images on scroll
UIImageView* imageshow=[[UIImageView alloc] initWithFrame:CGRectZero];
NSString* pathright = [[NSBundle mainBundle] pathForResource:#"images0" ofType:#"png"];
imageshow.image = [UIImage imageWithContentsOfFile:pathright];
[imageshow setFrame:CGRectMake(0, 0, 890, 430)];
UIImageView* imageshow1=[[UIImageView alloc] initWithFrame:CGRectZero];
NSString* pathright1 = [[NSBundle mainBundle] pathForResource:#"images1" ofType:#"png"];
imageshow1.image = [UIImage imageWithContentsOfFile:pathright1];
[imageshow1 setFrame:CGRectMake(890, 0, 890, 430)];
UIScrollView* scrollViewright = [[UIScrollView alloc] initWithFrame:CGRectMake(67,169,890, 430)];
[scrollViewright setContentSize:CGSizeMake(890*2,430)];
scrollViewright.pagingEnabled = YES;
[scrollViewright addSubview:imageshow];
[scrollViewright addSubview:imageshow1];
[self.view addSubview:scrollViewright];
but if i have more images,such as i have imagei should add more code ,so can i use loop (for?) to do this function? thanks
Try something like this:
NSUInteger tot = 5;
UIScrollView* scrollViewright = [[UIScrollView alloc] initWithFrame:CGRectMake(67,169,890, 430)];
[scrollViewright setContentSize:CGSizeMake(890*tot,430)];
scrollViewright.pagingEnabled = YES;
for (NSUInteger i = 0; i < tot; ++i) {
UIImageView* imageshow=[[UIImageView alloc] initWithFrame:CGRectZero];
NSString* pathright = [[NSBundle mainBundle] pathForResource:[NSString stringWithFormat:#"images%d", i + 1] ofType:#"png"];
imageshow.image = [UIImage imageWithContentsOfFile:pathright];
[imageshow setFrame:CGRectMake(i * 890, 0, 890 * (i + 1), 430)];
[scrollViewright addSubview:imageshow];
[imageshow release];
}
[self.view addSubview:scrollViewright];

Resources