我正在尝试创建卡片类型布局,并在此处使用了许多答案来获取我当前的解决方案。我使用的是Swift 2和XCode 7.3.1。
首次App负载
拉动刷新后
UITableViewCell代码是:
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()
}
为什么在拉动刷新后单元格看起来是正确的,并且预先在右边有额外的阴影?
编辑:
我还添加了TableViewController
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.
}
*/
}
答案 0 :(得分:0)
我认为任何有同样问题的人都会这样做:
override func awakeFromNib() {
super.awakeFromNib()
addShadow()
}
我在awakeFromNib
而非layoutSubviews