I have a UItableview cell that is contain a UItableview, I need to make height of that cell equal to height of it's child UItableview.
Image below explain what I need to do.
First see my ViewController,which has one tableview(tblview) and UITableViewCell(CustomTableViewCell) ,
class ViewController: UIViewController {
#IBOutlet var tblview:UITableView!
override func viewDidLoad() {
super.viewDidLoad()
self.tblview.delegate = self
self.tblview.dataSource = self
// Do any additional setup after loading the view, typically from a nib.
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
extension ViewController:UITableViewDelegate,UITableViewDataSource{
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return 5
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: CustomTableViewCell.identifier) as! CustomTableViewCell
return cell
}
func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
return UITableViewAutomaticDimension
}
func tableView(_ tableView: UITableView, estimatedHeightForRowAt indexPath: IndexPath) -> CGFloat {
return UITableViewAutomaticDimension
}
}
Then see my CustomTableViewCell which has one table view and one label in a cell.See,
class CustomTableViewCell: UITableViewCell {
static let identifier = "CustomTableViewCell"
#IBOutlet var tblviewCell:UITableView!
override func awakeFromNib() {
super.awakeFromNib()
// Initialization code
self.tblviewCell.delegate = self
self.tblviewCell.dataSource = self
tblviewCell.isScrollEnabled = false
}
override func setSelected(_ selected: Bool, animated: Bool) {
super.setSelected(selected, animated: animated)
// Configure the view for the selected state
}
override func systemLayoutSizeFitting(_ targetSize: CGSize, withHorizontalFittingPriority horizontalFittingPriority: UILayoutPriority, verticalFittingPriority: UILayoutPriority) -> CGSize {
let heighToReturn = self.tblviewCell.contentSize.height + 20 // upper and down space
return CGSize(width: self.tblviewCell.contentSize.width, height: heighToReturn)
}
}
extension CustomTableViewCell:UITableViewDelegate,UITableViewDataSource{
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int
{
return 5
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: customCell.identifier) as! customCell
cell.lblname?.text = "Vikas"
return cell
}
func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
return 80
}
}
class customCell: UITableViewCell {
static let identifier = "customCell"
#IBOutlet weak var lblname :UILabel?
}
So, if you give tableview content size height in systemLayoutSizeFitting method then problem will be solve.
I hope this will help.
Here's how to do table header cells. The cell needs to be prototyped in the storyboard and subclassed (if needs to be configured). Then override the following funcs in the table view delegate. Alternatively, you can generate a view on the fly and return that from viewForHeaderInSection.
override func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
return 150
}
override func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
let headerCell = tableView.dequeueReusableCell(withIdentifier: "ConferenceHeaderCell") as! ConferenceDetailHeaderCell
// configure cell
return headerCell
}
For the cells themselves, it's very similar:
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "ConferenceUserCell", for: indexPath) as! ConferenceDetail_TVCell
// Configure the cell...
return cell
}
and depending, you may to implement 'heightForRowAtIndexPath'
Related
UITableViewCell is crashing with "Unexpectedly found nil while implicitly unwrapping an Optional value" when i try to modify cell subviews.
Registering tableView cell
self.tableView.register(TravelTypeTableViewCell.self, forCellReuseIdentifier: "TravelCell")
tablView methods
func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
return 224
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return 1
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "TravelCell", for: indexPath) as! TravelTypeTableViewCell
cell.travelTypeSegment.selectedSegmentIndex = 0
return cell
}
TableViewcell class
class TravelTypeTableViewCell: UITableViewCell {
#IBOutlet var travelTypeSegment: UISegmentedControl!
override func awakeFromNib() {
super.awakeFromNib()
// Initialization code
}
override func setSelected(_ selected: Bool, animated: Bool) {
super.setSelected(selected, animated: animated)
// Configure the view for the selected state
}
}
Storyboard images
Comment this
self.tableView.register(TravelTypeTableViewCell.self, forCellReuseIdentifier: "TravelCell")
As for prototype cells you shouldn't register them as this done automatically for them and make sure travelTypeSegment is connected in IB
I have a CollectionView that is scrolling horizontally. I have custom CollectionViewCells that have inside a TableView with three cells. It looks like this:
[![Here is a example of how it looks][1]][1]
This is the main ViewController where I have the CollectionView
extension ViewController: UICollectionViewDataSource {
func numberOfSections(in collectionView: UICollectionView) -> Int {
return 1
}
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return self.viewModel.numberOfItems
}
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell
{
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: ItemCell.reuseIdentifier, for: indexPath) as! ItemCell
return cell
}
func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
let selectedItem = self.viewModel.items[indexPath.item]
}
}
And this is the ItemCell code that contains the tableview
class ItemCell: UICollectionViewCell, UITableViewDelegate, UITableViewDataSource {
#IBOutlet weak var tableView: UITableView!
override func awakeFromNib() {
super.awakeFromNib()
self.tableView.delegate = self
self.tableView.dataSource = self
}
override func layoutSubviews() {
super.layoutSubviews()
self.tableView.allowsMultipleSelection = true
self.tableView.register(UINib(nibName: "DailySelectionViewCell", bundle: nil), forCellReuseIdentifier: DailySelectionViewCell.reuseIdentifier)
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return 1
}
func numberOfSections(in tableView: UITableView) -> Int {
return 3
}
func tableView(_ tableView: UITableView, heightForFooterInSection section: Int) -> CGFloat {
return 20
}
func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
return 60
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: DailySelectionViewCell.reuseIdentifier, for: indexPath) as! DailySelectionViewCell
switch indexPath.section {
case 0:
cell.descriptionLabel.text = "Item1"
case 1:
cell.descriptionLabel.text = "Item2"
default:
cell.descriptionLabel.text = "Item3"
}
return cell
}
func tableView(_ tableView: UITableView, viewForFooterInSection section: Int) -> UIView? {
let view = UIView(frame: CGRect.zero)
view.backgroundColor = UIColor.clear
return view
}
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
}
}
I want to click on the TableView cells that are inside the CollectionView cells. When I want to click on a TableViewCell, the touches are only registered on the CollectionViewCell and I can't click on any of the tableview cells inside the CollectionViewCell.
By default didSelectRowAt must work.
As an assumption DailySelectionViewCell may contains some views with user interaction, namely subclassed from UIControl: UIButton, UIDatePicker, UISegmentedControl and so on. In this case didSelectRowAt not work and this is correct behavior.
numberOfSection and numberOfRowsInSection of UITableViewController is getting called but cellForRow not. What could be the reason ? Below is the code for tableViewController.
class GlobalSearchTableViewController: UITableViewController {
/// MARK: Properties
/// Delegate
weak var delegate: GlobalSearchTableViewControllerDelegate?
private var state = GlobalSearchTableState(searchResults: [])
override func viewDidLoad() {
super.viewDidLoad()
tableView.estimatedSectionFooterHeight = 60
tableView.sectionFooterHeight = UITableView.automaticDimension
tableView.estimatedRowHeight = 60
tableView.rowHeight = UITableView.automaticDimension
}
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
}
override func numberOfSections(in tableView: UITableView) -> Int {
return 2
}
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return 10
}
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
return UITableViewCell()
}
extension GlobalSearchTableViewController: GlobalSearchTablePresenter {
func present(state: GlobalSearchTableState) {
tableView.reloadData()
}
try to return cell
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
var cell : UITableViewCell!
cell = tableView.dequeueReusableCell(withIdentifier: "cell")
cell.textLabel?.text = dataArray[indexPath.row]
return cell
}
Try adding tableView.delegate = self or tableView.dataSource = self.
i am trying to resort UITableView cell's by long press then drag and drop the cell , its not working when i use Custom cell view any idea why !
Here is the TableView viewcontroller :
override func viewDidLoad()
{
super.viewDidLoad()
// Then delegate the TableView
self.tableView.delegate = self
self.tableView.dataSource = self
// Register table cell class from nib
let bundle = Bundle(for: type(of: self))
let cellNib = UINib(nibName: "tbc_song_vertical", bundle: bundle)
self.tableView.register(cellNib, forCellReuseIdentifier: "tbc_song_vertical")
//Loading Template
let nib_tbc_loading = UINib(nibName: "tbc_loading", bundle: bundle)
self.tableView.register(nib_tbc_loading, forHeaderFooterViewReuseIdentifier: "tbc_loading")
//Automated ell height
self.tableView.estimatedRowHeight = 44.0
self.tableView.rowHeight = UITableViewAutomaticDimension
self.tableView.reloadData()
self.tableView.isEditing = true
}
// MARK: - Sorting
func tableView(_ tableView: UITableView, editingStyleForRowAt indexPath: IndexPath) -> UITableViewCellEditingStyle {
return .none
}
func tableView(_ tableView: UITableView, shouldIndentWhileEditingRowAt indexPath: IndexPath) -> Bool {
return false
}
func tableView(_ tableView: UITableView, moveRowAt sourceIndexPath: IndexPath, to destinationIndexPath: IndexPath) {
let movedObject = self.data[sourceIndexPath.row]
data.remove(at: sourceIndexPath.row)
data.insert(movedObject, at: destinationIndexPath.row)
// To check for correctness enable: self.tableView.reloadData()
}
//loading footer
func tableView(_ tableView: UITableView, viewForFooterInSection section: Int) -> UIView? {
let footerView = self.tableView.dequeueReusableHeaderFooterView(withIdentifier: "tbc_loading") as! tbc_loading
footerView.startAnimate()
return footerView
}
//loading footer
func tableView(_ tableView: UITableView, heightForFooterInSection section: Int) -> CGFloat {
return ( self.is_fetching ) ? 40 : 0
}
//Pagination
func scrollViewDidEndDragging(_ scrollView: UIScrollView, willDecelerate decelerate: Bool) {
//Bottom Refresh
if scrollView == tableView{
if ((scrollView.contentOffset.y + scrollView.frame.size.height) >= scrollView.contentSize.height )
{
}
}
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return data.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell
{
let cell : tbc_song_vertical = tableView.dequeueReusableCell(withIdentifier: "tbc_song_vertical", for: indexPath) as! tbc_song_vertical
cell.fillwithInfo(dto: self.data[indexPath.row] )
return cell
}
// Tabbed Cell
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) { }
func numberOfSections(in tableView: UITableView) -> Int {
return 1
}
}
only if i use custom view its not working , but when i hold my finger on the right side of any cell to drag it up or down its dragging all the table
Add the following method
func tableView(_ tableView: UITableView, canMoveRowAt indexPath: IndexPath) -> Bool {
return true
}
I am trying to display a textfield on a tableview using a custom cell. I think I have set up the delegation protocol properly, but when loaded, the tableview doesn't show the textfield. I have added a textfield into the customcell, set the class to CustomCell, and changed the identifier to "Cell", but I am not sure what else I am missing.
view controller with tableview:
#IBOutlet weak var tableView: UITableView!
override func viewDidLoad() {
super.viewDidLoad()
tableView.delegate = self
tableView.dataSource = self
}
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
print("Hello")
}
func numberOfSections(in tableView: UITableView) -> Int {
return 1
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return data.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "Cell", for: indexPath) as! CustomCell
cell.cellDelegate = self
cell.contentView.bringSubview(toFront: cell.textField)
cell.textField.delegate = self as? UITextFieldDelegate
cell.textField.text = "Test"
return cell
}
func didPressTextField(indexPath: IndexPath) {
print("textfield was tapped")
}
custom cell VC:
protocol CustomCellDelegate : class {
func didPressTextField(indexPath: IndexPath)
}
class CustomCell: UITableViewCell {
weak var cellDelegate: CustomCellDelegate?
var indexPath: IndexPath!
override func awakeFromNib() {
super.awakeFromNib()
self.cellDelegate = nil
// Initialization code
}
#IBOutlet weak var textField: UITextField!
#IBAction func textFieldWasTapped(_ sender: UITextField) {
self.cellDelegate?.didPressTextField(indexPath: indexPath)
}
Your data source is empty.
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
// data is not containing any value
return data.count
}
Check data.count it should have some data and also if that is custom cell with XIB then you need to register cell in view didload
self.tableView.register(UINib(nibName: "Your Cell XIB name", bundle: nil),
forCellWithReuseIdentifier: "Cell")
Check your data.count. it must return value greater than 0 otherwise your tableView is showing with 0 rows and nothing is display in tableView.
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
// debug at this point check what is value of data.count its must be greater than 0
return data.count
}