Creating a smaller UIPickerView for use in table - iphone

I'm trying to create a smaller UIPickerView for use in a table and I can't work out how to make the actual control resize correctly.
What I'm trying to achieve is something like the following:
I am creating my picker in the cellForRowAtIndexPath: method:
cell = [tableView dequeueReusableCellWithIdentifier:#"Cell" forIndexPath:indexPath];
cell.accessoryType = UITableViewCellAccessoryNone;
cell.contentView.clipsToBounds = YES;
UIPickerView *myPickerView = [[UIPickerView alloc] initWithFrame:
CGRectMake(0, 0, cell.frame.size.width, 85)];
myPickerView.delegate = self;
myPickerView.showsSelectionIndicator = YES;
[myPickerView selectRow:3 inComponent:0 animated:YES];
[cell.contentView addSubview:myPickerView];
No matter what I set the height to, I get the same size UIPickerView. The first image is with 85 for the height. The second image is with 200 for the height.
The one thing I can do is change the cell height, but if I do this and set the height of the UIPickerView to the same value as the cell height, the picker isn't centred in the cell and the actual touch control point is near the bottom of the cell. The final image in this post illustrates this...
Result of "CGRectMake(0, 0, cell.frame.size.width, 85)"
Result of "CGRectMake(0, 0, cell.frame.size.width, 200)"
Picker Frame and Cell Height set to 100

Try to shrink the "height" of the UIPickerView
myPickerView.transform = CGAffineTransformMakeScale(0.7, 0.7);

Related

How to add image on UITableView cells?

How can I add one image on several cells of UITableView?
I add an image to one top cell, like this:
But after scroll to bottom and top I see this:
Second and third cell has a z-index more that first cell.
How I can do this?
Thanks..
UIImageView *imgView = [[UIImageView alloc]initWithFrame:CGRectMake(3,2, 20, 25)];
imgView.image=[UIImage imageNamed:#"img.png"];
[cell addSubview:imv];
neither you can directly add image to cell
-(UITableViewCell*)tableView:(UITableView*)tableView cellForRowAtIndexPath:(NSIndexPath*)indexPath
{
static NSString* CellIdentifier = #"Cell";
UITableViewCell* cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil)
cell = [[[UITableViewCell alloc] UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
cell.textLabel.text = #"I'm a UITableViewCell!";
cell.imageView.image = [UIImage imageNamed:#"image.png"];
return cell;
}
try this code....
and adjust the image size....
Are you adding the image to the CellView or to the TableView?
You can try the following function in which you have to specify the filename, position of the picture, size and view to which you want to add it.
- (void)createImage:(NSString *)name
inX:(NSInteger)x
inY:(NSInteger)y
width:(NSInteger)w
height:(NSInteger)h
view:(UIView *)v{
UIImage *background = [UIImage imageNamed:name];
CGRect rect = CGRectMake(x, y, w, h);
UIImageView *yourImageView = [[UIImageView alloc] initWithFrame:rect];
yourImageView.image = background;
[v addSubview:yourImageView];
}
if you want to add the image on the tableView, get the scroll view instance from the tableview using tableviewobj.scrollview and then add the image as subview on the scrollview and make the image come front using bringsubviewtofront method.
Adjust your cell height as a image size.
If you want to add image on cell then I would recommend you to make custom cell with imageView .
You can add image as subview to table
UIImageView *imgView = [[UIImageView alloc]initWithFrame:CGRectMake(3,2, 20, 25)];
imgView.image=[UIImage imageNamed:#"img.png"];
[UITableview addSubview:imv];
or
you can increase the height of cell
I did it.
Add UIImageView (let's say img1) object to UITableView (table1)
Set img1.Layer.ZPosition to 1 that the image was above cells in the table1
For Headers in sections of table set img1.Layer.ZPosition to 2 that a header was above an image
Thanks all for help!
You wouldn't add it to a single cell, but to the tableview itself. Then - in a tableview subclass - you would make sure that in the layoutSubviews the image view stays on top and the position gets adjusted to stay in a fixed place. Or next to a specific cell if you want that.
Use addSubview on tableview directly.
let imageView = UIImageView(frame: CGRectMake(0, 0, width, height))
imageView.image = UIImage(named: "My Image")
self.tableView.addSubview(imageView)

UITableViewStyleGrouped with a right angle

When I create table view with
myTableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, 320, 400) style:UITableViewStyleGrouped];
table view has rounded corners, but I wanna have right angle (so table view cell should look like rectangles, but table view still should have many sections).
The question is: how can I create table view with many sections and right angles?
In the TableView datasource. In the cellForRowAtIndexPath you can set the background of the cell to be blank...
cell.backgroundView = [[UIView alloc] initWithFrame:CGRectZero()];
Then you can put an image or rect or whatever in the back of the cell.
You could even create a new UIView and put that in place of the backgroundView.
Then you can put whatever you want in it, images, labels, etc... and they will go behind the content of the cell.
The rounded corners of the cell in grouped style is just an image so can be replaced by you.
set image as a background image to cell and give the backgroundcolor of UITableView to clearColor and set image like bellow..
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
///write your code..
UIImageView *img = [[[UIImageView alloc]initWithFrame:CGRectMake(0, 0, 320, 45)] autorelease]; // set frame as your cell frame
img.image = [UIImage imageNamed:#"yourImageName"];
cell.backgroundView = img;
////write your code
}
For Sections you can code a delegate method like
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
// Return the number of sections.
return 1;//how many sections you want you can write a value after return type
}
Best way I found to make the corners right angled is to set the background view to nil and then put a view in it.
[cell setBackgroundView:nil];
cell.backgroundView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 44)];

Is there a way to customize the size of the UITableViewCell's backgroundView?

I'm trying to assign a custom backgroundView to a UITableViewCell. The table is Grouped.
I'm a bit of a trailblazer so instead of using the typical width of a background view, 302 pixels, I'm trying to use a 300 px wide image.
Here's my code:
- (void)tableView:(UITableView*)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath {
// a 300 pixel wide image
UIImage *backgroundImage = [UIImage imageNamed:#"tableViewCellBackgroundTop"];
UIImageView *theBackgroundView = [[UIImageView alloc] initWithImage:backgroundImage];
theBackgroundView.frame = CGRectMake(10, 0, 300, 49);
cell.backgroundView = theBackgroundView;
}
If I run this in the simulator and measure with xScope, the cell still appears to be 302 pixels wide. What gives? Is there some other property I need to set to make the framework respect the frame I've assigned to the background view, or were table view cells not intended to be customized in this fashion?
You can create a UIView instance and make theBackgroundView a subview of it, and then set cell.backgroundView to the created view:
UIView *view = [[UIView alloc] initWithFrame:CGRectMake(10, 0, 300, 49)];
[view addSubview:theBackgroundView];
cell.backgroundView = view;

How to adjust position of textLabel, detailTextLabel and switch in tableview?

I have textLabel, detailTextLabel, UISwitch in tableview cell.
I want to adjust y position of them. How can I do that?
I already adjusted height of cell.
UISwitch *switchView = [[UISwitch alloc] initWithFrame:CGRectMake(200, 10, 50, 30);
cell.accessoryView = switchView;
Above code doesn't work for switch. It always shows at the same position.
You can custom your own UITableViewCell say MyTableViewCell and adjust controls in layoutSubviews.
eg:
- (void)layoutSubviews {
[super layoutSubviews];
// Adjust size
self.imageView.bounds = CGRectMake(0,0,44,44);
// Adjust position
self.textLabel.frame = CGRectMake(self.textLabel.frame.origin.x,
self.textLabel.frame.origin.y - 10,
self.textLabel.frame.size.width,
self.textLabel.frame.size.height);
}
You will need to add it as subView to cell 's content view like this:
[[cell contentView] addSubView:switchView]; and switchView's x,y should be relative to tableViewCell.

Changing frame of the tableView causing trouble in my app

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.