Expand Tableviewcell revealing labels at the bottom - ios

When you select a tableviewcell it expands from 44 pixels to 100 pixels and when you deselect it will contract to 44 pixels again.
In the storyboard i've customized my cell to be 100pixels as standard and added 3 labels. one at the top and two at the bottom (the 2 labels at the bottom should not be visible and hide at the bottom).
When i run my app every cell will get the height of 44 pixels. The bottom two labels are visible at the correct x-coordinates but above every other cells.
like this:
in viewDidLoad:
self.expandedIndexPath = [NSIndexPath indexPathForRow:7 inSection:1];
And the methods:
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
// Compares the index path for the current cell to the index path stored in the expanded
// index path variable. If the two match, return a height of 100 points, otherwise return
// a height of 44 points.
if ([indexPath compare:self.expandedIndexPath] == NSOrderedSame) {
return 100.0; // Expanded height
}
return 44.0; // Normal height
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
UILabel *DateName = (UILabel *)[cell viewWithTag:100100];
DateName.text = _objects[indexPath.row];
cell.accessoryType = UITableViewCellAccessoryNone;
return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
[tableView beginUpdates];
if ([indexPath compare:self.expandedIndexPath] == NSOrderedSame) {
self.expandedIndexPath = nil;
} else {
self.expandedIndexPath = indexPath;
}
[tableView endUpdates];
}
How can i hide the labels until the cells are expanded to 100 pixels?

Set the cell to clipToBounds = YES;

Related

Change height of row in UITableView

I have created a simple UITableView with a prototype cell and I have to enlarge the height of row. Unfortunately, I cannot achieve that. I just put the custom size of height for the prototype cell in Interface Builder (Xcode) and put method in ViewController:
-(float)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
return 200.0;
}
And cellForRowAtIndexPath method:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
AlbumCell *cell = [tableView dequeueReusableCellWithIdentifier:#"albumCellID"];
if (cell == nil) {
cell = [[AlbumCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:#"albumCellID"];
}
cell.albumTitle.text = [tableData objectAtIndex:indexPath.row];
return cell;
}
Simulator returns my table with still small height for rows (ca. 40 px). How can I enlarge the height of a row?

uitableview cell does't update cell height after I change a subview's height constraint

i have a custom cell with a button and a blue blank view in it, when the button is tapped, the cell should change the blue view's cell height constraint. But now when the blue view's height is changed, the cell height doesn't change, i want the cell height to change the same amount as the blue view, but now it doesn't.
Here is the constraint.
the result is
what is want is update the cell height to accommodate the blue view.
here is the code.
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return 10;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = #"TestZanTableViewCell";
TestZanTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
[self configureCell:cell forIndexPath:indexPath];
return cell;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
static TestZanTableViewCell *cell;
if (cell == nil) {
cell = [tableView dequeueReusableCellWithIdentifier:#"TestZanTableViewCell"];
}
[self configureCell:cell forIndexPath:indexPath];
NSLog(#"%f", [cell.contentView systemLayoutSizeFittingSize:UILayoutFittingCompressedSize].height + 1.0f);
return [cell.contentView systemLayoutSizeFittingSize:UILayoutFittingCompressedSize].height + 1.0f;
}
- (void)configureCell:(TestZanTableViewCell *)cell forIndexPath:(NSIndexPath *)indexPath {
if (cell.tapped) {
cell.heightOfLikeView.constant = 50;
}
cell.testLabel.text = #"Helloworld";
[cell.zanButton addTarget:self action:#selector(didTapButton:) forControlEvents:UIControlEventTouchUpInside];
}
- (void)didTapButton:(id)sender {
// Cast Sender to UIButton
UIButton *button = (UIButton *)sender;
// Find Point in Superview
CGPoint pointInSuperview = [button.superview convertPoint:button.center toView:self.tableView];
// Infer Index Path
NSIndexPath *indexPath = [self.tableView indexPathForRowAtPoint:pointInSuperview];
TestZanTableViewCell *cell = (TestZanTableViewCell *)[self.tableView cellForRowAtIndexPath:indexPath];
cell.tapped = YES;
[self.tableView reloadRowsAtIndexPaths:#[indexPath] withRowAnimation:UITableViewRowAnimationFade];
[cell.contentView setNeedsUpdateConstraints];
[cell.contentView layoutIfNeeded];
}
You need to refresh the table view and explicitly tell the tableview that, that particular cell has a different height.
You can refresh the table view as follows:
[tableview reloadData]
Or if you want to only refresh the one cell/ specific cells
[tableview reloadRowsAtIndexPaths:#[indexPathOfYourCell] withRowAnimation:UITableViewRowAnimationNone];
You will also need to add the height function to your TableView's delegate so that the table view can load with custom heights for each cell.
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
if (hasTheTallBlueView) {
return largerValue;
}
return regularHeight;
}

How to resize cell height and the row height in ios

I have a UITableView that display a program name and the DJ name. For some programmes, there are no any DJs. So I created a custom UITableViewCell by putting 2 UILabels. Middle of the UITableViewCell displays the programme name and bottom UILabel displays the DJ name. To display both programme and the DJ name I have to set the cell size and UITableViewCell size as 147. But when DJ is not available I want to hide that bottom label and resize the cell and row height to 70. so how should I do this ?
I am checking if this DJ is null or not inside my cellForRowAtIndexPath method.
How I can resize the cell, and row height ?
Thanks for the help.
You need to use the following delegate:
-(CGFloat)tableView:(UITableView*)tableView heightForRowAtIndexPath:(NSIndexPath*) indexPath {
if(...){
return 147.0f; //or whatever
}
else{
return 70.f;
}
}
Add NSIndexPath below #implementation
NSIndexPath *selectedCellIndexPath;
Add Both of these methods.
// And in the implementation file:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
selectedCellIndexPath = indexPath;
// Forces the table view to call heightForRowAtIndexPath
[tableView reloadRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationNone];
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
// Note: Some operations like calling [tableView cellForRowAtIndexPath:indexPath]
// will call heightForRow and thus create a stack overflow
if(selectedCellIndexPath != nil
&& [selectedCellIndexPath compare:indexPath] == NSOrderedSame)
return 338;
return 64;
}
You have 2 types of cell. With height of 147 and 70 pixels.
1) As Anoop Vaidya said you should set cell height by checking "DJ is null or not".
2) To avoid reusing wrong cell template, you should use 2 types of cellIdentifier:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
NSString *CellIdentifier = nil;
if (check DJ is nil or not) {
CellIdentifier = #"CellWithHeight_147";
} else {
CellIdentifier = #"CellWithHeight_70";
}
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:CellIdentifier];
}
...
return cell;
}

