Enkha
Enkha

Reputation: 430

Not work UITableView in cellForRowAt But numberOfRowsInsection Work in Swift

I wrote a function to add a Cell in a UITableView.

numberOfRowsInSection works but cellForRowAt does not work.

class ShopMenuViewCell: UITableViewCell {
    @IBOutlet weak var menuTableView: UITableView!

    var menuPrice: MenuPrice!

    override func awakeFromNib() 
    {
        super.awakeFromNib()
    }
}

extension ShopMenuViewCell
{
    func setView()
    {
        self.menuTableView.delegate = self
        self.menuTableView.dataSource = self
        self.menuTableView.regCells(cells: ["ShopMenuInformationCell"])

        DispatchQueue.main.async
            {
                self.menuTableView.reloadData()
            }
    }
}

extension ShopMenuViewCell : UITableViewDelegate, UITableViewDataSource {
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return 1
    }

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        guard let cell = tableView.dequeueReusableCell(withIdentifier: "ShopMenuInformationCell", for: indexPath) as? ShopMenuInformationCell,
            let menu = self.menuPrice,
            let name = menu.name,
            let price = menu.amt else { return ShopMenuInformationCell() }

        return cell
    }
}

In ShpMenuInformationCell.swift

class ShopMenuInformationCell: UITableViewCell
{
    @IBOutlet weak var containerView: UIView!
    @IBOutlet weak var shopNameLabel: UILabel!
    @IBOutlet weak var menuNameLabel: UILabel!
    @IBOutlet weak var priceLabel: UILabel!

    override func awakeFromNib()
    {
        super.awakeFromNib()
    }
}

I saw the answer to add a ViewController, but I do not know what to do because it is not a storyboard. (.xib)

I think it is because I add UITableViewCell in the UITableViewCell class.

Do you know why?

Upvotes: 1

Views: 1370

Answers (3)

Enkha
Enkha

Reputation: 430

I had a problem for about 5 days and found a solution!

First Reference Site: Joel's Answer cellForRowAtIndexPath: not called

Second reference site: Paolo's answer Swift: UITableViewCell rowheight

When I add another kind of TableCell, the vertical size of the cell in question is small.

So I increased the size of the Cell's RowHeight (frame.height) before reloading the data and it's fixed!

Currently, cellforrowat runs safely. Your answers were helpful to the results.

Upvotes: 2

iOS_Maccus
iOS_Maccus

Reputation: 397

Try below code:

Solution 1

class ShopMenuViewCell: UITableViewCell {

       @IBOutlet weak var menuTableView: UITableView! {
                didSet {
                    self.menuTableView.regCells(cells: ["ShopMenuInformationCell"])
                    self.menuTableView.delegate = self
                    self.menuTableView.dataSource = self
                }
            }
    }

Solution 2: Please check Anand Verma Answer

Upvotes: 0

Anand Verma
Anand Verma

Reputation: 592

Please try below code:

class ShopMenuViewCell: UITableViewCell
{
    @IBOutlet weak var menuTableView: UITableView!

    var menuPrice: MenuPrice!

    override func awakeFromNib()
    {
      super.awakeFromNib()
    }

   fun setUpTable() {
     self.menuTableView.regCells(cells: ["ShopMenuInformationCell"])
      self.menuTableView.delegate = self
      self.menuTableView.dataSource = self
      self.menuTableView.reloadData() 
     }
}

and call the function setUpTable() from ShopMenuViewCell as an cell.setUpTable()

Upvotes: 1

Related Questions