Custom problem of scrolling table on cells
I have a programmatically generated table view
var tableView: UITableView = UITableView()
var items = ["1","2","3","4","5","6"," 1","L 2","La 3","La 4","La 5","La 6","L 7","La 8","La 9","La 10","La 11","Lab 12","Lab 13","Lab 14","Lab 15","Lab 16","Lab 17","Lab 18","Lab 19","Lab 20","La 1","L 2","La 3"]
override func viewDidLoad() {
super.viewDidLoad()
tableView.frame = CGRectMake(0, 50, self.view.frame.width,100); //If we give self.view.frame.height It worked because no need to scroll
tableView.delegate = self
tableView.dataSource = self
tableView.estimatedRowHeight = 30
tableView.registerClass(UITableViewCell.self, forCellReuseIdentifier: "Cell")
self.view.addSubview(tableView)
}
Here is the code that has the problem
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
var cell:UITableViewCell = tableView.dequeueReusableCellWithIdentifier("Cell") as! UITableViewCell
if indexPath.row == 2 || indexPath.row == 3 {
var redBtn = UIButton()
redBtn = UIButton(frame: CGRectMake(0, 0, 40, 40))
redBtn.backgroundColor = UIColor.redColor()
redBtn.setTitle("das", forState: UIControlState.Normal)
cell.contentView.addSubview(redBtn)
}else {
cell.textLabel?.textAlignment = NSTextAlignment.Center
cell.textLabel?.text = items[indexPath.row]
}
return cell
}
}
On the 2nd and 3rd lines, I only need to display the button. But when you view the table, it appears in all cells. If you increase the height of the table to see the height, then there is no problem, because there is no scrolling. / p>
I read this question scrolling and redrawing a UITableView from this I found it was due to a cell reuse issue during scrolling. But this solution didn't work for me.
Suitable solutions are welcome. I have given the complete code so you can copy and paste it into your project and get this problem.
THANK YOU CURRENTLY
source to share
When reusing cells, the old values are still preserved, so every time you use dequeueReusableCellWithIdentifier, you need to reset to the default or last values still cached in it. In your specific case, you need to remove the buttons that you create from the cell, or set them hidden in the else statement.
The table covers the implementation of the data source tableView: cellForRowAtIndexPath: Always reset all content when reusing a cell.
The best way to implement this is to create a custom cell with all the components as needed (button, shortcut, etc.) as you are deleting new custom cells you turn the property hidden to true or false as needed.
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
var cell:UITableViewCell = tableView.dequeueReusableCellWithIdentifier("Cell") as! MyCustomCell
if indexPath.row == 2 || indexPath.row == 3 {
redBtn.hidden = false
cell.contentView.addSubview(redBtn)
}else {
redBtn.hidden = true
cell.textLabel?.textAlignment = NSTextAlignment.Center
cell.textLabel?.text = items[indexPath.row]
}
return cell
It is not recommended to program the button on the fly in this case, as you will need to loop through all the sub-items in the cell for each reusable cell to see if it exists in order to decide if you need to create or delete as shown below:
for button:AnyObject in subviews{
if(button .isKindOfClass(UIButton)){
if(button.accessibilityIdentifier == "MyButton"){
println("Button Already Exists")
}else{
println("Create new button")
}
}
}
I hope this helps you
source to share