UIPickerView does not show added subview in UIView - ios

at the end of that example I'm adding subviews to the UIView, but the subs arent showing. The UIView is showing (made it for test blue and it was there) If I return rowLabel or rowImage seperatly they are showing. but not embedded in the UIView. Any Ideas?
-(UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(UIView *)view {
NSDictionary *oneResultDict = [_postArray objectAtIndex:row];
NSData *tmpImageData = [NSData dataWithContentsOfURL:[NSURL URLWithString:[oneResultDict objectForKey:#"thumbnail"]]];
UIImage *tmpImage = [UIImage imageWithData:tmpImageData];
UIImageView *rowImage = [[UIImageView alloc] initWithImage:tmpImage];
rowImage.frame = CGRectMake(0, 0, 60, 60);
UILabel *rowLabel = [[UILabel alloc]initWithFrame:CGRectMake(80, 0, 200, 60)];
rowLabel.text = [oneResultDict objectForKey:#"title"];
UIView *rowView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 160)];
[rowView insertSubview:rowImage atIndex:0];
[rowView insertSubview:rowLabel atIndex:1];
return rowView;
}

you just to have to return your view, you don't need to do [rowView insertSubview:rowImage atIndex:0]. It is better to do like
(if component == 0)
[rowView addsubview:rowImage]
(if component == 1)
[rowView addsubview:rowLabel]
return rowView

From insertSubview:atIndex: Apple Docs:
The index in the array of the subviews property at which to insert the
view. Subview indices start at 0 and cannot be greater than the
number of subviews.
In other words: You don't have any subviews yet, so yo can't insert a new subview.
Try using addSubview: instead:
[rowView addSubview:rowImage];
[rowView addSubview:rowLabel];

Related

Place Label Under iCarousel Image ios

I am using Nick Lockwood's iCarousel class to display a series of images stored in an array using the method
- (UIView *)carousel:(iCarousel *)carousel viewForItemAtIndex:(NSInteger)index reusingView:(UIView *)view
{
//create new view if no view is available for recycling
if (view == nil)
{
view = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 100.0f, 100.0f)];
((UIImageView *)view).image = [images objectAtIndex:index];
view.contentMode = UIViewContentModeScaleAspectFit;
}
}
I would now like to display text stored in a similar array below(and centered) each image much like movie titles are placed under thumbnails in the imDB app. Any help would be immensely appreciated. Thanks in advance!
You can add UILabel to view i.e,
- (UIView *)carousel:(iCarousel *)carousel viewForItemAtIndex:(NSInteger)index reusingView:(UIView *)view
{
//create new view if no view is available for recycling
if (view == nil)
{
view = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 100.0f, 100.0f)];
((UIImageView *)view).image = [images objectAtIndex:index];
view.contentMode = UIViewContentModeScaleAspectFit;
UILabel *lbl = [[UILabel alloc] initWithFrame:CGRectMake(0, 70, 100.0f, 30.0f)];
lbl.text = [titlesArray objectAtIndex:index]; // Here you can add titles to label
/* Here you can code the EFX. */
[view addSubview:lbl];
}
}

How to position a image in customized section header so that it appears correctly in both the orientations?

