UITableView bug? incorrect reusable cells - ios

I have default UITableViewController with 10 row
have UITableViewCell with segment control
If I change segmentController selected index in probably cell : 2, then scroll down this state of segmentController will be in other cells
cells1(selectedIndex: 1 )
cells2(selectedIndex: 1 )
cells3(selectedIndex: 1 )
cells4(selectedIndex: 1 )
cells5(selectedIndex: 1 )
... Select in cell 2 other index (2)
scroll down
and cell 5 have same selected index, but I am not touch them
cells1(selectedIndex: 1 )
cells2(selectedIndex: 2 )
cells3(selectedIndex: 1 )
cells4(selectedIndex: 1 )
cells5(selectedIndex: 2 )
selected 0 row
row 5 is too selected, why?
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return 10
}
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath)
cell.selectionStyle = .none
cell.textLabel?.text = String(indexPath.row)
cell.textLabel?.textColor = .white
return cell
}

UITableView and UICollectionView reuse cells. Which means it only keeps enough cells in memory to show what is on the screen. As you scroll cells off the screen they are fed back in to become the cells that are appearing. You need to properly configure the cells as they re-appear in your override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath)
So in your case you need to set the segmented control's selection in cellForRow every time.
This means you will need a way to keep track of what selection has been made on your segmented controls and call that up to set the segmented control correctly when the cell is being shown again.

Reset selection in prepareForReuse method of UITableViewCell by subclassing it.
override func prepareForReuse() {
}

Related

iOS: can VoiceOver swiping work with custom UITableViewCells?

I'm having trouble with swiping between accessibility elements in a UITableView with VoiceOver on. When in a UITableView, the next/previous element in the table is focused when the user swipes right/left. The element is usually a UITableViewCell.
But I have custom cells that use subviews of the cell as accessibility elements, as in:
cell.isAccessibilityElement = false
cell.accessibilityElements = [element1, element2];
This works perfectly fine within the cell, but there's a problem swiping between cells. If the custom cell is at the bottom of the table view, it fails to focus on the next cell in the table view. Normally, using the cells themselves as accessibility elements, the table view will auto-scroll and focus on the next cell. But after the custom cell, it doesn't auto-scroll, as if there are no more cells in the table view.
See screenshot for clarification. When Label 9 is focused, the user can't swipe right to the next cells, even though there are more cells. They can still scroll though using 3 fingers, and then they'll be able to swipe to the next cells as normal.
This problem happens when using the code below. It uses a storyboard that only has a default UITableView with a default prototype cell with some UILabels added to it. So all the accessibility stuff is done here in the code.
class ViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {
#IBOutlet weak var tableView: UITableView!
let customRow = 5
override func viewDidLoad() {
super.viewDidLoad()
self.tableView.register(UITableViewCell.self, forCellReuseIdentifier: "cell")
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
10
}
func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
if indexPath.row == self.customRow {
return UITableView.automaticDimension
} else {
return 120
}
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell: UITableViewCell
if indexPath.row == self.customRow {
cell = self.tableView.dequeueReusableCell(withIdentifier: "customCell", for: indexPath)
cell.isAccessibilityElement = false
cell.accessibilityElements = cell.contentView.subviews
} else {
cell = self.tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath)
var content = cell.defaultContentConfiguration()
content.text = "row \(indexPath.row)"
cell.contentConfiguration = content
}
return cell
}
}

Load 10 items at a time when scroll to bottom load more items on tableview ios ,swift , Firebase

Loading all the date from the firebase to the tableview at a time but i wanted to load 10 items at a time when i scroll to bottom again 10 items should load to the tableview, below is my code
extension ViewController: UITableViewDataSource {
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return items.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let item = items[indexPath.row]
let cell = tableView.dequeueReusableCell(withIdentifier: "inciCell") as!
IncidentCell
cell.delegate = self
cell.setupCell(with: item, indexPath)
return cell
}
}
In the delegate method cellForRowAtIndexPath you may check something like indexPath.row == items.count - 1.
if it's true, then you get 10 items from your DB and append it to your `items.
then do tableView.reloadData() to update the table view with 10 new items.
You may also make some nice spinner in the bottom (tableFooterView) of the table view. But it could be the next step.
Hope it helps.

UITableViewCells invisible

