I've currently multiple items in tableView cell .I want to increase the height of cell when text condition is matched like if name = "john" then increase the height of cell without disturbing another cell. I want to achieve this screenshot result
My current code is
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
if tableView == tableOrder {
DispatchQueue.main.asyncAfter(deadline: .now() + 0.3) {
self.tableOrderHeight.constant = self.tableOrder.contentSize.height
//self.tableOrderHeight.constant = (6 * 80)
}
}
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath ) -> UITableViewCell {
guard let cell = tableView.dequeueReusableCell(withIdentifier: String(describing: PriceTVCell.self)) as? PriceTVCell else {return UITableViewCell()}
return cell
}
first get the index of the row you want to increase the height and use the delegate function heightForRowAt.
func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
if indexPath.row == customRow {
return 100.0;//Choose your custom row height
}
}
Related
I am working on a UITableView with a custom row (single row), that I load from a separate xib file. UITableView also has a sticky footer view.
Everything is working fine except that when I tap on the footer view, the UITableView's delegate method tableView(_:didSelectRowAt:) is being called, and that's weird.
The IndexPath that I received inside that method is [0,0]. Now how that is problematic for me? Well I am doing a certain thing when the cell of that tableview is pressed. But under the current scenario, that certain thing is also being done when I press footer view instead of the cell.
The code for UITableView is:
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
guard let cell = tableView.dequeueReusableCell(
withIdentifier: CellIdentifiers.INVITE_CELL_ID,
for: indexPath) as? InviteCell else {
return UITableViewCell()
}
// cell configuration
return cell
}
func tableView(_ tableView: UITableView, viewForFooterInSection section: Int) -> UIView? {
if (contacts.count > 0) {
guard let footer = tableView.dequeueReusableCell(
withIdentifier: CellIdentifiers.INVITE_FOOTER_CELL_ID)
as? InviteFooter else {
return nil
}
footer.delegate = self
return footer
}
return nil
}
func tableView(_ tableView: UITableView, heightForFooterInSection section: Int) -> CGFloat {
if (contacts.count > 0) {
return 90
}
return 0
}
I have JSON object where one key is interface_id and I want to set Cell height accordingly, When I try to set height 0, after that tableView stuck while scrolling.
public func tableView(_ tableView: UITableView, estimatedHeightForRowAt indexPath: IndexPath) -> CGFloat{
if (dataArray[indexPath.row] as AnyObject).value(forKey: "interface_id") as! Int == 1{
return 0
}else{
return UITableViewAutomaticDimension
}
}
Basically, to use self sizing cells, for the UITableView you will set an estimated row height, and also set the rowHeight to a value of UITableViewAutomaticDimension. so changing the Cell height for row your code should be look like that.
func tableView(_ tableView: UITableView, estimatedHeightForRowAt indexPath: IndexPath) -> CGFloat {
return 44.0
}
func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
if (dataArray[indexPath.row] as AnyObject).value(forKey: "interface_id") as! Int == 1{
return 0
}else{
return UITableViewAutomaticDimension
}
}
I have two cells (one dynamic and other static) and I want to set different height for each cell like:
override func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
/*tableView.dequeueReusableCell(withIdentifier: "Cell1", for: indexPath)
tableView.dequeueReusableCell(withIdentifier: "Cell2", for: indexPath)
if cell1 return 100
if cell2 return 20 */
}
It's possible to specific height for each cell not row.
How can I resolve this issue?
For static cell (created either as Xib or in storyboard), you can set the height like this, if you are displaying static cell in first row of your Table View.
func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
if indexPath.row == 0 {
return 120
}
return UITableViewAutomaticDimension
}
To populate the static cell along with dynamic cell, You should do,
override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return datasource.count + 1
}
UITableViewAutomaticDimension
https://www.raywenderlich.com/129059/self-sizing-table-view-cells -
If you set correctly the autolayout for the cell you can use UITableViewAutomaticDimension to have the size without have to specify.
You only need to return a height for each indexPath. There is no need to dequeue a cell here. If you want different prototype cells you will do this in cellForRowAtIndexPath.
You can specify as many sections and rows as you want:
override func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
switch indexPath.section {
case 0:
switch indexPath.row {
case 0:
return 100.0
case 1:
return 20.0
// Add rows here if needed
default:
return UITableViewAutomaticDimension
// Add sections here if needed
default:
default:
return UITableViewAutomaticDimension
}
}
*In, Swift 3.0
override func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
if indexPath.row == 0 {
return 100
} else {
return 200
}
}
or you can set Constraint of your Cell proper and then write this code of your ViewDidLoad Method
yourTblView.estimatedRowHeight = 100
yourTblView.rowHeight = UITableViewAutomaticDimension*
override func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
if indexPath.row == 0 {
return 100
} else {
return 20
}
}
Only those cell are displaying which are fitting on screen. if scrolled to next(offscreen) cell only white screen is displayed. it allows to scroll but only white space is displayed. Even the last row which was half displayed is also remains half only.
extension LiveTVController: UITableViewDataSource,UITableViewDelegate {
func numberOfSections(in tableView: UITableView) -> Int {
return 10
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return 1
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let reuseIdentifier = tableView.tag == 1 ? ProgrammeListCell.reuseIdentifier : StreamListCell.reuseIdentifier
let cell = tableView.dequeueReusableCell(withIdentifier: reuseIdentifier, for: indexPath)
if let cell = cell as? StreamListCell {
cell.programmeList.delegate = self
cell.programmeList.dataSource = self
}
return cell
}
func tableView(_ tableView: UITableView, heightForFooterInSection section: Int) -> CGFloat {
return tableView.tag == 2 ? 10 : 0
}
Now after scrolled down for next rows. there are total 10 sections returned and each section have 1 row.
Specify the height of cell according to cell type.And also place this video view in a table view header instead of using cell for first element.
In my app i have a tableview with a custom cell which contains a collectionview and a button in it.I want to implement expanded functionality in my app.The issue is that when the expand a paticular cell, the collectionview data and button are not being clicked.I found out that when i fixed the table height, my collectionview and buttons are getting clicked but when i calculate the expanded height and assign it to tableview at that time its not being clicked.
Code
func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
let cell = self.tableView.cellForRowAtIndexPath(indexPath) as! YourBudgetTableViewCell
cell.budgetIcon.image = UIImage(named: "ic_your_budget_close");
// cell.collectionView.tag = indexPath.row;
// cell.setCollectionViewDataSourceDelegate(self, forRow: indexPath.row)
cell.selected=true;
isCellSelected = indexPath.row;
self.tableView.beginUpdates();
self.tableView.endUpdates();
}
func tableView(tableView: UITableView, didDeselectRowAtIndexPath indexPath: NSIndexPath) {
let cell = self.tableView.cellForRowAtIndexPath(indexPath) as! YourBudgetTableViewCell
cell.budgetIcon.image = UIImage(named: "ic_your_budget_expand");
isCellSelected = -1;
self.tableView.beginUpdates();
self.tableView.endUpdates();
}
func tableView(tableView: UITableView, willSelectRowAtIndexPath indexPath: NSIndexPath) -> NSIndexPath? {
let selectedCell = tableView.cellForRowAtIndexPath(indexPath)!
if selectedCell.selected {
self.tableView.deselectRowAtIndexPath(indexPath, animated: false);
self.tableView(self.tableView, didDeselectRowAtIndexPath: indexPath)
return nil;
}
return indexPath
}
func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
if isCellSelected == indexPath.row
{
return 200;
}
else
{
return 44;
}
// return 200;
}
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return self.listBudget.count
}
So if i give fixed height say 200 in heightForRowAtIndexPath the click works fine, but if not given fixed click is not working
Check for the height for the expended cell or set alternate background color to cell. You will see that cell does not have that height which is required to display CollectionView. Because of that you are not able to click on the buttons at bottom of cell.