Hello i'm trying to make drop down menu using tableview and its cells
I use default tableview cell for menu and custom cell for content
custom cell has CollectionView and CollectionView has its cells
drop down and up works fine but it has afterimages like this
this is my code for when menu title cell (default cell) selected
override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
tableView.deselectRow(at: indexPath, animated: true)
if indexPath.row == 0 {
sectionDatas[indexPath.section].isOpended = sectionDatas[indexPath.section].isOpended ? false : true
let sections = IndexSet(integer: indexPath.section)
tableView.reloadSections(sections, with: .none)
}
}
is there any solution to remove that afterimages
Try to set clipToBounds = true for collection view or IsHidden - true/false(for collection view)
Related
I have expandable table view cells, everything works, except animation. I have one cell with UISwitch, when I tap on it, other cells appear but with some view movements, same thing when I hide these cells when I tap on UISwitch.
I'm using insetGroup Table View and these movements make view square instead of round.
I want to keep animation, I tried reloadSections(IndexSet(integer: 0), with: .fade) seems like a solution, but it also reloads my header, but I don't want that.
My cellForRowAt func:
internal func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "RemindersCell", for: indexPath) as! RemindersCell
cell.switchReminder.isOn = remindersAllowed ? true : false
cell.switchReminder.addTarget(self, action: #selector(switchTapped(sender:)), for: .touchUpInside)
return cell
}
My numberOfRowsInSection func:
internal func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return remindersAllowed ? 5 : 1
}
My func for UISwitch:
#objc private func switchTapped(sender: UISwitch) {
if !remindersAllowed {
// Add
remindersAllowed = true
} else {
// Delete
remindersAllowed = false
}
tableView.beginUpdates()
tableView.reloadSections(IndexSet(integer: 0), with: .none)
tableView.endUpdates()
}
Default remindersAllowed is true, when I switch it becomes false and hides cells. I really don't understand what the problem is, any help would be appreciated!
The gif shows this bug when I hide the cells.
you can use activate cell as a tableviewheader
You can try this code to delete system reload section animation and reload section with fade animation:
tableView.reloadData()
tableView.reloadSections(IndexSet(integer: 0), with: .fade)
I want to deselected a table view cell when another cell is selected. Here is the code that's just deselect the cell for second tap:
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
tableView.deselectRow(at: indexPath, animated: true)
}
You don't need to call tableView.deselectRow(), just simply set multipleSelection to false.
With Storyboard
Programmatically
tableView.allowsMultipleSelection = false
Select tableView & go to Attributes Inspector & set selection to single selection.
Programatically (inside viewDidLoad() ) you can set as:
self.tableView.allowsSelection = true
self.tableView.allowsMultipleSelection = false
I created a custom table view cell. I added a black transparent overlay image for when the user presses the cell. It works great, however, when you go back to the tableview the cell is still selected. Here is my code.
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
let selectedCell: PackCell = tableView.cellForRow(at: indexPath)! as! PackCell
selectedCell.highlightImage.isHidden = false
performSegue(withIdentifier: segueIdentifiers[indexPath.row], sender: self)
tableView.deselectRow(at: indexPath, animated: true)
}
Maybe you should also hide your highlightImage when come back?
selectedCell.highlightImage.isHidden = true
So I have a tableview that has a list of items in each cell. Each of these cells contain an image view which, upon being tapped, expands the cell and displays the image for that item. When I scroll down the table view and scroll back up to the cell that was selected, the image is gone. I know this is due to reusing cells but I'm not sure on how to keep the expanded cells image in place while scrolling through other items.
The closest I've come is here:
my table view reuse the selected cells when scroll -- in SWIFT
If someone could lend me a hand that would be awesome. Thanks!
Edit: Adding code snippets - Sorry for the wait.
fileprivate var expandedRowIndex: Int?
// cellForRowAt
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell
{
// CatalogItem row.
let item = self.items[indexPath.row]
let expanded = indexPath.row == self.expandedItemRowIndex
// Return standard catalog item cell.
let reuseID = expanded
? CatalogItemCell.PROTOCELL_EXPANDED.id
: CatalogItemCell.PROTOCELL.id
let cell = tableView.dequeueReusableCell(withIdentifier: reuseID) as! CatalogItemCell
// Reset thumbnail image back to nil. Needed so that images appear
// only in the cell that they belong in.
if indexPath.row == self.expandedRowIndex{
cell.uiImage_Thumbnail.image = nil
}
return cell
}
// didSelectRowAt
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath)
{
tableView.deselectRow(at: indexPath as IndexPath, animated: true)
// Expand row - Get the current cell and show image
self.expandedItemRowIndex = indexPath.row
let item = self.items[indexPath.row]
let currentCell = tableView.cellForRow(at: indexPath)
// Pass both the selected cell and item to the ImageManager
ImageManager.startImageRequest(currentCell: currentCell!, item: item)
if self.expandedRowIndex == indexPath.row
{
// Selected row is already expanded.
return
}
var reloadPaths = [IndexPath]()
// Collapse previously expanded row.
if let previousRowIndex = self.expandedRowIndex
{
reloadPaths.append(IndexPath(row: previousRowIndex, section: 0))
}
// Expand the selected row.
self.expandedRowIndex = indexPath.row
let item = self.items[indexPath.row]
debugPrint(item.description)
reloadPaths.append(IndexPath(row: indexPath.row, section: 0))
tableView.reloadRows(at: reloadPaths as [IndexPath], with: .fade)
}
You can maintain a selectedIndex variable.
In your cellForRow you check whether this call is for selectedCell. If yes, then do the customisation that is required for selected cell.
Also you might want to handle heightForRow, there also check whether the call is for selected cell.
You can maintain an indexPath for selected cell. If there are multiple sections.
No need to prevent it from getting reused.
Hope that helps.
I have a custom tableView cell, When I click on the cell the height of the cell increases (the cell expands):
var selectedIndex:IndexPath?
var isExpanded = false
func didExpandCell(){
self.isExpanded = !isExpanded
self.TableView.reloadRows(at: [selectedIndex!], with: .automatic)
}
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
self.selectedIndex = indexPath
self.didExpandCell()
}
I would like to click the cell a second time and segue to another view controller; is this possible?
self.performSegue(withIdentifier: "details", sender: tableView)
In the func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) method you have to check whether the cell is expanded or not. If it is expanded try calling self.performSegue(withIdentifier: "details", sender: tableView). If it is not then implement the code to expand the cell.
This is a different way other than checking for isExpanded state:In didSelectRowAt, put a condition to check whether selected cell's height is equal to the expanded cell height (For ex., if the expanded cell height is set to 100 static). If YES, then call performSegue
The following is just a pseudocode for the logic:
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
let cell = getCellForIndexPath(indexPath)
if cell.frame.size.height == 100 { // 100 is assumed to be static height for expanded cell. If it is dynamic, compare with normal cell height
performSegue()
} else {
self.didExpandCell()
}
}