Following is the image i have taken from an app from the App Store:
I have created an app that is using StoryBoard with custom tableview cell. The data is loaded in it from a web service. This is what i want:
1) How can i change the size of the uitableviewcell according to the image? For example if one image is 640*500, then the UITableViewCell will change its size accordingly. If the size if the image is 640*1000, then the cell should change its size accordingly.
2) How can i add the grey coloured border around the tableview cells just like in the image above?
3) How can i add a shadow dropping from the UITableViewCell just like in the image above?
Thanks!
Currently i have a partial answer to your question. Part 2 of the question that is the colored border can be solved by the following code:
[cell.contentView.layer setBorderColor:[UIColor colorWithRed:222/255.0f green:222/255.0f blue:222/255.0f alpha:1.0f].CGColor];
[cell.contentView.layer setBorderWidth:5.0f];
Part-1
The height of the tableviewcell is determined using
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
/*
Get the image size
CGFloat width = myImage.size.width;
CGFloat height = myImage.size.height;
use this to return the height of your cell.
*/
}
Part-2 and Part-3
Use..
- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath {
//This will give you access to the cell being rendered and any customization you want can be done here... You can use the earlier answer for this part.. I havent tried it but should work..
}
Hope this helps..
Related
I am trying to set a custom background for the rows of a grouped UITableView and I have absolutely no idea how to achieve that.
The table design I am trying to apply on my table looks like this:
And I have sliced it into 3 cell types: top, middle and bottom. How can I apply the custom background for each of the table's cells?
You can set UIImageView as cell backgroundView for first, middle and last cell in function:
-(void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath
Check good (and simple) example at:
http://www.planet1107.net/blog/tips-tutorials/custom-grouped-cell-tutorial/
In cellForRowAtIndexPath, check the type of current row and then assign the corresponding background to it.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
// Create cell
// ...
// Configure cell
switch (cellType) {
case TOP:
cell.backgroundView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"top.png"]];
break;
case MIDDLE:
...
}
}
Ray Wenderlich has a very good tutorial on how to do this with Core Graphics: http://www.raywenderlich.com/2033/core-graphics-101-lines-rectangles-and-gradients
This tutorial will give you exactly what you're looking for.
http://cocoawithlove.com/2009/04/easy-custom-uitableview-drawing.html
UITableViewCell has an property backgroundView. Set this value in your tableView:cellForRowAtIndexPath:-method.
https://developer.apple.com/library/ios/#documentation/uikit/reference/UITableViewCell_Class/Reference/Reference.html
Instead of assigning separate image for each, Create one custom cell and assign background image as cell has above. And Crop above image other than inner cells assign that one to UITableView's background view (Plain table view) i.e you dont need to create grouped table view.
I can subclass the cell and resolve the problem, but I'd like to know why this doesn't work.
I set an image in the cell, then in this method, I move the image to the right:
- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath
{
[cell.imageView setFrame:CGRectMake(cell.contentView.bounds.size.width/2 - cell.imageView.frame.size.width/2 + 100,
cell.contentView.bounds.size.height/2 - cell.imageView.frame.size.height/2,
cell.imageView.frame.size.width,
cell.imageView.frame.size.height)];
NSLog(#"center: %#", NSStringFromCGPoint(cell.imageView.center));
NSLog(#"frame: %#", NSStringFromCGRect(cell.imageView.frame));
}
Notice the +100 I added to ensure it's over to the right.
When I check the console, it indicates this:
center: {250, 57.5}
frame: {{150, 7.5}, {200, 100}}
However the output is this:
The image is still aligned to the left side. Can this be changed, and if so, how?
if you go to the header file of UITableViewCell than there you will find...
> // Content. These properties provide direct access to the internal
> label and image views used by the table view cell. These should be
> used instead of the content properties below.
> #property(nonatomic,readonly,retain) UIImageView *imageView
> __OSX_AVAILABLE_STARTING(__MAC_NA,__IPHONE_3_0); // default is nil. image view will be created if necessary.
so as you can see it says readOnly property to the image view... I think this explains your inability to move the imageView.
You can not move the image view. Solution is or to subclass and do your own drawing in drawRect, or to add a subview to the cell.contentView.
Edit:
If an image is set, it appears on the left side of the cell, before
any label. UITableViewCell creates the image-view object when you
create the cell.
From UITableViewCell Class Reference
In my apps, i have created table view and displayed the images and the labels in the table view. I have downloaded the image and set the image height into the another class and i have passed the image height value using delegates. In my problem is, heightForRowAtIndexPath method is call very earlier and it never calls again. So how i set the cell height depends on the image height?
//appDelegate.imageHeight - Image height delegate value.
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
NSLog(the height of the image is --------------------------%f, appDelegate.imageHeight);
In Console:
the height of the image is --------------------------0.000000
the height of the image is --------------------------0.000000
the height of the image is --------------------------0.000000
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
NSLog(the height of the image is --------------------------%f, appDelegate.imageHeight);
In Console:
the height of the image is --------------------------98.567039
the height of the image is --------------------------300.000000
the height of the image is --------------------------232.288406
}
Is any possible to call the heightforRowAtIndexPath method inside the cellForRowAt method. Because i have set the cell frame size and height depends on the image height. So how can i set the cell height depends on the image height?, please guide me.
Thanks!
heightForRowAtIndexPath method is called before cellForRowAtIndexPath, so you need to know height of images you are displaying in your cells. Maintain the dictionary or array that contains height of images which get passed to this class and use it to set the height of cell accordingly
here are some suggestions UITableView flexible/dynamic heightForRowAtIndexPath
Since upgraded XCode to Version 3.2.3 with iPhone SDK 4 my code doesn't work anymore.
I have a default cell with style UITableViewCellStyleSubtitle and want to set the textAlignment of textLabel and detailTextLabel to center, but nothing happens.
Same code used before now not working anymore. On UITableViewCellStyleDefault center alignment still works.
Does anyone know how to solve this? I don't want to use a custom cell only in fact of this.
The reason why text isn't centered is because the label is only as wide as the text. You can confirm this by setting the background color of the text label:
- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath {
cell.textLabel.backgroundColor = [UIColor orangeColor];
}
Setting the cell width manually also doesn't seem to have an effect. So you should really add your own subviews or create your own subclass of UITableViewCell.
The docs for UITableViewCellStyleSubtitle even say:
A style for a cell with a left-aligned
label across the top and a
left-aligned label below it in smaller
gray text. The iPod application uses
cells in this style.
here is a two-part solution to alter the label width after rendering, thus allowing the centering to happen:
-- (void) updateCenteringForTextLabelInCell:(UITableViewCell*)cell {
UILabel* label = cell.textLabel;
label.frame = CGRectMake(label.frame.origin.x, label.frame.origin.y, cell.contentView.frame.size.width, label.frame.size.height);
}
-- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
// do your usual
// call the helper method
[self performSelector:#selector(updateCenteringForTextLabelInCell:) withObject:cell afterDelay:0.05];
}
ok finally the Apple development team answered my bug report from 23rd June 2010:
"Please try setting the text alignment
in layoutSubviews after calling
super."
In the iPhone SDK, is there a way to customize the width of the cells in a tableview that is having the style of UITableViewStyleGrouped?
The problem is that I am using an custom background image (width of 290px) for the cell.backgroundView but apparently that gets stretched. I want to be able to set the custom width of this cell so the background image doesn't stretch.
Just resize the tableView as per your requirement ..........either from Nib or programmatically.....
size of cell depends on the width of table.
because there is not such any method in IPhoneSDK
-(CGFloat)tableView:(UITableView *)tableView widthForRowAtIndexPath:(NSIndexPath *)indexPath
{
// width
return 300;
}
Can you not call the delegate method
-(CGFloat)tableView:(UITableView *)tableView widthForRowAtIndexPath:(NSIndexPath *)indexPath
{
// width
return 300;
}