8 votes

Ombre autour d'une section de UITableView

J'ai un UITableView avec 4 sections. Maintenant je veux ajouter un effet d'ombre à une section particulière du tableau mais pas au tableau entier. Comment puis-je réaliser cette tâche ?

0voto

jimpic Points 3213

Vous devrez modifier l'en-tête et le pied de page de la section, ainsi que toutes les cellules de la section.

Utilice tableView:viewForFooterInSection: , tableView:viewForHeaderInSection: y tableView:cellForRowAtIndexPath: pour ça. Ajoutez simplement un UIView avec rgba 0/0/0/0.2 ou similaire à chaque vue que vous voulez rendre sombre.

0voto

Denis Chaschin Points 237

Vous pouvez ajouter une image avec une ombre comme arrière-plan de UITableViewCell. L'ombre doit être dessinée dans l'image. C'est très simple et votre application fonctionnera plus rapidement.

-1voto

Mathew Varghese Points 2535

Vous pouvez spécifier l'ombre pour la section que vous voulez. Voici un exemple.

Tout d'abord, nous mettons de l'espace à disposition pour votre en-tête.

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    if(section == mysection)
    {
        // Returns the height you want for the header section. I am giving 20
        return 20; 
    }
}

Puis la décoration de l'en-tête

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    if(section == mysection)
    {
        UIView *shadowView  =  [[[UIView alloc] initWithFrame: CGRectMake(0,0,320,20)] autorelease];
        shadowView.backgroundColor = [UIColor whiteColor];

        // Doing the Decoration Part
        shadowView.layer.shadowColor = [[UIColor blackColor] CGColor];
        shadowView.layer.shadowOffset = CGSizeMake(1.0f, 1.0f);
        shadowView.layer.shadowRadius = 3.0f;
        shadowView.layer.shadowOpacity = 1.0f;

        return shadowView;
    }
    return nil;
}

Complétez-le de votre côté. Celui-ci est un schéma de base. Bon codage :)

-3voto

Omarj Points 1357

Essayez ceci pour l'ombre de la table complète

yourView.layer.shadowColor = [[UIColor blackColor] CGColor];
yourView.layer.shadowOffset = CGSizeMake(1.0f, 1.0f);
yourView.layer.shadowRadius = 3.0f;
yourView.layer.shadowOpacity = 1.0f; 

Essayez ceci pour l'ombre de la section

override func tableView(_ tableView: UITableView, willDisplay cell: UITableViewCell, forRowAt indexPath: IndexPath) {

    if (cell.responds(to: #selector(getter: UIView.tintColor))){
        if tableView == self.tableView {
            let cornerRadius: CGFloat = 12.0
            cell.backgroundColor = .clear
            let layer: CAShapeLayer = CAShapeLayer()
            let path: CGMutablePath = CGMutablePath()
            let bounds: CGRect = cell.bounds
            bounds.insetBy(dx: 25.0, dy: 0.0)
            var addLine: Bool = false

            if indexPath.row == 0 && indexPath.row == ( tableView.numberOfRows(inSection: indexPath.section) - 1) {
                path.addRoundedRect(in: bounds, cornerWidth: cornerRadius, cornerHeight: cornerRadius)

            } else if indexPath.row == 0 {
                path.move(to: CGPoint(x: bounds.minX, y: bounds.maxY))
                path.addArc(tangent1End: CGPoint(x: bounds.minX, y: bounds.minY), tangent2End: CGPoint(x: bounds.midX, y: bounds.minY), radius: cornerRadius)
                path.addArc(tangent1End: CGPoint(x: bounds.maxX, y: bounds.minY), tangent2End: CGPoint(x: bounds.maxX, y: bounds.midY), radius: cornerRadius)
                path.addLine(to: CGPoint(x: bounds.maxX, y: bounds.maxY))

            } else if indexPath.row == (tableView.numberOfRows(inSection: indexPath.section) - 1) {
                path.move(to: CGPoint(x: bounds.minX, y: bounds.minY))
                path.addArc(tangent1End: CGPoint(x: bounds.minX, y: bounds.maxY), tangent2End: CGPoint(x: bounds.midX, y: bounds.maxY), radius: cornerRadius)
                path.addArc(tangent1End: CGPoint(x: bounds.maxX, y: bounds.maxY), tangent2End: CGPoint(x: bounds.maxX, y: bounds.midY), radius: cornerRadius)
                path.addLine(to: CGPoint(x: bounds.maxX, y: bounds.minY))

            } else {
                path.addRect(bounds)
                addLine = true
            }

            layer.path = path
            layer.fillColor = UIColor.white.withAlphaComponent(0.8).cgColor

            if addLine {
                let lineLayer: CALayer = CALayer()
                let lineHeight: CGFloat = 1.0 / UIScreen.main.scale
                lineLayer.frame = CGRect(x: bounds.minX + 10.0, y: bounds.size.height - lineHeight, width: bounds.size.width, height: lineHeight)
                lineLayer.backgroundColor = tableView.separatorColor?.cgColor
                layer.addSublayer(lineLayer)
            }

            let testView: UIView = UIView(frame: bounds)
            testView.layer.insertSublayer(layer, at: 0)
            testView.backgroundColor = .clear
            cell.backgroundView = testView
        }
    }
}

Prograide.com

Prograide est une communauté de développeurs qui cherche à élargir la connaissance de la programmation au-delà de l'anglais.
Pour cela nous avons les plus grands doutes résolus en français et vous pouvez aussi poser vos propres questions ou résoudre celles des autres.

Powered by:

X