圆形标签在swift xcode中不起作用

时间:2017-07-10 08:54:04

标签: swift xcode

我是快速IOS编程的新手。我需要将标签四舍五入。我已经搜索了SO中的代码并抓取到我的应用程序,这是接受的答案和upvoted超过10.但是,在我的情况下,代码不起作用。

CODE

 override init(style: UITableViewCellStyle, reuseIdentifier: String!) 
 {
    super.init(style: style, reuseIdentifier: reuseIdentifier)
    changeToRoundLable(countLabel: txtDays)
 }

我在UITableViewCell class.in下面的contructor上实现了它。

import UIKit

class ProductListItemCell: UITableViewCell {

@IBOutlet weak var txtOfferPercentage: UILabel!



@IBOutlet weak var txtDays: UILabel!


@IBAction func btnViewDeal(_ sender: UIButton) {

}

override init(style: UITableViewCellStyle, reuseIdentifier: String!) {
    super.init(style: style, reuseIdentifier: reuseIdentifier)
    changeToRoundLable(countLabel: txtDays)

    offerPercentageImage()
}

required init?(coder aDecoder: NSCoder) {
    super.init(coder: aDecoder)

}
func offerPercentageImage(){
    let point =  CGPoint(x:10,y:10);
    let size = CGSize(width:txtOfferPercentage.frame.size.width - 20,height:20)
    let labelLeft = SMIconLabel(frame: CGRect(origin:point,size: size))
    labelLeft.text = "Icon on the left, text on the left"

    // Here is the magic
    labelLeft.icon = UIImage(named: "percentage") // Set icon image
    labelLeft.iconPadding = 5               // Set padding between icon and label
    labelLeft.numberOfLines = 0  // Icon position
    labelLeft.iconPosition.horizontal = SMIconHorizontalPosition.right
    txtOfferPercentage.addSubview(labelLeft)
}



func changeToRoundLable(countLabel : UILabel){


    let size:CGFloat = 55.0
    countLabel.textColor = UIColor.white
    countLabel.textAlignment = .center
    countLabel.font = UIFont.systemFont(ofSize: 14.0)
    countLabel.bounds = CGRect(x : 0.0,y : 0.0,width : size, height :  size)
    countLabel.layer.cornerRadius = size / 2
    countLabel.layer.borderWidth = 3.0
    countLabel.layer.masksToBounds = true
    countLabel.layer.backgroundColor = UIColor.orange.cgColor
    countLabel.layer.borderColor = UIColor.orange.cgColor
    countLabel.translatesAutoresizingMaskIntoConstraints = false
}
override func awakeFromNib() {
    super.awakeFromNib()

}

}

我不知道我犯了哪个错误。请帮助我。

XCode版本:8.3.3(8E3004b) Swift版本:Apple Swift 3.1版

更新

  class ProductListPageController: 
  UIViewController,UITableViewDataSource,UITableViewDelegate {



  @IBOutlet weak var tblProductList: UITableView!
  override func viewDidLoad() {
    super.viewDidLoad()
 //          tabname.text = tabText!
       // Do any additional setup after loading the view.
  }

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

  func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
      return 10
}
  func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {

    let cell = self.tblProductList.dequeueReusableCell(withIdentifier: "cell", for: indexPath) as! ProductListItemCell
    cell.imgProduct.image = UIImage(named: "burger")

    return cell
  }

 }

按以下方式创建单元格:

->whereBetween('reservation_time_start', [$time_from, $time_to])
->whereBetween('reservation_time_end', [$time_from, $time_to])
->get();

1 个答案:

答案 0 :(得分:0)

以这种方式创建自定义单元格时,只会调用required init?(coder aDecoder: NSCoder) {,而不是override init(style: UITableViewCellStyle, reuseIdentifier: String!)

您可以将标签设置代码移动到所需的init方法中,也可以在cellForRow方法中执行此操作。