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.
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..
The cell is not indenting according to is level.
I have configured indentation width inside cellForRowAtIndexPath.
- (NSInteger)tableView:(UITableView *)tableView indentationLevelForRowAtIndexPath:(NSIndexPath *)indexPath{
NSInteger theLevel=0;
if ( indexPath.row==1) {
theLevel=5;
}
return theLevel;
}
In the below image I changed frame frame of button and label according to its level.
If you have added subviews to the cell then you need to set the subviews' autoresizing masks so that they are repositioned when the contentView size gets changed.
Please look at this answer.
I create custom cells within my tableview some have images and are tall some are just text. The height of the cells are calculated in heightForRowAtIndexPath, which I beleive is done before cellForRowAtIndexPath is called. I want to place an imageview at the bottom of the cell regardless of heigh, but I am not sure how to get the calculated height from within cellForRowAtIndexPath?
Too late for an answer..
But, like #user216661 pointed out, the problem with taking the height of the Cell or the ContentView is that it returns the cells original height. Incase of rows with Variable height, this is an issue.
A better solution is to get the Rect of the Cell (rectForRowAtIndexPath) and then get the Height from it.
- (UITableViewCell *)tableView:(UITableView *)iTableView cellForRowAtIndexPath:(NSIndexPath *)iIndexPath {
UITableViewCell *aCell = (UITableViewCell *)[iTableView dequeueReusableCellWithIdentifier:aCellIdentifier];
if (aCell == nil) {
CGFloat aHeight = [iTableView rectForRowAtIndexPath:iIndexPath].size.height;
// Use Height as per your requirement.
}
return aCell;
}
You can ask the delegate, but you'll be asking twice since the tableView already asks and sizes the cell accordingly. It's better to find out from the cell itself...
// in cellForRowAtIndexPath:, deque or create UITableViewCell *cell
// this makes the call to heightForRow... and sizes the cell
CGFloat cellHeight = cell.contentView.bounds.size.height;
// alter the imageView y position (assuming the rest of the frame is correct)
CGRect imageFrame = myImageView.frame;
imageFrame.y = cellHeight - imageFrame.size.height; // place the bottom edge against the cell bottom
myImageView.frame = imageFrame;
You are allowed to call heightForRowAtIndexPath yourself! Just pass the indexPath from cellForRowAtIndexPath as an argument and you can know the height of the cell you are setting up.
Assuming you are using a UITableViewController, just use this inside cellForRowAtIndexPath...
float height = [self heightForRowAtIndexPath:indexPath]
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'd like to know the best strategy for adaptive cell heights. The cells first know how high they will be when they are created (they contain some textboxes and images) in the cellForRowAtIndexPath method.
My idea was to store the cell's height in a NSMutableDictionary with a cell identifiying key.
The problem is that the heightForRowAtIndexPath: method is called before the cells are created and only then.
How do you manage that usually?
If you want to resize your cells (that is - to make heightForRowAtIndexPath method get called) you can use empty beginUpdates - endUpdates block:
[table beginUpdates];
[table endUpdates];
That will force UITableView to update its geometry and cells heights will be updated
If you want to adapt you cell's height, use - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath from the UITableViewDelegate.
Store the cell's height into a NSArray for example and get it thanks to the indexPath parameter.
I do the following now: I calculate the height from the string height directly, I use:
-(CGFloat) estimateCellheightFromText:(NSString*) text andFont:(UIFont*)ffont widthConstraint:(CGFloat)width andLineBreakMode:(UILineBreakMode)lbMode{
CGSize stringSize = [text sizeWithFont:ffont constrainedToSize:CGSizeMake(width, 9999) lineBreakMode:lbMode]; return stringSize.height;}