I have a customized section header, in viewForHeaderInSection, I have a view and a button on that so it will be like clicking the section header some action will be performed.Now I need an image in the section header so that it changes on clicking the section header.So I added a image view to the view in viewForHeaderInSection but the image position is not correct,it is different in landscape and portrait.I need the image in the right corner of the section header.I need to do it immediately.
Please help.
Thanks
-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
/* Create custom view to display section header... */
UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.tableView.frame.size.width, 18)];
UIButton *btn= [[UIButton alloc] initWithFrame:CGRectMake(10, 5, self.tableView.frame.size.width, 18)];
[view addSubview:btn];
UIImageView *imageView = [[UIImageView alloc]initWithFrame:CGRectMake(275.0f, 1.5f, 8.0f, 18.0f)];
UIImage *image = [UIImage imageNamed:#"Selection-Indicator.png"];
[imageView setImage:image];
[view addSubview:imageView];
return view;
}
oky try this hope this helps u
-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
/* Create custom view to display section header... */
UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.tableView.frame.size.width, 18)];
UIButton *btn= [[UIButton alloc] initWithFrame:CGRectMake(10, 5, self.tableView.frame.size.width, 18)];
[view addSubview:btn];
UIImageView *imageView = [[UIImageView alloc]initWithFrame: CGRectMake(view.bounds.size.width - 20, 1.5f, 8.0f, 18.0f); //set the offset from right
UIImage *image = [UIImage imageNamed:#"Selection-Indicator.png"];
[imageView setImage:image];
[view addSubview:imageView];
return view;
}
and in rotation handling method u need to reload or update the UI
for example
- (void)didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation
{
//so u dont want to reload the tableview then u can reload the sections
// [self.tableVIew reloadData]; //just try this (comment)
//try this
NSArray *cells = [self.tableVIew visibleCells];
NSIndexPath *indexPath = [self.tableVIew indexPathForCell:(UITableViewCell *)[cells lastObject]];
NSIndexSet *set = [NSIndexSet indexSetWithIndexesInRange:NSMakeRange(0, totalSectionCount)]; //try by placing the all the section count
[self.tableVIew reloadSections:set withRowAnimation:UITableViewRowAnimationAutomatic];
}
You should have something like this:
UIImageView *imgView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"yourImage.png"]];
[imgView setFrame:CGRectMake(parentView.frame.size.width-50, 5, 30, 30)];
[parentView addSubview:imgView];
The sizes in CGRectMake are only as an example, but make sure your UIImageView is always related to you parentView.frame.size.width... that should do it!
EDIT: After your code, you should replace:
UIImageView *imageView = [[UIImageView alloc]initWithFrame:CGRectMake(275.0f, 1.5f, 8.0f, 18.0f)];
with
UIImageView *imageView = [[UIImageView alloc]initWithFrame:CGRectMake(view.frame.size.width-15, 1.5f, 8.0f, 18.0f)];
play with the -15 to put the image where you wish...
i hope it helps!

UICollectionView with Custom UICollectionViewCell

