Create an account

Very important

  • To access the important data of the forums, you must be active in each forum and especially in the leaks and database leaks section, send data and after sending the data and activity, data and important content will be opened and visible for you.
  • You will only see chat messages from people who are at or below your level.
  • More than 500,000 database leaks and millions of account leaks are waiting for you, so access and view with more activity.
  • Many important data are inactive and inaccessible for you, so open them with activity. (This will be done automatically)


Thread Rating:
  • 775 Vote(s) - 3.56 Average
  • 1
  • 2
  • 3
  • 4
  • 5
UITableView set to static cells. Is it possible to hide some of the cells programmatically?

#11
The above answers that hide/show cells, change rowHeight, or mess with Auto layout constraints didn't work for me because of Auto layout issues. The code became intolerable.

For a simple static table, what worked best for me was to:

1. Create an outlet for every cell in the static table
2. Create an array only with the outlets of cells to show
3. Override cellForRowAtIndexPath to return the cell from the array
4. Override numberOfRowsInSection to return the count of the array
5. Implement a method to determine what cells need to be in that array, and call that method whenever needed, and then reloadData.

Here is an example from my table view controller:

@IBOutlet weak var titleCell: UITableViewCell!
@IBOutlet weak var nagCell: UITableViewCell!
@IBOutlet weak var categoryCell: UITableViewCell!

var cellsToShow: [UITableViewCell] = []

override func viewDidLoad() {
super.viewDidLoad()
determinCellsToShow()
}

func determinCellsToShow() {
if detail!.duration.type != nil {
cellsToShow = [titleCell, nagCell, categoryCell]
}
else {
cellsToShow = [titleCell, categoryCell]
}
}

override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
return cellsToShow[indexPath.row]
}

override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return cellsToShow.count
}

Reply

#12
Turns out, you can hide and show cells in a static UITableView - and with animation. And it is not that hard to accomplish.

[Demo project](

[To see links please register here]

)

[Demo project video]()

The gist:

1. `Use tableView:heightForRowAtIndexPath:` to specify cell heights dynamically based on some state.
2. When the state changes animate cells showing/hiding by calling `tableView.beginUpdates();tableView.endUpdates()`
3. Do not call `tableView.cellForRowAtIndexPath:` inside `tableView:heightForRowAtIndexPath:`. Use cached indexPaths to differentiate the cells.
4. Do not hide cells. Set "Clip Subviews" property in Xcode instead.
5. Use Custom cells (not Plain etc) to get a nice hiding animation. Also, handle Auto Layout correctly for the case when cell height == 0.

[More info in my blog (Russian language)](

[To see links please register here]

)
Reply

#13
Answer in **swift**:

Add the following method in your TableViewController:

override func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
return indexPathOfCellYouWantToHide == indexPath ? 0 : super.tableView(tableView, heightForRowAtIndexPath: indexPath)
}

if the tableView tries to draw the cell you wish to hide, then it won't display it because its height will be set to 0pt thanks to the method above, everything else stays unaltered.

Please note that ````indexPathOfCellYouWantToHide```` can be changed at anytime :)
Reply

#14
In > Swift 2.2, I've combined few answers here.

Make an outlet from storyboard to link to your staticCell.

@IBOutlet weak var updateStaticCell: UITableViewCell!

override func viewDidLoad() {
...
updateStaticCell.hidden = true
}

override func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
if indexPath.row == 0 {
return 0
} else {
return super.tableView(tableView, heightForRowAtIndexPath: indexPath)
}
}

I want to hide my first cell so I set the height to 0 as described above.
Reply

#15
1. In the designer, create an outlet for the cell(s) you want to hide. For example you want to hide 'cellOne', so in viewDidLoad() do this

cellOneOutlet.hidden = true

now override the below method, check which cell status is hidden and return height 0 for those cell(s). This is one of many ways you can hide any cell in static tableView in swift.



