0

I have multiple type cell to show in UITableView.

I have five type of cell in UITableView. all cell show according the different service response data, if service data nil or empty then we will not need to show the cell. so how i can check the number of row and show the cell according the service response.

Manjeet Singh
  • 57
  • 1
  • 10
  • 1
    What is your service resposne.? – Niroj Dec 04 '17 at 07:14
  • Share Response Format So we could see what actually need to do – iOS Geek Dec 04 '17 at 07:17
  • Can you give your service response format? – Irshad Ahmad Dec 04 '17 at 07:17
  • Possible duplicate of [2 different types of custom UITableViewCells in UITableView](https://stackoverflow.com/questions/1405688/2-different-types-of-custom-uitableviewcells-in-uitableview) – Vivek Molkar Dec 04 '17 at 08:24
  • you can designate difference sections for each service, and when the result arrived back from the web-server (asynchronously) just reload the related section in the table-view; with having neither section header nor footers, the final visual appearance would be just like a standard table-view with one session and dynamically appearing contents – it feels a safe and elegant solution, and you can deal with the sections independently; but I don't know how your data looks, so I can't be sure such concept would work for you as well. – holex Dec 04 '17 at 11:26

5 Answers5

1

Swift 3

Let say you have a tableView which has two types of cell. indexPath.row == 0 will show a header cell & rest of cell are info holder cell. For this you need to do the following things

extension YourControllerName: UITableViewDelegate, UITableViewDataSource {


      func numberOfSections(in tableView: UITableView) -> Int {
         return 1
      }

      func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {

         return yourDataHolderArray.count
      }

      func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        // Specify the height of cell if the cells height are different 
        if indexPath.row == 0 {
            return 30.0 
        }
        return 80.0
      }

      func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {

        if yourDataHolderArray.count > 0 {

            if indexPath.row == 0 {

                let cell = tableView.dequeueReusableCell(withIdentifier: "Cell1", for: indexPath) as! YourHeaderCell

                return cell
            }
            else {

                let cell = tableView.dequeueReusableCell(withIdentifier: "Cell2", for: indexPath) as! YourInfoCell

                return cell
            }
        }
        return UITableViewCell()
    }
}

Hope it helps.

Riajur Rahman
  • 1,976
  • 19
  • 28
0

The question could be better answered if you had provided the response. But for a generic approach, you can use numberOfSections method in combination with numberOfRows method.

Configure the numberOfSections method by the count of your responseData. And further you can assign numberOfRows in each section if there is data available for that section.

Amrit Sidhu
  • 1,870
  • 1
  • 18
  • 32
0

you can insert type in your array .. and in cellforRow function of tableView

if type == 1{ 
  // your first cell code
   }
else if type == 2
{
//your second cell
}

and so - on...

0

First receive the ResponseDataSet and call tableView.reloadData()

Then

  1. The number of section delegate will return the number of section

    func numberOfSections(in tableView: UITableView) -> Int {
    
        return 1
    }
    
  2. The number of rows delegate will return the number of rows in each respective section. if it returns 0 then the cell for row delegate will not be called

    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    
         return responseSet?.count ?? 0  // The count of response data set
    }
    
  3. Here you can check the type of each cell from responseSet and then create or reuse the cell using dequeue cell function.

        func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    
             var cell:UITableViewCell?
    
             if responseSet["type"] == "1" { 
                 cell = tableView.dequeueReusableCell(withIdentifier: "Cell1", for: indexPath)
             }
             else if responseSet["type"] == "2"
             {
                cell = tableView.dequeueReusableCell(withIdentifier: "Cell2", for: indexPath)
             }
             ..........
             ..........
    
             return cell
         }
    
Lineesh K Mohan
  • 1,702
  • 14
  • 18
0

First -

func numberOfSections(in tableView: UITableView) -> Int {
        return 2
   }

second -

func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {

        if (section == 0){
            return 1
        }
        else{return 0
        }
}

Third -

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {

        if indexPath.row == 0 && indexPath.section == 0 {
            let cell = tableView.dequeueReusableCell(withIdentifier:"ImageCell") as! ImageCell

            if strfeaturedImageUrl != nil
            {
             cell.CasinoImage.sd_setImage(with:URL(string: strfeaturedImageUrl!))
            }
           return cell
        }
        else{
            let cell = tableView.dequeueReusableCell(withIdentifier:"InformationCell") as! InformationCell

     return cell
}
}
Sangram Shivankar
  • 3,535
  • 3
  • 26
  • 38