import UIKit
class CustomTableViewCell: UITableViewCell {
var labelStackView: UIStackView!
var innerView: UIView!
var label: UILabel!
override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
super.init(style: style, reuseIdentifier: reuseIdentifier)
setupInnerView()
setupLabelStackView()
}
required init?(coder: NSCoder) {
super.init(coder: coder)
setupInnerView()
setupLabelStackView()
}
private func setupInnerView() {
innerView = UIView()
innerView.translatesAutoresizingMaskIntoConstraints = false
innerView.layer.borderWidth = 1.0 // 1-point border width
innerView.layer.borderColor = UIColor.lightGray.cgColor // Border color
contentView.addSubview(innerView)
NSLayoutConstraint.activate([
innerView.leadingAnchor.constraint(equalTo: contentView.leadingAnchor, constant: 10), // 8-point spacing from the left
innerView.topAnchor.constraint(equalTo: contentView.topAnchor, constant: 6), // 8-point spacing from the top
innerView.trailingAnchor.constraint(equalTo: contentView.trailingAnchor, constant: -10), // 8-point spacing from the right
innerView.bottomAnchor.constraint(equalTo: contentView.bottomAnchor, constant: -6) // 8-point spacing from the bottom
])
innerView.layer.cornerRadius = 13
innerView.layer.borderColor = self.hexStringToUIColor(hex: "DCD8D8").cgColor
}
private func setupLabelStackView() {
labelStackView = UIStackView()
labelStackView.translatesAutoresizingMaskIntoConstraints = false
labelStackView.axis = .vertical
labelStackView.spacing = 8 // Adjust the vertical spacing between labels
contentView.addSubview(labelStackView)
NSLayoutConstraint.activate([
labelStackView.leadingAnchor.constraint(equalTo: contentView.leadingAnchor, constant: 16),
labelStackView.topAnchor.constraint(equalTo: contentView.topAnchor, constant: 16),
labelStackView.trailingAnchor.constraint(equalTo: contentView.trailingAnchor, constant: -16),
labelStackView.bottomAnchor.constraint(equalTo: contentView.bottomAnchor, constant: -16)
])
}
func setupLabels(labelData: [(String, UIImage, Int)]) -> ([UILabel], [UIImageView], [Int]) {
var labels: [UILabel] = []
var imageViews: [UIImageView] = []
var tags: [Int] = []
for (labelText, image, tag) in labelData {
print("tagfkj : ", tag)
let stackView = UIStackView()
stackView.axis = .horizontal
stackView.spacing = 8
let imageView = UIImageView(image: image)
imageView.contentMode = .scaleAspectFit
imageView.widthAnchor.constraint(equalToConstant: 23).isActive = true
imageView.heightAnchor.constraint(equalToConstant: 23).isActive = true
imageView.tag = tag
imageView.accessibilityIdentifier = "sewer"
let label = UILabel()
label.translatesAutoresizingMaskIntoConstraints = false
label.text = labelText
label.tag = tag
stackView.addArrangedSubview(imageView)
stackView.addArrangedSubview(label)
labelStackView.addArrangedSubview(stackView)
labels.append(label)
imageViews.append(imageView)
tags.append(tag)
}
return (labels, imageViews, tags)
}
func hexStringToUIColor (hex:String) -> UIColor {
var cString:String = hex.trimmingCharacters(in: .whitespacesAndNewlines).uppercased()
if (cString.hasPrefix("#")) {
cString.remove(at: cString.startIndex)
}
if ((cString.count) != 6) {
return UIColor.gray
}
var rgbValue:UInt64 = 0
Scanner(string: cString).scanHexInt64(&rgbValue)
return UIColor(
red: CGFloat((rgbValue & 0xFF0000) >> 16) / 255.0,
green: CGFloat((rgbValue & 0x00FF00) >> 8) / 255.0,
blue: CGFloat(rgbValue & 0x0000FF) / 255.0,
alpha: CGFloat(1.0)
)
}
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
var imagesArray = ["circle_complete_icon", "circle_unsel"]()
var textArray = ["jfdsk", "dfs"]()
var tag = indexpath.row
var idsArray = ["1","2"]()
tag = indexPath.row
print("tag43 : \(indexPath.row) ", tag)
let cell = tableView.dequeueReusableCell(withIdentifier: "CustomTableViewCell", for: indexPath) as! CustomTableViewCell
cell.selectionStyle = .none
for subview in cell.labelStackView.arrangedSubviews {
cell.labelStackView.removeArrangedSubview(subview)
subview.removeFromSuperview()
}
var count = 0
let labelsAndImagesForCell = textArray.enumerated().map { (index, labelText) -> (String, UIImage, Int) in
let imageName = imagesArray[count]
count += 1
let image = UIImage(named: imageName) ?? UIImage()
// Replace "UIImage()" with your default image if needed
print("chk378: ", indexPath.row)
return (labelText, image, tag)
}
let (labels, imageViews, tags) = cell.setupLabels(labelData: labelsAndImagesForCell)
// Access and configure each label and image view individually
var c = 0
for (index, label) in (labels).enumerated() {
label.text = labelsAndImagesForCell[index].0
let labelTapGesture = UITapGestureRecognizer(target: self, action: #selector(subCategoryPressed(tagGesture: )))
label.tag = tags[c]
label.accessibilityIdentifier = idsArray[c]
c += 1
label.isUserInteractionEnabled = true
label.addGestureRecognizer(labelTapGesture)
}
var c1 = 0
for (index, imageView) in imageViews.enumerated() {
imageView.image = labelsAndImagesForCell[index].1
let tapGesture = UITapGestureRecognizer(target: self, action: #selector(subCategoryPressed(tagGesture: )))
imageView.tag = tags[c1]
imageView.accessibilityIdentifier = idsArray[c1]
c1 += 1
imageView.isUserInteractionEnabled = true
imageView.addGestureRecognizer(tapGesture)
}
imagesArray = []
textArray = []
return cell
}
Comments