UITableViewCell with UIImageView in a few of them

I have a customized UITableViewCell and I've added a UIImageView to it. The thing is, there are times when I want to remove the UIImageView as the data I receive from the server does not have an image associated with it. How do I make the cell layout change such that the other elements move up. Should I set the UIImageView's frame to zero? Using a placeholder image is not a solution.
Here's what my problem looks like:
The row with index 0 does not have an image but there is a space in between the top and the bottom label. Is there a way I can do this using Auto Layout?
Your problem is actually caused by heightForRowAtIndexPath, you have to return the proper value in this method, something like:
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
YourCustomObject *object = [tableDataSource objectAtIndex:indexPath.row];
if(object.hasImage) {
return heightForCellWithImageInside;
}
else {
return heightForCellWithoutImageInside;
}
}
Then, in your cellForRowAtIndexPath just hide the UIImageView if there is no image for it.
You can dynamically change the height of cell.If there is an image,then increase cell height otherwise not.Also set the imageView whenever u get image from server otherwise no need to set imageView.like this:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath: (NSIndexPath *)indexPath
{
static NSString *simpleTableIdentifier = #"ImageCellItem";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:simpleTableIdentifier];
}
if(imageFromserver)
{
//Set your imageView here
}
else
{
// Do nothing
}
return cell;
}
when ever there is no image coming from server hide or remove your image view and assign the frame of image view to your bottom label.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:nil];
if (cell == nil)
{
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:CellIdentifier] ;
}
if(yourimage==nil)
{
bottomlabel.frame=imageview.frame; // you can do something like this, as per your need.this is just example
}
return cell;

Make UILabel Fit Height in a Dynamic Sized UITableViewCell

I have the following setup in my app:
I have a UITableView which has animated height change, when a cell is selected. In the cell is I have a label, which text I want to be on one line with truncation, when the cell is not selected and then when it is selected (the cell height get's bigger) I want the text (UILabel) to be on multiple lines, based on the text.
This is my code at the moment:
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
if(self.selectedIndex != nil && [self.selectedIndex isEqual:indexPath]) {
return kCellHeight * 3.9;
}
return kCellHeight;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
[tableView deselectRowAtIndexPath:indexPath animated:NO];
ProductCell *cell = (ProductCell *) [tableView cellForRowAtIndexPath:indexPath];
if (![indexPath isEqual:self.selectedIndex]) {
cell.addButton.hidden = NO;
cell.descLabel.numberOfLines = 0;
cell.descLabel.lineBreakMode = NSLineBreakByWordWrapping;
[cell.descLabel sizeToFit];
tableView.scrollEnabled = NO;
self.selectedIndex = indexPath;
}else {
self.selectedIndex = nil;
cell.descLabel.lineBreakMode = NSLineBreakByTruncatingTail;
tableView.scrollEnabled = YES;
cell.addButton.hidden = YES;
}
[tableView beginUpdates];
[tableView endUpdates];
}
The first part (when the cell is selected, expanded and the height gets bigger) the text fits as I want, but the second part (when the cell is contracted, and the cell height get back to the normal height) the text does not fit to the new height and is truncated as I want it to, it just stays multi lined. How can I fix this? So when the cell is selected the text gets multilined and when the cell is deselected the text gets back to its normal truncated state?
EDIT:
Override
- (void)tableView:(UITableView *)tableView didDeselectRowAtIndexPath:(NSIndexPath *)indexPath
and add below line in this function
cell.descLabel.numberOfLines = 1;

Resources