override func tableView(tableView: UITableView, heightForRowAtIndexPathindexPath: NSIndexPath) -> CGFloat
{

let tableViewCell = super.tableView(tableView,cellForRowAtIndexPath: indexPath)

if tableViewCell.hidden == true
{
return 0
}
else{
return super.tableView(tableView, heightForRowAtIndexPath: indexPath)
}

}
Reply

#16
To hide static cells in UITable:

1. **Add this method**:

In your UITableView controller delegate class:

*Objective-C:*

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell* cell = [super tableView:tableView cellForRowAtIndexPath:indexPath];

if(cell == self.cellYouWantToHide)
return 0; //set the hidden cell's height to 0

return [super tableView:tableView heightForRowAtIndexPath:indexPath];
}

*Swift:*

override func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
var cell = super.tableView(tableView, cellForRowAtIndexPath: indexPath)

if cell == self.cellYouWantToHide {
return 0
}

return super.tableView(tableView, heightForRowAtIndexPath: indexPath)
}

This method will get called for each cell in the UITable. Once it calls it for the cell you want to hide, we set its height to 0. We identify the target cell by creating an outlet for it:

2. **In the designer, create an outlet for the cell(s) you want to hide.** The outlet for one such cell is called "cellYouWantToHide" above.
3. **Check "Clip Subviews" in the IB for the cells you want to hide.** The cells you are hiding need to have ClipToBounds = YES. Otherwise the text will pile up in the UITableView.
Reply

#17
In addition to @Saleh Masum solution:

If you get **auto-layout errors**, you can just remove the constraints from the `tableViewCell.contentView`

**Swift 3:**

override func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
let tableViewCell = super.tableView(tableView, cellForRowAt: indexPath)

if tableViewCell.isHidden == true
{
tableViewCell.contentView.removeConstraints(tableViewCell.contentView.constraints)
return 0
}
else{
return super.tableView(tableView, heightForRowAt: indexPath)
}

}

This **solution depends on the flow of your app**. If you want to show/hide the cell in the same view controller instance this may not be the best choice, because it **removes the constraints**.
Reply

#18
# Simple iOS 11 & IB/Storyboard Compatible Method
For iOS 11, I found that a modified version of [Mohamed Saleh's answer][answer] worked best, with some improvements based on Apple's documentation. It animates nicely, avoids any ugly hacks or hardcoded values, and **uses row heights already set in Interface Builder**.

The basic concept is to set the row height to 0 for any hidden rows. Then use [`tableView.performBatchUpdates`][batchupdates] to trigger an animation that works consistently.

### Set the cell heights

override func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
if indexPath == indexPathOfHiddenCell {
if cellIsHidden {
return 0
}
}
// Calling super will use the height set in your storyboard, avoiding hardcoded values
return super.tableView(tableView, heightForRowAt: indexPath)
}

You'll want to make sure `cellIsHidden` and `indexPathOfHiddenCell` are set appropriately to your use case. For my code they're properties on my table view controller.

### Toggling the cell
In whatever method controls the visibility (likely a button action or `didSelectRow`), toggle the cellIsHidden state, inside a `performBatchUpdates` block:

tableView.performBatchUpdates({
// Use self to capture for block
self.cellIsHidden = !self.cellIsHidden
}, completion: nil)

[Apple recommends `performBatchUpdates` over `beginUpdates`/`endUpdates`][beginupdates] whenever possible.

[answer]:

[To see links please register here]

[batchupdates]:

[To see links please register here]

[beginupdates]:

[To see links please register here]

Reply

#19
Swift 4:

override func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
var height = super.tableView(tableView, heightForRowAt: indexPath)
if (indexPath.row == HIDDENROW) {
height = 0.0
}
return height
}
Reply

#20
As per Justas's answer, but for Swift 4:

override func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
let cell = super.tableView(tableView, cellForRowAt: indexPath)

if cell == self.cellYouWantToHide {
return 0
}

return super.tableView(tableView, heightForRowAt: indexPath)
}
Reply



Forum Jump:


Users browsing this thread:
1 Guest(s)

©0Day  2016 - 2023 | All Rights Reserved.  Made with    for the community. Connected through