I have designed UITableViewCell in .xib, and its height and width is :
Retrieving from the Xcode ->Show the size inspector.
Width : 358 Height : 562
TableView Delegate:
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
return 800;
}
What I am trying?
In the following method, i m trying to find width and height of the cell, so that i can draw a vertical line in the cell.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath: (NSIndexPath *)indexPath
{
/* FINDING THE WIDTH AND HEIGHT */
my Custom Cell instance say : cell
cell.contentView.bounds.size.width Give me the value -> 320,
cell.contentView.bounds.size.height Gives me the value -> 44,
}
Question?..
How can i get the correct height/width of the custom cell?
Kindly let know if my understanding is wrong?
You need to override -tableView:willDisplayCell:forRowAtIndexPath:. In this method the correct dimensions are already set up by table view.
Related
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..
I'm using a grouped uitableview. I have a lot of sections.
In the first section there is just one UITableViewCell. In this cell, I will show a text.
The problem is that I don't know the number of characters of the text and I would like to change the height of cell to show entire text.
can someone help me?
Thanks
in the UItableViewDelegate method - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath you can return height for specific row. For that you must know the length of the string you want to display in that row.
see this link Get tableView:heightForRowAtIndexPath: to happen after tableView:cellForRowAtIndexPath:?
You will have to use heightForRowAtIndexPath. Within that you will have to do all the calculations that you do in the cell's layoutsubviews or in cellForRowAtIndexPath too. For doing so you will have to figure out how long the text is. You will have to access the text in cellForRowAtIindexPath anyway. So why don't you know how long the text will be?
To determine the size of the actual text you can use NSSTring's sizeWithFont:forWidth:lineBreakMode:.
I have solved the problem applying the code below and creating a property numberOfTextRows.
(CGFloat)tableView:(UITableView *)t heightForRowAtIndexPath:(NSIndexPath *)indexPath
float height = 45.0f;
if (indexPath.section == 0 && indexPath.row == 0) {
CGSize theSize = [self.strResult sizeWithFont:[UIFont systemFontOfSize:12.0f] constrainedToSize:CGSizeMake(265.0f, 9999.0f) lineBreakMode:UILineBreakModeWordWrap];
self.numberOfTextRows = round(theSize.height / 12);
if ((indexPath.section == height) || (self.numberOfTextRows < 2)) {
height = 45;
} else {
height = theSize.height + 16;
}
}
return height;
(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
cell.textLabel.lineBreakMode = UILineBreakModeWordWrap;
cell.textLabel.numberOfLines = self.numberOfTextRows;
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
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;
}
I am building a UITableViewCell. I am using the style UITableViewCellStyleSubtitle for my table view cell. In the textLabel of the cell I have data that can require a wrap. So to accomodate, I want to move down the detailTextLabel so that the wrapped text isn't covered by the text in the detailTextLabel. I am attempting the following:
CGPoint tempPoint = tableViewCell.detailTextLabel.center;
tempPoint.y += 100;
tableViewCell.detailTextLabel.center = tempPoint;
I have tried similar approaches with the frame of the label, but I could not get it to move. When I log the y of the center point before and after, I always see it start as 0 and then it is just 100 afterwards. I am using the %f flag in NSLog to view the value. This all occurs in the function:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
Any help would be great.
I subclassed UITableViewCell and implemented the - (void)layoutSubviews method. I moved the label in that method.