this is what i want to achieve. When user tap on a cell, it expands showing some extra buttons on it.
Any tutorial or sample code in swift?
As i am a newbie, a beginner level and detailed approach will be appreciated.
https://www.dropbox.com/s/1jsrxv6nw03p5gk/profiles.png?dl=0
in this snapshot blue cell is selected one and white cell are normal cells.
Use like this;
Add property
var selectedCellIndexPath: NSIndexPath?
Add your custom Heights
let selectedCellHeight: CGFloat = 88.0
let unselectedCellHeight: CGFloat = 44.0
After add custom height tableview codes
override func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
if selectedCellIndexPath == indexPath {
return selectedCellHeight
}
return unselectedCellHeight
}
Related
I have a tableView and cells. The Cells are loaded from a xib and they have a label with automatic height. I need to narrow one cell if the user taps on it.
I have tried hiding - doesn't work
I have tried removeFromSuperView()- doesn't work
Is there any alternative?
When setting up your tableViewCell store the height anchor you want to update
var yourLabelHeightAnchor: NSLayoutConstraint?
private func setupLayout() {
yourLabelHeightAnchor = yourLabel.heightAnchor.constraint(equalToConstant: 50)
// Deactivate your height anchor as you want first the content to determine the height
yourLabelHeightAnchor?.isActive = false
}
When the user clicks on a cell, notify the tableView that the cell is going to change, and activate the height anchor of your cell.
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
let cell = tableView.dequeueReusableCell(withIdentifier: "YourTableViewCellIdentifier") as? YourCell
self.tableView.beginUpdates()
cell?.yourLabelHeightAnchor?.isActive = true
self.tableView.endUpdates()
}
Did you try to do something like this:
func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
var result: CGFloat
if (indexPath.row==0) {
result = 50 }
else {result = 130}
return result
}
This is just an example where height is changed for the first row. I tested on my application and it gave result like this.
In the iOS App Store, there is a cell for the Description of the app. If the text is too long, the cell has a blue "more" button which expands the cell to fit the entire text. There is the same functionality for the "What's New" section detailing the information of the latest update. I have tried implementing this with some problems.
Note: I am using AutoLayout in my Storyboard.
I have a UITableViewController subclass and a UITableViewCell subclass.
import UIKit
class SystemDetailDescriptionTableViewCell: UITableViewCell {
static let defaultHeight: CGFloat = 44
#IBOutlet weak var descriptionTextView: UITextView!
}
The descriptionTextView is set in AutoLayout to 0 on the top, bottom, left, and right.
Next, we have a UITableViewController subclass. My first thought was to use the heightForRowAtIndexPath method.
override func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
if indexPath.section == 0 {
if self.didExpandDescriptionCell {
if let cell = tableView.dequeueReusableCellWithIdentifier(StoryboardPrototypeCellIdentifiers.descriptionCell) as? SystemDetailDescriptionTableViewCell {
return cell.descriptionTextView.contentSize.height
}
}
return SystemDetailDescriptionTableViewCell.defaultHeight
}
return tableView.rowHeight
}
override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
if indexPath.section == 0 {
self.didExpandDescriptionCell = true
tableView.reloadRowsAtIndexPaths([indexPath], withRowAnimation: .Automatic)
} else {
if let link = self.links?[indexPath.row] {
self.performSegueWithIdentifier(StoryboardSegueIdentifiers.toVideoView, sender: link)
}
}
}
The problem is that the contentSize is not properly sizing to the full length of the text. Instead, it's about 3/4 of the text. I have heard that this method will not work with AutoLayout and instead some trickery needs to be done with the LayoutManager, but those methods returned the exact same results.
Can anybody give me some insight on why this isn't working as expected?
You need to tell the text viewYou should try saying something like:
let newSize = cell.descriptionTextView.sizeThatFits(CGSize(width: cell.descriptionTextView.bounds.size.width, height: CGFloat.max))
cell.descriptionTextViewHeightConstraint.constant = newSize.height
return newSize.height
You need to get the appropriate height for the content, then update the constraint to that new height (if you've set a height constraint), then update the cell height. This code assumes that there is only descriptionTextView in that cell and that it requires zero padding.
I have a static UITableView which has 12 rows and for 11 rows I know what the height needs to be.
I have a UILabel which has dynamic text which sits inside the 12 row, how do i go about making just that one cell to have a dynamic height based on the text in the UILabel.
I have tried the below code inside viewDidLoad, but it didn't work. The rows remained the same height. I have also set the lines = 0 for the UILabel
tableView.estimatedRowHeight = 100.0
tableView.rowHeight = UITableViewAutomaticDimension
Have you tried this?
override func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
if (indexPath.row < 11) {
// Everything starts at 0, so this covers 0-10
// Whatever your cell height is
return <#fixedCellHeight#>
} else {
// This is your 12th cell (indexPath.row 11), as we start counting at 0
return UITableViewAutomaticDimension
}
}
Adding to #Adrian answer , if you are using static Cells , changing one cell to dynamic height , and others as they are you can edit it to this .
override func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
if indexPath.row == 11 {
// This is your 12th cell (indexPath.row 11), as we start counting at 0
return UITableViewAutomaticDimension
} else {
return super.tableView(tableView, heightForRowAtIndexPath: indexPath)
}
}
Try this:
func tableView(tableView: UITableView, estimatedHeightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
return UITableViewAutomaticDimension
}
You can create 2 cell prototype cells for the table view. You give them 2 different id.
And then in your code you override this fonction
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
var cell = UITableViewCell()
if indexPath.row < 12 {
cell = tableView.dequeueReusableCellWithIdentifier("cell", forIndexPath: indexPath)
} else {
cell = tableView.dequeueReusableCellWithIdentifier("cell12", forIndexPath: indexPath)
}
return cell
}
You Can create the dynamic cell with Text height then you can set the static and dynamic cell both in on table view
Here is link to dynamic cell with text How to change cell height dynamically in UITableView static cell
More elegant might be to just implement as suggested:
func tableView(tableView: UITableView, estimatedHeightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
return UITableViewAutomaticDimension
}
Or in Objective C:
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
return UITableViewAutomaticDimension;
}
But rather than the row specific logic, add constraints to your static cell's content in the Storyboard to keep the row heights constant. That way if you move rows or change content you don't need to change any code.
I have UITabelViewController with custom MyUITableViewCell.
MyUITableViewCell class has #IBOutlet MyTextView:UITextView and method:
func getHeight() -> CGFloat
{
let height = (self.MyTextView?.contentSize)!.height
return height
}
I need set high for cell depend textView content size.
In UITabelViewController I set high for TableViewCell:
override func tableView(_tableView: UITableView, estimatedHeightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat
{
let cell = tableView(_tableView, cellForRowAtIndexPath: indexPath)
println(cell.getHeight())
return cell.getHeight()
}
But println show 20.0 for all cells on output, although I see that real cell high in table is different.
Simply check the UITableView's rowHeight method, like this:
tableView.rowHeight
implement heightForRowAtIndexPath:NSIndexPath
I have a table that loads data from a database but the problem is if the text being loaded is too long, it goes off the screen. I'm trying to find a way for the text to go onto the next line and have the cell resize automatically to fit this change. Does anyone know how this is done? The cell has three labels but one of them is allowed to be multi-lined.
EDIT:
I got it to work using auto constraints but how can I resize the table cell so that the actual items fit inside the cell and do not go over the cell boundary?
Set label number of "Lines" to ZERO. And set "Line Breaks" to "Word Wrap"
Adding these two methods along with above code fixed the problem with custom font
tamilRow.textLabel?.numberOfLines=0
tamilRow.textLabel?.lineBreakMode = NSLineBreakMode.ByWordWrapping
func tableView(tableView: UITableView, heightForRowAtIndexPath
indexPath: NSIndexPath) -> CGFloat {
return UITableViewAutomaticDimension
}
func tableView(tableView: UITableView, estimatedHeightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
return UITableViewAutomaticDimension
}
swift 5
let unselectedCellHeight: CGFloat = 130.0
func tableView(_: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
if selectedCellIndexPath == indexPath {
//return selectedCellHeight
return UITableView.automaticDimension
}
return unselectedCellHeight
}