I have this code as following. I am trying to add a subview with background color red to my selected tableView cell. But I am facing two bugs:
1: The height of the tableView Cell is 44, but when I set my subView's height to be 44, it seems to be half of the cell height. I can only make the two views equal when the subView frame height is set to be 88.
2: The subView only shows up when I tab it twice. If I tab it for only once, The sub View doesn't show up.
BTW: I have two tableViews in one viewController, so please look at the tableView inside of "else".
The problem in you code is that you try to add selectedBar view directly to selectedCell and this isn't valid you must add it to contentView of selectedCell
like this
selectedCell.contentView.addSubview(selectedBar)
Also selectedBar is shown when you tap twice because you must add this to the end of didSelectRowAt
tableView.deselectRow(at: indexPath, animated: false)
// Edit -> add tag of 555 when you create the view
add this code in cellForRowAt
if(index == indexpath.row)
{
// add the view here
}
else
{
for i in 0..<selectedCell.contentView.subviews.count
{
let cv = selectedCell.contentView.subviews[i]
if cv.tag == 555
{
cv.removeFromSuperview()
}
}
}
Note: when you select didSelectCellAt update only the index to indexpath.row
and reload the table
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.