Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Shadows in tableview keep stacking (Swift)

I currently have a list of 70 questions in a tableview. Once a question is solved, the table is updated and shows a checkmark (all works OK).

The issue I am having is with the shadow that I add to each of my cells in the tableview. For some reason they keep stacking when scrolling up and down. Also when putting the device in landscape, the new CGRect is drawn, but the old one is still there (also overlap).

The code:

    override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
            let cell:QuestionCell = tableView.dequeueReusableCellWithIdentifier("cell") as! QuestionCell
let bgColorView = UIView()
        bgColorView.frame = CGRectMake(5, 5, (tableView.frame.width)-10, (cell.frame.height)-10)
        bgColorView.backgroundColor = UIColor.clearColor();
        cell.selectedBackgroundView = bgColorView

        let myBackView=UIView(frame:cell.frame)
        myBackView.frame = CGRectMake(5, 5, (tableView.frame.width)-10, (cell.frame.height)-10)
        myBackView.backgroundColor = UIColor.whiteColor();
        myBackView.layer.masksToBounds = false
        myBackView.clipsToBounds = false
        myBackView.layer.cornerRadius = 3
        myBackView.layer.shadowOffset = CGSizeMake(-1, 1)
        myBackView.layer.shadowRadius = 2
        myBackView.layer.shadowOpacity = 0.4;
        let test:CGRect = myBackView.layer.bounds
        myBackView.layer.shadowPath = UIBezierPath(rect: test).CGPath
        cell.addSubview(myBackView)
        cell.sendSubviewToBack(myBackView)



        if (question.showAfter == "true") {
            cell.accessoryType = UITableViewCellAccessoryType.Checkmark
            //give header of correct question blue color
            cell.backgroundColor = UIColor.lightTextColor()
            cell.headerQuestion.textColor = UIColor(red:0.01, green:0.53, blue:0.82, alpha:1.0)
            }
        else
        {
                cell.accessoryType = UITableViewCellAccessoryType.DisclosureIndicator
                cell.backgroundColor = UIColor.whiteColor()
                cell.headerQuestion.textColor = UIColor(red:0.38, green:0.49, blue:0.55, alpha:1.0)
        }
        return cell

}

overlapping of the shadows at rotating (or scrolling up/down)

I have already tried the following (it was a solution from someone on an objective-C topic):

let tag = 120 
if (cell.tag != 120) {
//the above code here
cell.tag = tag 
}

This solves the shadow issue, BUT when I turn the device to landscape mode it does not redraw. Any ideas? All are welcome

like image 821
SoundShock Avatar asked Feb 16 '26 11:02

SoundShock


2 Answers

Your adding a new shadow every time a cell is reused. You should only add the shadow once at creation time.

Make a subclass of UITableViewCell and do your common customizations (background color, shadow) there. Then register your class with the tableview. Only do stuff that's different per cell in tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell

like image 92
orkoden Avatar answered Feb 18 '26 03:02

orkoden


Try this :

if(cell.viewWithTag(120)==nil)
{
  let myBackView=UIView(frame:cell.frame)
  myBackView.frame = CGRectMake(5, 5, (tableView.frame.width)-10, (cell.frame.height)-10)
  myBackView.backgroundColor = UIColor.whiteColor();
  myBackView.layer.masksToBounds = false
  myBackView.clipsToBounds = false
  myBackView.layer.cornerRadius = 3
  myBackView.layer.shadowOffset = CGSizeMake(-1, 1)
  myBackView.layer.shadowRadius = 2
  myBackView.layer.shadowOpacity = 0.3;
  let test:CGRect = myBackView.layer.bounds
  myBackView.layer.shadowPath = UIBezierPath(rect: test).CGPath

  myBackView.tag = 120;

  cell.addSubview(myBackView)
}
  cell.sendSubviewToBack(myBackView)
like image 35
ShahiM Avatar answered Feb 18 '26 04:02

ShahiM