无法在tableView中添加圆形掩码

时间:2016-01-23 06:17:22

标签: ios swift uitableview mask

我正在创建一个UITableViewCell对象,并将其返回到cellForRowAtIndexPath中的tableView函数内。我在单元格中有一个UIView,我想制作圆形。使用this link中的代码,我编写了以下代码:

 override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {


    let cell = UITableViewCell()

     let containerDP = UIView()
    containerDP.backgroundColor = UIColor.redColor()

    // some code 

   let firstLetter = UILabel()
        firstLetter.backgroundColor = UIColor.yellowColor()
                    firstLetter.text = (data[indexPath.row].UserObject.FirstName! as String).uppercaseString[0]

        firstLetter.font = UIFont(name: firstLetter.font.fontName, size: 50)
        firstLetter.adjustsFontSizeToFitWidth = true
        firstLetter.textAlignment = NSTextAlignment.Center

        firstLetter.layer.cornerRadius = firstLetter.frame.size.width / 2;
        firstLetter.clipsToBounds = true

        containerDP.addSubview(firstLetter)
        firstLetter.snp_makeConstraints { (make) -> Void in
            make.center.equalTo(containerDP)
            make.edges.equalTo(containerDP).inset(UIEdgeInsetsMake(10, 10, 10, 10))

        }

      // some code 


    return cell

    }

但仍然没有圆形面具:

enter image description here

3 个答案:

答案 0 :(得分:0)

试试这个:

 override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {


    let cell = UITableViewCell()

     let containerDP = UIView()
    containerDP.backgroundColor = UIColor.redColor()

    // some code 

   let firstLetter = UILabel()
        firstLetter.backgroundColor = UIColor.yellowColor()
                    firstLetter.text = (data[indexPath.row].UserObject.FirstName! as String).uppercaseString[0]

        firstLetter.font = UIFont(name: firstLetter.font.fontName, size: 50)
        firstLetter.adjustsFontSizeToFitWidth = true
        firstLetter.textAlignment = NSTextAlignment.Center

        firstLetter.layer.cornerRadius = firstLetter.frame.size.width / 2;
        firstLetter.layer.masksToBounds = true 
        firstLetter.clipsToBounds = true

        containerDP.addSubview(firstLetter)
        firstLetter.snp_makeConstraints { (make) -> Void in
            make.center.equalTo(containerDP)
            make.edges.equalTo(containerDP).inset(UIEdgeInsetsMake(10, 10, 10, 10))

        }

      // some code 


    return cell

    }

答案 1 :(得分:0)

  1. 使用dequeueReusableCellWithIdentifier进行tablewView单元格创建和缓存
  2. awakeFromNib方法
  3. 中为tableViewCell进行子视图设置
  4. clipsToBounds = true应该有效

答案 2 :(得分:0)

  • 首先,您应该为您的tableview创建一个customtableviewCell课程。
  • 然后将IBOutlet连接到您的cutomtablviewCell
  • 然后#import customtablviewCell tableviewController
  • 然后在cellForRowAtIndexPath方法内(我在ObjectiveC中,而不是在swift中)调用cutomTableviewcell,如下所示

    customTableviewCell * cell = [您的代码在这里];

  • 之后你的风格就像

    cell.fisrtLetter.layer.cornerRadius = cell.firstletter.frame.size.width / 2;     cell.firstLetter.layer.masksToBounds = true;

  • 请明白。