Requirement :
I have a list of UITableviewCell loaded from a nib that I'm presenting on UITableview. The first time I open the UIViewController all cells are shown correctly and work as expected.
Issue :
If I navigate back to the parent and then open the UIViewController again the UITableviewCell are 'invisible'. I say invisible because with a breakpoint in cellForRowAt I can see that the table view does load all cells and the cells are valid.
Code :
func numberOfSections(in tableView: UITableView) -> Int {
return 1
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return 13
}
func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
return 40
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = (project?.sliderData.sliders[indexPath.row].view)! as UITableViewCell
print(cell.contentView.subviews.count)
if let left = cell.viewWithTag(2) as? UILabel {
left.text = "left"
}
if let middle = cell.viewWithTag(3) as? UILabel {
middle.text = "middle"
}
if let right = cell.viewWithTag(4) as? UILabel {
right.text = "right"
}
return cell
}
Screen Shot Image
Expected observation :
I was thinking that maybe the subviews of the cells get released because I don't have any bindings to them in IB. To test this I'm printing the count of subviews and writing some text to the subview labels. And everything seems to go fine, the cells are loaded and the labels are there but the cells just don't show up.
But then, if I scroll the TableView up and down a little to get some cells updated those cells do appear at the top and bottom of the view as shown in the pic.
You need to call dequeueReusableCell(withIdentifier: "cell") inside your code then will show your table cell. It will reuse cell for your all numbers of row data content.
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "cell") as! UITableViewCell
return cell
}
More Details : How to create uitableview with multiple sections in iOS Swift.
Did not find reason why the tableView behaves the way it does so I solved the issue by dequeueing default cells. The views provided by the slider objects are added as subviews to the dequeued cells. Now the subviews can of course be any UIViews.
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
var cell = tableView.dequeueReusableCell(withIdentifier: "sliderCell")
if cell == nil {
cell = UITableViewCell.init(style: .default, reuseIdentifier: "sliderCell")
}
cell?.addSubview((project?.sliderData.sliders[indexPath.row].view)!)
return cell!
}

Swift TableView Crash: "Missing cell for newly visible row"

Im experiencing a crash when adding new rows to a tableview. In short, the crash log says "Missing cell for newly visible row 3".
Reproduction
1. Add N amount of objects to datasource
2. Manually add the same amount
of cells to tableview
3. Reload and animate using beginUpdates - endUpdates
Known Problem
This crash has already been discussed at question and reported at Apple. Their solution to this problem (not using estimated cell heights) does not work for me as i need 2 different heights for the cells.
My tableview is composed out of 2 different cell classes. Both have their own standard height, configured as such:
func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
if indexPath.row % (straightsetLogs.count + 1) == 0 {
return 44
} else {
return tableView.rowHeight
}
}
func numberOfSections(in tableView: UITableView) -> Int {
return 1
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
// (exerciseSets.count / straightsetLogs.count) = amount of (super)sets
// (straightsetLogs.count + 1) = amount of cells needed for 1 (super)set
return (exerciseSets.count / straightsetLogs.count) * (straightsetLogs.count + 1)
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
// Headercell: 1 headercell for every (super)set
if indexPath.row % (straightsetLogs.count + 1) == 0 {
// Create header cells once for every set
let headerCell = tableView.dequeueReusableCell(withIdentifier: CellID.setHeaderCell, for: indexPath) as! SetHeaderTableViewCell
return configureHeaderCell(headerCell, forIndexPath: indexPath, totalCellsPerSet: straightsetLogs.count + 1)
} else {
// Create picker cells for rest of rows
let pickerCell = tableView.dequeueReusableCell(withIdentifier: CellID.setPickerCell, for: indexPath) as! SetPickerTableViewCell
// Configure according to set and movement
return configurePickerCell(pickerCell, forIndexPath: indexPath, totalCellsPerSet: straightsetLogs.count + 1)
}
}
In storyboard i've configured the tableview itself to have a row height of 260.0 and for the header cell i've checked the custom row height box and set it to 44.0.
Current Status
The code for determining the correct cells for the index paths works and the crash can be solved by removing the height for row at indexPath code. But then i end up with all cells being 260.0 height.
Goal
I need 44.0 height for header cells and 260.0 height for picker cells without experiencing this crash.

Why repeated cells show up in UITableView?

Now why would repeated cells show up in a UITableView ?
As you can see in this GIF, I press on a cell button to do some fade effect, but other cell gets affected too !
https://media.giphy.com/media/xT0BKL5KnCgEjaXm9i/giphy.gif
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int
{
return numberOfCells
}
and the count of cells is always 10
numberOfCells = 10
I always make tableviews and I'm sure of the setup, is this a bug by apple's side?
EDIT :
How the cells get created:
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell
{
let cell = tableView.dequeueReusableCellWithIdentifier("brandCell", forIndexPath:indexPath) as! CustomCell1INF
cell.selectionStyle = UITableViewCellSelectionStyle.None
cell.frame = self.view.frame
return cell
}
This happens because the UITableViewCell are being reused.
You changed the cell when you press the button, you need to keep track of that in your data source model.
In cellForRowAtIndexPath you have to add a condition to check if that button was pressed or not, then you display the appropriate view accordingly.

Resources