I have a UICollectionView with a custom cell. I had this working completely till I tried to create the collection view programmatically rather than from the storyboard. I did this because I was not able to change the frame size of the collection view for different screen sizes. So I tried to create a collection view programmatically. My problem is my custom cells are not working.
For my custom cells I draw a circle in the view, user cornerradius on the view of the cell, have a UILabel and a UIImageView.
I draw the circle and the perform the cornerradius thing in the drawRect: of the custom cell. The rest i.e: put image in ImageView and text in UILabel, I do it in the collectionView datasource method.
The problem I am having is I don't get the cornerradius thing working although weirdly enough I can draw a circle on the view. second problem is I don't get the image in the image view and no text in the label
Here is my code. I create the collection view in ViewDidload: method.
[layout setSectionInset:UIEdgeInsetsMake(24, 10, 24, 10)];
[layout setScrollDirection:UICollectionViewScrollDirectionHorizontal];
[layout setMinimumLineSpacing:15];
[layout setMinimumInteritemSpacing:10];
self.collectionView = [[UICollectionView alloc]initWithFrame:rect collectionViewLayout:layout];
self.collectionView.delegate=self;
self.collectionView.dataSource=self;
[self.collectionView registerClass:[customCell class] forCellWithReuseIdentifier:#"cell"];
self.collectionView.backgroundColor= [UIColor blackColor];
self.searchBar.delegate = self;
self.locations = [[NSArray alloc]init];
self.location = [[NSString alloc]init];
[self.view addSubview:self.collectionView];
Here is the drawRect: for my customCell.m
- (void)drawRect:(CGRect)rect
{
UIBezierPath *circularpath = [[UIBezierPath alloc]init];
CGRect Rect = CGRectMake(6, 20, 130, 130);//338
CGPoint mypoint = CGPointMake(Rect.origin.x + (Rect.size.width / 2), Rect.origin.y + (Rect.size.height / 2));
NSLog(#"Circle center point::%f, %f", mypoint.x, mypoint.y);
circularpath = [UIBezierPath bezierPathWithOvalInRect:Rect];
circularpath.lineWidth = 3.0;
[[UIColor whiteColor]setStroke];
UIImage *ori = [UIImage imageNamed:#"drogba.jpg"];
UIImage *image = [[UIImage alloc]initWithCGImage:ori.CGImage scale:1.45 orientation:UIImageOrientationUp];
[[UIColor colorWithPatternImage:image]setFill];
NSLog(#"Circular path:%#", circularpath);
//this works
[circularpath stroke];
//this does not
self.viewForBaselineLayout.layer.cornerRadius = 8.0f;
}
and here is my datasource method
customCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:#"cell" forIndexPath:indexPath];
UIImage *image = [[UIImage alloc]init];
if([self.allInfo count]>0){
image = [self getImageForLocation:[self.allInfo objectAtIndex:indexPath.item]];
NSDictionary *dict = [[NSDictionary alloc]initWithDictionary:[self.allInfo objectAtIndex:indexPath.item]];
NSDictionary *city = [dict objectForKey:#"city"];
NSString *name = [[NSString alloc]initWithString:[city objectForKey:#"name"]];
cell.locationLabel.text = name;
cell.imageView.image = [self getImageForSky:dict];
cell.viewForBaselineLayout.backgroundColor = [UIColor colorWithPatternImage:image];
cell.tempLabel.text = [self getTempForLocation:dict];
}
NSLog(#"image description:%f", image.size.height);
count =1;
return cell;
I don't think it is a problem with my data because all I changed is creating Collection view programmatically rather than using storyboard.
EDIT::
I had to alloc init the image view and the labels for the custom cell and then add them as subview. now 3 of my 4 problems are solved. I still cannot get the corner radius to work for me. I want a slightly curved border for my cell
So firstly, because I removed the cell from the storyboard, I had to add the views to the cell's view as subview. Secondly, to get the rounded corners, I had to also set masksToBounds to YES All this had to be done in the initWithFrame: method of the custom cell. Here the code snippet
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
self.imageView = [[UIImageView alloc]init];
self.imageView.frame = CGRectMake(26, 27, 90, 90);
self.tempLabel = [[UILabel alloc]initWithFrame:CGRectMake(26, 125, 90, 21)];
self.locationLabel = [[UILabel alloc]initWithFrame:CGRectMake(11, 156, 121, 21)];
self.tempLabel.textAlignment = NSTextAlignmentCenter;
self.tempLabel.textColor = [UIColor whiteColor];
self.locationLabel.textAlignment = NSTextAlignmentCenter;
self.locationLabel.textColor = [UIColor whiteColor];
[self.viewForBaselineLayout addSubview:self.imageView];
[self.viewForBaselineLayout addSubview:self.tempLabel];
[self.viewForBaselineLayout addSubview:self.locationLabel];
self.viewForBaselineLayout.layer.masksToBounds = YES;
self.viewForBaselineLayout.layer.cornerRadius = 8.0f;
}
return self;
}
You have init method in Your custom cell
- (id)initWithFrame:(CGRect)frame
change corner radius in this method.
P.S. Works for me.

Use UITableView instead of UIPickerView to set UITextField text

I would like to use a UITextField to enter text into a UITextField instead of UIPickerView using the UITextField .tag
The view is quite complex and consists of several views which I will explain.
htmlContainerScrollView, contains multiple
- axisContainerScrollView, contains multiple
- itemField
UITableView - used to pass text into itemField
So with that in mind, this is how I set up my htmlContainerScrollView. I have added comments to explaine what I am trying to achive.
- (void) displayViews {
// add scrollview, This view is ued to hold all of the axis (vertical scrolling only)
htmlContainerScrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0.0, 44.0, self.view.frame.size.width, 309.0)];
//TODO: replace this array with a dynamic implementation of the axis images. Will need someone to design the images for me
imagesArray = [NSArray arrayWithObjects:[UIImage imageNamed:#"LPA.png"], [UIImage imageNamed:#"LPB.png"], [UIImage imageNamed:#"LPC.png"], [UIImage imageNamed:#"LPD.png"], [UIImage imageNamed:#"LPA.png"], [UIImage imageNamed:#"LPB.png"], [UIImage imageNamed:#"LPC.png"], [UIImage imageNamed:#"LPD.png"], [UIImage imageNamed:#"LPA.png"], [UIImage imageNamed:#"LPB.png"], [UIImage imageNamed:#"LPC.png"], [UIImage imageNamed:#"LPD.png"], [UIImage imageNamed:#"LPA.png"], [UIImage imageNamed:#"LPB.png"], [UIImage imageNamed:#"LPC.png"], [UIImage imageNamed:#"LPD.png"], [UIImage imageNamed:#"LPA.png"], [UIImage imageNamed:#"LPB.png"], [UIImage imageNamed:#"LPC.png"], [UIImage imageNamed:#"LPD.png"], [UIImage imageNamed:#"LPA.png"], [UIImage imageNamed:#"LPB.png"], [UIImage imageNamed:#"LPC.png"], [UIImage imageNamed:#"LPD.png"], nil];
// Loop creates each axis
tagNumber = 1; // init the tagNumver starting from 1
for(int i = 0; i< axisNo; i++) {
CGFloat y = i * 77; // places each axis 77pxl below the previous
int itemsForRow = [itemsArray[i] intValue]; // get the current number of items for this axis
int scrollWidth = (itemsForRow * 40)+4; // calculate scroll width using the umber of items for this axis * by the size of each item.textfield
// create axis scroll view (horizontal scrolling only)
UIScrollView *axisContainerScrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0.0, y,self.view.frame.size.width, 77.0)]; //device view width etc.
axisContainerScrollView.contentSize = CGSizeMake(scrollWidth, 77.0); // axis.view scrollable width
axisContainerScrollView.backgroundColor = [UIColor whiteColor];
[htmlContainerScrollView addSubview:axisContainerScrollView];
// make sure the view goes right to the edge of the screen.
UIView *view = [[UIView alloc] init];
view.frame = CGRectMake(0.0, 0.0, scrollWidth+10, 77.0);
//grey header for each cell, if the total number of items exceeds the width of the device view then make the header the correct size
if(scrollWidth > self.view.frame.size.width) {
topBorder = [[UIView alloc] initWithFrame:CGRectMake(0.0, 0.0, scrollWidth, 18.0)];
topBorder.backgroundColor = [UIColor colorWithRed:colorController.fbRed/255.0 green:colorController.fbGreen/255.0 blue:colorController.fbBlue/255.0 alpha:1.0];
} else {
topBorder = [[UIView alloc] initWithFrame:CGRectMake(0.0, 0.0, self.view.frame.size.width, 18.0)];
topBorder.backgroundColor = [UIColor colorWithRed:colorController.fbRed/255.0 green:colorController.fbGreen/255.0 blue:colorController.fbBlue/255.0 alpha:1.0];
}
[axisContainerScrollView addSubview:topBorder];
[axisContainerScrollView addSubview:view];
int itemsCount = [itemsArray[i] intValue];
// add axis image to each scrollview (i.e. a, b, c, d, e, f, g etc.)
UIImageView *currentAxisImage = [[UIImageView alloc] initWithFrame:CGRectMake(0.0, y, 18.0, 18.0)];
currentAxisImage.image = imagesArray[i];
[htmlContainerScrollView insertSubview:currentAxisImage aboveSubview:view];
// loop creates each item for current axis
for (int i = 0; i < itemsCount; i++) {
// create header for itemField
itemHeaderLabel = [[UILabel alloc] initWithFrame:CGRectMake((i*40)+2, 20, 40, 15)];
itemHeaderLabel.backgroundColor = [UIColor colorWithRed:colorController.grRed/255.0 green:colorController.grGreen/255.0 blue:colorController.grBlue/255.0 alpha:1.0];
[itemHeaderLabel setTextAlignment:NSTextAlignmentCenter];
itemHeaderLabel.font = [UIFont fontWithName:#"Helvetica" size:14];
itemHeaderLabel.textColor = [UIColor whiteColor];
NSString *strFromInt = [NSString stringWithFormat:#"%d",i+1]; // start from 1 not 0
itemHeaderLabel.text = strFromInt;
[view addSubview:itemHeaderLabel];
// itemField is the UITextField I would like to add text too from UITableViewCell selections
itemField = [[UITextField alloc] initWithFrame:CGRectMake((i*40)+2, 35, 40, 40)];
itemField.delegate = self; // set delegate so you can use UITextField delegate methods
itemField.textColor = [UIColor blackColor];
[itemField setTextAlignment:NSTextAlignmentCenter];
itemField.font = [UIFont fontWithName:#"Helvetica" size:20];
itemField.backgroundColor=[UIColor whiteColor];
itemField.layer.borderColor = [[UIColor colorWithRed:colorController.grRed/255.0 green:colorController.grGreen/255.0 blue:colorController.grBlue/255.0 alpha:1.0] CGColor];
itemField.layer.borderWidth = 0.5f;
[view addSubview:itemField];
itemField.tag = tagNumber; // set tag
tagNumber ++;
[columnArrayOfTextFields addObject:itemField]; // array of items textfields.. not using this currently but might become usefull in the future. (legacey code)
}
[rowArrayOfTextFields addObject:columnArrayOfTextFields]; // array of arrays.. not using this currently but might become usefull in the future. (legacey code)
}
// exit loop, set first reasponder to the first itemField.
[itemField viewWithTag:1];
[itemField becomeFirstResponder];
// add the whole scrollview to the mainview.
htmlContainerScrollView.contentSize = CGSizeMake(self.view.frame.size.width, 77 *axisNo);
[self.view addSubview:htmlContainerScrollView];
}
So at this point I have created the view and assigned each itemField a unique tag value. Now I will show you my didSelectRowAtIndexPath which is not complete, I think this is where I should set the text using the UITextFieldDelegates but I am not sure how.
- (void) tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *selectedCell = [tableView cellForRowAtIndexPath:indexPath];
itemField.text = selectedCell.textLabel.text; // dose not work.. dosnt call UITextfield delegates or anything
[tableView deselectRowAtIndexPath:indexPath animated:YES]; //turns the UITableViewCell button as apposed to a single press fielf
}
and finally these are my UITextFieldDelegates.
- (void)textFieldDidBeginEditing:(UITextField *)textField {
itemField.text = textField.text;
}
-(BOOL)textFieldShouldBeginEditing:(UITextField *)textField {
return NO; // Hide keyboard so that you can use the UITableView selections to populate the UITextfeild itemField
}
-(BOOL)textFieldShouldReturn:(UITextField*)textField;
{
currentSelected.text = textField.text;
NSInteger nextTag = currentSelected.tag + 1;
// Set next responder
UIResponder* nextResponder = [textField.superview viewWithTag:nextTag];
if (nextResponder) {
// Found next responder, so set it.
[nextResponder becomeFirstResponder];
} else {
// probably dont need this if I am not showing the UIkeyboard
[textField resignFirstResponder];
}
return NO; // We do not want UITextField to insert line-breaks.
}
You should create a custom tableview cell which has a uitextfield in it. This way you can easily set its delegate to self in cellForRowAtIndexPath like this:
- (UITableViewCell *)tableView:(UITableView *)view cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
MyCellWithTextField* cell = .. //create cell
cell.textField.delegate = self;
return cell;
}
If you don't want to put it in tableview, fix this:
[itemField viewWithTag:1];
[itemField becomeFirstResponder];
should be:
itemField = [view viewWithTag:1];
[itemField becomeFirstResponder];

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!

Resources