ios-swift-collapsible-table-section
ios-swift-collapsible-table-section copied to clipboard
Cannot use uiimageview as header title
I modified example data so it contains:
public var name: String public var imageURL: String public var items: [Item]
I can get imageURL string within this function:
func collapsibleTableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String? { return sections[section].imageURL }
but when I change it to return uiimage, it cannot show any image from assets even I already put that image inside assets with same name like imageURL string.
this is my code:
private func collapsibleTableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> UIImage? { let imageView = UIImageView(frame: CGRect(x: 0, y: 0, width: 40, height: 20)) imageView.contentMode = .scaleAspectFit let image = UIImage(named: "\(sections[section].imageURL)") imageView.image = image return imageView.image }
How to show image within header title in section?
you can use it in CollapsibleTableViewHeader class, just replace your arrowLabel with something similar to code below;
// Arrow Image View contentView.addSubview(triangleImageView) triangleImageView.translatesAutoresizingMaskIntoConstraints = false // width constant triangleImageView.widthAnchor.constraint(equalToConstant: 15).isActive = true // aspectRatio - or you may use height constant as well triangleImageView.heightAnchor.constraint(equalTo: triangleImageView.widthAnchor, multiplier: 1.15).isActive = true triangleImageView.centerYAnchor.constraint(equalTo: marginGuide.centerYAnchor).isActive = true triangleImageView.trailingAnchor.constraint(equalTo: marginGuide.trailingAnchor).isActive = true