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 ?
Réponses
Trop de publicités?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.
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 :)
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
}
}
}