jycjyc13213213 jycjyc13213213 - 4 months ago 13
iOS Question

Some unwanted UIImageViews disappear while trying to set them as borders of UITableView

What I am trying to do is to replace borders(solid lines by default, of course) by small dots. I want the size of the dots to be 6 by 6.

Making borders invisible is a simple thing to do; I just set separators as 'None' in the attribute inspector.

But there was no field to set images or other objects as separators. To solve this problem, I splitted the dot into up and down pieces, assigned them in UIImageViews, and located the upper one on the bottom and the lower one on the top.

Then in the function tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath), I got the lower image in the first row and the upper image in the last row invisible.

What make the problem is that some unwanted dots disappear, while the dots I intended to change are properly gone away. Furthermore they come back or go away when the table is scrolled. The codes and the screenshot are below.

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cellIdentifier = "reuseIdentifier"
let cell = tableView.dequeueReusableCellWithIdentifier(cellIdentifier, forIndexPath: indexPath) as! MyListTableViewCell

//each element in datas[] has lowercase alphabets, a from q.
let array = datas[indexPath.row]

//data is a UIButton, which is the only component in each row besided those dots
cell.data.setTitle(array, forState: .Normal)

//this is where I typed to handle the unwanted dots on the very top and bottom
if indexPath.row == 0{
cell.lowerHalf.hidden = true
}
else if indexPath.row == datas.count - 1{
cell.upperHalf.hidden = true
}
///////////////////

return cell
}


enter image description here

When I scrolled this one up and down, it changed to this:

enter image description here

What makes the dots which shouldn't disappear go away? Or is their a better way to set images as borders?

+In the way iSashok suggested, things didn't change unfortunately. I applied this code right after the declaration; and then I moved to right before the return statement. Both did not work.

cell.clipsToBound = false

Answer

Your cell is getting reuse so you need to change your if condition of cellForRowAtIndexPath like this

if indexPath.row == 0 {
    cell.lowerHalf.hidden = true
    cell.upperHalf.hidden = false
}
else if indexPath.row == datas.count - 1{
    cell.lowerHalf.hidden = false
    cell.upperHalf.hidden = true
}
else {
    cell.lowerHalf.hidden = false
    cell.upperHalf.hidden = false
}