i want to show a gallery kind of thing with the help of a tableView in my iPad app. 5 images should be shown in portrait mode and 8 in landscape mode. To achieve this functionality i have done the following.
1- I'v taken a tableView and rotated it 90 degrees
2- I again rotated the tableView cell by -90 degrees so that i can place my images in it.
3- I placed some images in my TableView and added it to my scrollView.
4 - i have set frames for my tableView differently for portrait and landscape....
if(UIInterfaceOrientationIsPortrait(self.interfaceOrnetation)){
myTableView.frame = CGRectMake(0,0,768,200);
}
else{
myTableView.frame = CGRectMake(0,0,1024,200);
}
5 - i am also changing the frames with the above lines in didRotateFrominterfaceOrientationmethod.
i am using the following code to load the images in the tableView
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
if (cell==nil) {
NSLog(#"count ");
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewStylePlain reuseIdentifier:cellIdentifier] autorelease];
UILabel *pageNumberLabel = [[UILabel alloc] init];
pageNumberLabel.tag = 10;
pageNumberLabel.frame = CGRectMake(35, -10, 100, 50);
[cell.contentView addSubview:pageNumberLabel];
UIImageView *thumbnailImageView = [[UIImageView alloc] init];
thumbnailImageView.tag = 20;
thumbnailImageView.frame = CGRectMake(10, 35, 140, 160);
[cell.contentView addSubview:thumbnailImageView];
}
id<PageDataSource> pd = [kbDataSource pageAtIndex:indexPath.row];
UILabel *pageLabel = [cell viewWithTag:10];
pageLabel.backgroundColor = [UIColor clearColor];
pageLabel.text = [NSString stringWithFormat:#"Page %d",indexPath.row+1];
pageLabel.textColor = [UIColor whiteColor];
UIImageView *thumbnail = [cell viewWithTag:20];
thumbnail.image = [pd thumbnailImageForPageNumber:indexPath.row+1];
cell.selectionStyle = UITableViewCellSelectionStyleGray;
cell.transform = CGAffineTransformMakeRotation(M_PI/2);
return cell;
}
The frames works just fine for the first time. But, when i rotate the device, the images in the tableView are not getting displayed. I dont know where the problem lies.. Again if i dont changes the frame after rotation, it works fine and all the images are getting displayed... IS there something i am missing in the tableView???
It sounds like your positioning of the images is off. What is the x and y positions of the images? I think you'll find your images are outside the containing view frame when rotating hence you can't see them.
However, you say that you are adding a tableview to a scrollview? you shouldn't do so.
Related
I need to display separator line in iOS 7 same like iOS 6 (as my image 2). I am using below code in CellForRowAtIndexPath:
UIView* separatorLineView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 1)];
separatorLineView.backgroundColor = [UIColor colorWithRed:229/255.0 green:229/255.0 blue:229/255.0 alpha:1.0];
[cell.contentView addSubview:separatorLineView];
UIView *customColorView = [[UIView alloc] init];
customColorView.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:#"cell_highlighted.png"]];
cell.selectedBackgroundView = customColorView;
cell.backgroundColor=[UIColor clearColor];
//Separation style for iOS7
if ([tableView respondsToSelector:#selector(setSeparatorInset:)]) {
[tableView setSeparatorInset:UIEdgeInsetsZero];
}
return cell;
I am getting output as below image:
But my output should like below image for both IOS 6 and 7:
Can anyone please help me how to achieve this? Thanks..
contentView doesn't always have the same size as the cell itself, add separator view on top of the cell view above the content view:
separatorLineView.autoresizingMask = UIViewAutoresizingFlexibleWidth;
[cell insertSubview:separatorLineView aboveSubview:cell.contentView];
UITableView has a property separatorInset. Use that to set the insets of the table view separators to zero to let them span the full width of the screen.
[tableView setSeparatorInset:UIEdgeInsetsZero];
OR
You can set separator inset property in xib also. Change it to custom. And make both left and right values to zero.
I have the following code which draws a separator line and text for a UITableViewCell. It looks fine but when I scroll off screen then back, the separator line is gone but the text is still fine. Any ideas?
static NSString *aProgressIdentifier = #"CustomerCell";
UITableViewCell *aCustomerCell = [iTableView dequeueReusableCellWithIdentifier:aProgressIdentifier];
if (!aCustomerCell) {
aCustomerCell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:aProgressIdentifier] autorelease];
aCustomerCell.contentView.backgroundColor = [UIColor whiteColor];
UIImageView *aLine = [[UIImageView alloc] initWithFrame:CGRectMake(0, 72, 800, 1)];
aLine.backgroundColor = [UIColor colorWithWhite:0.9 alpha:1.0];
[aCustomerCell addSubview:aLine];
[aLine release];
}
CMACustomer *aCustomerObject = aCellObject;
aCustomerCell.textLabel.text = aCustomerObject.customerFullName;
aCustomerCell.detailTextLabel.text = nil;
aCell = aCustomerCell;
Try to add the "aLine" image view as subview of the contentView and not the whole table itself. Probably when the cell is reused and then layoutSubviews is called again, the contentView overlaps (white background) your aLine. Infact consider that iOS default cells have their subviews dynamically redrawn and resized each time they are displayed on screen.
So I would try this:
[aCustomerCell.contentView addSubview:aLine];
If this doesn't work, what can you do is to remove the contentView completely and add your own custom subviews (do this inside the if(!aCustomerCell) and not outside unless you will not get the benefits of the cell re-use):
if (!aCustomerCell) {
aCustomerCell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:aProgressIdentifier] autorelease];
[cell.subviews makeObjectsPerformSelector:#selector(removeFromSuperview)];
UIImageView *aLine = [[UIImageView alloc] initWithFrame:CGRectMake(0, 72, 800, 1)];
aLine.backgroundColor = [UIColor colorWithWhite:0.9 alpha:1.0];
[aCustomerCell.contentView addSubview:aLine];
[aLine release];
}
Finally another check is verify that the cell height is > 72 (it seems a trivial check but its often source of headaches!).
The table view is using a pool of cells, so you can't be sure which one you're getting for any given index path. You can use the cell or the content view, but be sure to add only one of your custom lines per cell.
UIImageView *aLine = (UIImageView *)[cell viewWithTag:64];
if (!aLine) {
// etc.
UIImageView *aLine = [[UIImageView alloc] initWithFrame:CGRectMake(0, 72, 800, 1)];
aLine.tag = 64;
[cell addSubview:aLine];
//
}
// other formatting logic here, you can also hide/show aLine based on biz logic
try adding it to the contentView
[aCustomerCell.contentView addSubview:aLine]
all iphone developers
I am currently developing an iphone application in which I am showing a uitableview in grouped style in one of the segments of the segmented control.
My problem is that I don't know how to show a small image dynamically in the table header view.
Another issue is that i don't know how to show labels and button or any other control in one cell of a particular section and also make them multi line.
If any one have code or example please help me doing this.
This one will replace your header with a view, you can add anything there:
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
}
it can be done by this code:
GRect newFrame = CGRectMake(0.0, 0.0, self.myDetailView.bounds.size.width, 50.0);
UIView *trialHeaderView = [[UIView alloc] initWithFrame:newFrame];
trialHeaderView.backgroundColor = [UIColor clearColor];
UIImageView *myImageView = [[UIImageView alloc]initWithFrame:CGRectMake(15, 10, 35, 35)];
myImageView.image = [UIImage imageNamed:#"r1.jpg"];
myImageView.contentMode = UIViewContentModeScaleToFill;
[trialHeaderView addSubview:myImageView];
myDetailView.tableHeaderView = trialHeaderView;
ok..
I can't get the searchResultsTableView cells to be fully visible when loading with a background image. The cells look quite weak and don't stand out from the background imageview, even when selected. Any suggestions?
- (void)searchDisplayController:(UISearchDisplayController *)controller willShowSearchResultsTableView:(UITableView *)tableView {
for (UIView *view in controller.searchResultsTableView.subviews) {
//if ([view isKindOfClass:[UIImageView class]]) {
[view removeFromSuperview];
//}
}
UIImage *patternImage = [UIImage imageNamed:#"background_newer.png"];
UIImageView * backgroundImageView = [[UIImageView alloc] initWithImage:patternImage];
//backgroundImageView.opaque = NO;
backgroundImageView.alpha = 0.9;
controller.searchResultsTableView.separatorStyle = UITableViewCellSeparatorStyleNone;
controller.searchResultsTableView.backgroundColor = [UIColor clearColor];
[controller.searchResultsTableView addSubview:backgroundImageView];
[controller.searchResultsTableView sendSubviewToBack:backgroundImageView];
controller.searchResultsTableView.rowHeight = 25;
[patternImage release];
[backgroundImageView release];
}
I am not doing anything else than allocating a new UITableViewCell for use (in searchResultsTableView) inside this delegate method:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { .... }
Thanks for any corrections!
(I am on iPhone simulator 3.1.2)
#Jessedc
Thanks for your advice. Haven't tried out your code yet.
I solved this headache (before I saw your reply) by setting hidden=YES on the main tableview behind the searchResultsTableView whenever the searchResultsTableView loads, and hidden=NO when the search is over. The main tableview is set with backgroundColor=[UIColor clearColor] on top of an imageview (loaded with the same image as in my original searchResultsTableView code up there).
This new layout is as previously desired :-).
Are trying to display a static background image with the table text scrolling over the top?
I think you may have your code a little mixed up (possibly).
Customising a table is usually done in a viewWillAppear method. so this code should go there:
UIImage *patternImage = [UIImage imageNamed:#"background_newer.png"];
UIImageView * backgroundImageView = [[UIImageView alloc] initWithImage:patternImage];
//backgroundImageView.opaque = NO;
backgroundImageView.alpha = 0.9;
controller.searchResultsTableView.separatorStyle = UITableViewCellSeparatorStyleNone;
controller.searchResultsTableView.backgroundColor = [UIColor clearColor];
[controller.searchResultsTableView addSubview:backgroundImageView];
[controller.searchResultsTableView sendSubviewToBack:backgroundImageView];
controller.searchResultsTableView.rowHeight = 25;
[patternImage release];
[backgroundImageView release];
Next, in your delegate method searchDisplayController:willShowSearchResultsTableView you are not referring to the tableview object passed in, but you are calling it through the top level (unnecessary perhaps?)
Have a go at moving your table setup code into the viewWillAppear.
Let me know if this helps, or if you can provide more information/code.
It seems to me like you're setting the background of your tableview the hard way. I don't know if it's 100% the problem, but you should set your UITableView background like this:
controller.searchResultsTableView.backgroundColor = [[[UIColor alloc] initWithPatternImage:[UIImage imageNamed:#"background_newer.png"]]autorelease];
I am trying to develop an app with a UIPicker in landscape mode, taking up (almost) the entire width of the screen (with 5 or 6 components). Can you please tell me how to set the size of UIPicker. Thank you very much for your help.
Actually, I resize my pickers for almost every app. I do not like that they take up the entire screen. Here is the method that I am using: (note that I am also rotating the picker to be horizontal)
in viewDidLoad .....
picker = [[UIPickerView alloc] initWithFrame:CGRectZero];
picker.delegate = self;
picker.dataSource = self;
picker.showsSelectionIndicator = NO;
//Resize the picker, rotate it so that it is horizontal and set its position
CGAffineTransform rotate = CGAffineTransformMakeRotation(-1.57);
rotate = CGAffineTransformScale(rotate, .46, 2.25);
CGAffineTransform t0 = CGAffineTransformMakeTranslation(3, 22.5);
picker.transform = CGAffineTransformConcat(rotate,t0);
[self.view addSubview:picker];
[picker release];
Then, I like to add a background image for my view that covers up the grey bars (which are now on the top and bottom) of the UIPicker:
//Create the overlay to superimpose ontop of the picker
//In this case, the image is the size of the screen with a transparent area the size of the //UIPickerView cut out in the middle
UIImageView *uiiv = [[UIImageView alloc]
initWithImage:[UIImage imageNamed:#"problem_bg.png"]];
uiiv.userInteractionEnabled = NO;
uiiv.opaque = YES; //for performance
[self.view addSubview:uiiv];
[uiiv release];
UIPickerView delegate method:
-(UIView *) pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)rowforComponent:(NSInteger)component reusingView:(UIView *)view
{
UIView *viewForRow = [[[UIView alloc] initWithFrame:CGRectMake(0, 0, 100, 280)] autorelease];
UIImageView *img = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"myimage.png"]];
img.frame = CGRectMake(0, 0, 102,280);
img.opaque = YES;
[viewForRow addSubview:img];
[img release];
UILabel *label;
UIFont *font = [ UIFont fontWithName:#"Helvetica" size:20];
label = [[[UILabel alloc] initWithFrame:CGRectMake(10, 20, 270, 100)] autorelease];
label.text = #"I am a label";
label.textAlignment = UITextAlignmentCenter;
label.textColor = [UIColor blackColor];
label.font = font;
label.backgroundColor = [UIColor clearColor];
label.opaque = NO;
[viewForRow addSubview:label];
CGAffineTransform rotate = CGAffineTransformMakeRotation(1.57);
[viewForRow setTransform:rotate];
return viewForRow;
}
This gives a much smaller, horizontal picker with a nice look and feel. I hope this helps someone.
You can edit the size by opening the .xib file in TextEdit and changing the size of the UIPickerView.
You can't. UIPickerView's size is constant.
UPDATE: It turns out you can resize an UIPickerView. The trick is to place it inside another (smaller) UIView, and resize that view. I haven't tried this yet.
UPDATE 2: This method does not resize the UIPickerView, but rather crops it. It might or might not be what you're looking for, but AFAIK, there's no way to truly resize an UIPickerView, and this is as close as it gets. It doesn't look that bad.
UPDATE 3 (Long overdue): As of SDK 3.0, UIPickerView is completely resizeable using initWithFrame or setFrame.
I wrestled with the same issue of resizing the pickerview. After some research and headache here is something you can easily do:
Forget Interface builder! I think you can so much better UI without
the interface builder.
In your controllers overwrite the load view method.
-(void)loadView
{
//create your view
self.view = [[UIView alloc] initWithFrame:CGRectZero];
// create a default sized pickerView
pickerView = [[UIPickerView alloc] initWithFrame:CGRectZero];
pickerView.delegate = self;
pickerView.dataSource = self;
pickerView.showsSelectionIndicator = YES;
// Get its frame
CGRect pickerFrame = pickerView.frame;
// Set it to what ever you like, I use a default screen height times a shrink factor like 0.75 for 3/4 of its original size
pickerFrame.size.width = screenWidth*pickerShrinkFactor;
pickerFrame.size.height = pickerHeight*pickerShrinkFactor;
// You can also set the upper left corner of the picker
pickerFrame.origin.x = 0;
// Set the picker frame to new size and origin
pickerView.frame = pickerFrame;
// Add it to your view
[self.view addSubview:pickerView];
}
Good luck
I'm fairly certain that the UIPicker comes in one size, which you can't change. Would be interested to hear different.
The answer is in:
-(UIView *) pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(UIView *)view {
Since with:
- (NSString*)pickerView:(UIPickerView*)pv titleForRow:(NSInteger)row forComponent:(NSInteger)component
Makes it work (that is: sideways with scaled label) of course
No problem (I just registered). By the way I just realized that when I posted my answer, that the method (UIView *) pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent .... lost the space between row and forComponent. So make sure that you have the delegate correct or else it will not work.