Last active
August 23, 2020 04:14
-
-
Save rahulsingh1101/c102dcde24d5ab7652dcd2035065bd4d to your computer and use it in GitHub Desktop.
Autoresizing content size of table view
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class RelatedItemsTableView: UITableView, UITableViewDelegate, UITableViewDataSource { | |
var items = [Items]() { | |
didSet{ | |
self.reloadData() | |
} | |
} | |
var headerTitles = [String]() { | |
didSet { | |
self.reloadSections(IndexSet.init(arrayLiteral: 0), with: .automatic) | |
} | |
} | |
override var intrinsicContentSize: CGSize { | |
self.layoutIfNeeded() | |
return self.contentSize | |
} | |
override var contentSize: CGSize { | |
didSet{ | |
self.invalidateIntrinsicContentSize() | |
} | |
} | |
override func reloadData() { | |
super.reloadData() | |
self.invalidateIntrinsicContentSize() | |
} | |
weak var relatedItemsDelegate: RelatedItemCellDelegate? | |
override init(frame: CGRect, style: UITableView.Style) { | |
super.init(frame: frame, style: .plain) | |
setupView() | |
} | |
required init?(coder: NSCoder) { | |
super.init(coder: coder) | |
} | |
private func setupView(){ | |
self.delegate = self | |
self.dataSource = self | |
self.isScrollEnabled = false | |
self.register(UINib.init(nibName: "RelatedItemsCell", bundle: nil), forCellReuseIdentifier: "RelatedItemsCell") | |
self.backgroundColor = .clear | |
self.separatorStyle = .none | |
} | |
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { | |
return items.count | |
} | |
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { | |
let cell = tableView.dequeueReusableCell(withIdentifier: "RelatedItemsCell", for: indexPath) as! RelatedItemsCell | |
let item = items[indexPath.row] | |
cell.itemImage.image = UIImage.init(named: item.imageName) | |
cell.itemName.text = item.name | |
cell.itemOriginalPrice.attributedText = item.originalPrice.strikeThrough() | |
cell.itemDiscountedPrice.text = item.discountedPrice | |
cell.itemQuantity.setTitle(item.quantity, for: .normal) | |
cell.relatedDelegate = self | |
cell.itemQuantity.tag = indexPath.row | |
return cell | |
} | |
func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? { | |
let stackView = UIStackView() | |
stackView.axis = .horizontal | |
stackView.spacing = 10 | |
stackView.alignment = .fill | |
stackView.distribution = .fillEqually | |
stackView.layoutMargins = UIEdgeInsets(top: 0, left: 8, bottom: 0, right: 20) | |
stackView.isLayoutMarginsRelativeArrangement = true | |
for (index,text) in headerTitles.enumerated() { | |
let label = UILabel() | |
stackView.addArrangedAutolayoutSubview(label) | |
apply(label) { | |
$0.text = text | |
if index == 0 { | |
$0.textColor = kDarkTextColor | |
$0.textAlignment = .left | |
} else { | |
$0.textColor = kHeadingTitleTextColor | |
$0.textAlignment = .right | |
} | |
$0.font = UIFont(name: kMuliBold, size: 15) | |
} | |
} | |
return stackView | |
} | |
func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat { | |
return 50 | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
let relatedItems = RelatedItemsTableView() | |
relatedItems.relatedItemsDelegate = self | |
relatedItems.headerTitles = ["Related Items"] | |
relatedItems.items = [.init(imageName: "mangoes", | |
name: "Seasoned Mangoes", | |
quantity: "500g", | |
originalPrice: "Rs.180", | |
discountedPrice: "149"), | |
.init(imageName: "oil_masala", | |
name: "Oil & Masala", | |
quantity: "500g", | |
originalPrice: "Rs.180", | |
discountedPrice: "149"), | |
.init(imageName: "chicken", | |
name: "Chicken", | |
quantity: "500g", | |
originalPrice: "Rs.180", | |
discountedPrice: "149"), | |
.init(imageName: "pedegree", | |
name: "Pedigree", | |
quantity: "500g", | |
originalPrice: "Rs.180", | |
discountedPrice: "149") | |
] | |
scrollContentView.addAutolayoutSubview(relatedItems) | |
NSLayoutConstraint.activate([ | |
relatedItems.topAnchor.constraint(equalTo: detailsView.bottomAnchor, constant: 20), | |
relatedItems.leadingAnchor.constraint(equalTo: scrollContentView.leadingAnchor, constant: 12), | |
relatedItems.trailingAnchor.constraint(equalTo: scrollContentView.trailingAnchor, constant: -12), | |
relatedItems.bottomAnchor.constraint(equalTo: scrollContentView.bottomAnchor, constant: -20), | |
]) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment