Dominic Baker Dominic Baker - 3 months ago 34
Swift Question

Remove right shadow of UITableViewCell on initial load

I am trying to create a card type layout and have used many answers here to get to my current solution. I am using Swift 2 and XCode 7.3.1.

First App Load



After pull to refresh



The UITableViewCell code is:

func addShadow() {
cardView.layer.shadowPath = UIBezierPath(rect: cardView.bounds).CGPath
cardView.layer.shadowOpacity = 0.7
cardView.layer.shadowOffset = CGSizeZero
cardView.layer.shadowRadius = 2
cardView.layer.masksToBounds = false
}

override func layoutSubviews() {
addShadow()
}


Why does the cell only look correct after a pull to refresh and have the extra shadow to the right beforehand?

EDIT:

I'm added the TableViewController as well

class BusinessTableViewController: PFQueryTableViewController {

override func viewDidLoad() {
super.viewDidLoad()
navigationController!.navigationBar.translucent = false
navigationController?.toolbar.translucent = false
configureTable()
// Do any additional setup after loading the view.
}

override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}

func configureTable() {
tableView.tableFooterView = UIView(frame: CGRectZero)
tableView.backgroundColor = Utility.tableBackgroundColor
}

override func queryForTable() -> PFQuery {
let query = PFQuery(className: "Business")
query.cachePolicy = .NetworkElseCache
query.orderByAscending("createdAt")
return query
}

override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath, object: PFObject?) -> PFTableViewCell? {
let cell = tableView.dequeueReusableCellWithIdentifier("businessCell", forIndexPath: indexPath) as! BusinessCell
cell.layoutIfNeeded()

let business = object as! Business
cell.nameLabel.text = business.name
var categoryString = ""

for cat in business.categories {
categoryString += cat

if cat != business.categories.last {
categoryString += ", "
}
}

// Image setup
if cell.mainImageView.layer.cornerRadius != 3 {
cell.mainImageView.layer.cornerRadius = 3
cell.mainImageView.clipsToBounds = true
cell.mainImageView.layer.frame = CGRectInset(cell.mainImageView.layer.frame, 60, 60)
cell.mainImageView.layer.borderColor = UIColor.purpleColor().CGColor
cell.mainImageView.layer.borderWidth = 2.0
}
let imageFile = business.displayImage
cell.mainImageView.file = imageFile
cell.mainImageView.loadInBackground()

// Button steup
let defaultAttributes = [
NSFontAttributeName: UIFont.fontAwesomeOfSize(15),
NSForegroundColorAttributeName: UIColor.grayColor()]
let activeAttributes = [
NSFontAttributeName: UIFont.fontAwesomeOfSize(15),
NSForegroundColorAttributeName: UIColor.blueColor()]

cell.bookmarkButton.setTitleTextAttributes(defaultAttributes, forState: .Normal)
cell.bookmarkButton.title = "\(String.fontAwesomeIconWithName(.BookmarkO)) Bookmark"

let bookmarkQuery = PFQuery(className: "Bookmark")
bookmarkQuery.whereKey("business", equalTo: business)
bookmarkQuery.whereKey("user", equalTo: User.currentUser()!)
bookmarkQuery.cachePolicy = .NetworkElseCache
bookmarkQuery.countObjectsInBackgroundWithBlock { (count, error) in
if error == nil && count > 0 {
cell.bookmarkButton.setTitleTextAttributes(activeAttributes, forState: .Normal)
cell.bookmarkButton.title = "\(String.fontAwesomeIconWithName(.Bookmark)) Bookmark"
}
}

cell.phoneButton.setTitleTextAttributes(defaultAttributes, forState: .Normal)
cell.phoneButton.title = "\(String.fontAwesomeIconWithName(.Phone)) Call"
if business.phone == "" {
cell.phoneButton.enabled = false
}
cell.reviewButton.setTitleTextAttributes(defaultAttributes, forState: .Normal)
cell.reviewButton.title = "\(String.fontAwesomeIconWithName(.MapMarker)) Directions"

return cell
}

override func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
if indexPath.row + 1 > self.objects?.count {
return 44
}

let height = super.tableView(tableView, heightForRowAtIndexPath: indexPath)
return height
}

override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
if indexPath.row >= self.objects?.count {
self.loadNextPage()
tableView.deselectRowAtIndexPath(indexPath, animated: true)
return
}

//performSegueWithIdentifier("viewBusiness", sender: self)
}

/*
// MARK: - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
// Get the new view controller using segue.destinationViewController.
// Pass the selected object to the new view controller.
}
*/

}

Answer

I figured this out for anyone who has the same issue:

override func awakeFromNib() {
  super.awakeFromNib()
  addShadow()
}

I added the code in awakeFromNib instead of layoutSubviews

Comments