2016-06-28 4 views
1

В моем представлении таблицы у меня есть логика в моем tableView(tableView: UITableView, willDisplayCell cell: UITableViewCell, forRowAtIndexPath indexPath: NSIndexPath), чтобы определить, имеет ли ячейка закругленные углы сверху, снизу или без закругленных углов.Кнопка удаления не отображается на UITableViewCell с закругленными углами

override func tableView(tableView: UITableView, willDisplayCell cell: UITableViewCell, forRowAtIndexPath indexPath: NSIndexPath) { 

     if indexPath.row == 0 { 
      // round top 2 corners 
      let maskLayer = CAShapeLayer() 
      maskLayer.path = UIBezierPath(roundedRect: cell.bounds, byRoundingCorners: [UIRectCorner .TopLeft, UIRectCorner .TopRight], cornerRadii: CGSizeMake(4, 4)).CGPath 
      cell.layer.mask = maskLayer 
     } 

     if indexPath.section != 1 { 

      if indexPath.row == self.tableView.numberOfRowsInSection(indexPath.section) - 1 { 
       let maskLayer = CAShapeLayer() 
       maskLayer.path = UIBezierPath(roundedRect: cell.bounds, byRoundingCorners: [UIRectCorner .BottomLeft, UIRectCorner .BottomRight], cornerRadii: CGSizeMake(4, 4)).CGPath 
       cell.layer.mask = maskLayer 
      } 
     } 

    } 

Это работает, но после того, как я внедрил редактирование в виде таблицы, с кнопкой удаления происходят странные вещи.

С закругленными углами код:

with rounded corners

Когда я удалить скругленные углы:

without rounded corners

Это действительно сбивает с толку меня, потому что я не уверен, что я делаю неправильно. Может ли кто-нибудь помочь?

+0

Вы пытались добавить 'cell.layer.maskToBounds = true'? –

+0

Возможно, вы не должны делать это непосредственно с камеры. Попробуйте применить маску слоя к 'contentView' ячейки и посмотрите, работает ли это. – Bringo

ответ

0
override func tableView(tableView: UITableView, willDisplayCell cell: UITableViewCell, forRowAtIndexPath indexPath: NSIndexPath) { 

     if indexPath.row == 0 { 
      // round top 2 corners 
      let maskLayer = CAShapeLayer() 
      maskLayer.path = UIBezierPath(roundedRect: cell.bounds, byRoundingCorners: [UIRectCorner .TopLeft, UIRectCorner .TopRight], cornerRadii: CGSizeMake(4, 4)).CGPath 
      cell.contentView.layer.mask = maskLayer 
     } 

     if indexPath.section != 1 { 

      if indexPath.row == self.tableView.numberOfRowsInSection(indexPath.section) - 1 { 
       let maskLayer = CAShapeLayer() 
       maskLayer.path = UIBezierPath(roundedRect: cell.bounds, byRoundingCorners: [UIRectCorner .BottomLeft, UIRectCorner .BottomRight], cornerRadii: CGSizeMake(4, 4)).CGPath 
       cell.contentView.layer.mask = maskLayer 
      } 
     } 

    } 

использование cell.contentView.layer.mask = maskLayer

+0

Хммм ... Он показывает кнопку удаления, но ячейка не кажется округленной. Я попытался установить цвет фона ячейки, чтобы очистить фоновый цвет contentView до белого, но область вокруг красного круга показывает фон таблицы. – Minebomber

Смежные вопросы