Reputation: 77
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.
Upvotes: 0
Views: 976
Reputation: 31
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
}
}
Upvotes: 0
Reputation: 1712
First receive the ResponseDataSet and call tableView.reloadData()
Then
The number of section delegate will return the number of section
func numberOfSections(in tableView: UITableView) -> Int {
return 1
}
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
}
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
}
Upvotes: 0
Reputation: 2034
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.
Upvotes: 1
Reputation:
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...
Upvotes: 0
Reputation: 1950
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.
Upvotes: 0