lpd
lpd

Reputation: 175

How to use two custom cell in UITableview

var mutipleArrayForFirstCell:[String] = ["1","2","3","4","5","6","7","8","9","10"]
var mutipleArrayForSecondCell:[String] = ["11","12","13","14","15","16","17","18","19","20"]

//MARK - TableView delegates
func numberOfSections(in tableView: UITableView) -> Int {
    return 2
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    if section == 1 {
        return mutipleArrayForFirstCell.count
    }else {
        return mutipleArrayForSecondCell.count
    }  
}

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

    if indexPath.row == 0 {
        let valueAt1stCell = tableView.dequeueReusableCell(withIdentifier: "FirstCell", for: indexPath) as! multipleCustomCellTableViewCell
        valueAt1stCell.Firstlabel.text =  mutipleArrayForFirstCell[indexPath.row]

        return valueAt1stCell

    } else {
        let valueAt2ndCell = tableView.dequeueReusableCell(withIdentifier: "SecondCell", for: indexPath) as! SecondCustomTableviewCell
        valueAt2ndCell.secondLabel.text = mutipleArrayForSecondCell[indexPath.row]
        return valueAt2ndCell
    }

}

func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
    return 100
}

My questions is i want to show the above declared two array value in two different cells. How can i do it?? it showing like this:

enter image description here

But it should show 1-10 in one cell and 11-20 in another cell.someone please help me ,Thanks in advance

enter image description here

how can i give some space between two cells ?

Upvotes: 0

Views: 82

Answers (2)

Naresh
Naresh

Reputation: 17932

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {        
    if indexPath.section == 0 {
        let valueAt1stCell = tableView.dequeueReusableCell(withIdentifier: "FirstCell", for: indexPath) as! multipleCustomCellTableViewCell
        valueAt1stCell.Firstlabel.text =  mutipleArrayForFirstCell[indexPath.row]
        return valueAt1stCell
    } else {
        let valueAt2ndCell = tableView.dequeueReusableCell(withIdentifier: "SecondCell", for: indexPath) as! SecondCustomTableviewCell
        valueAt2ndCell.secondLabel.text = mutipleArrayForSecondCell[indexPath.row]
        return valueAt2ndCell
    }
}

Upvotes: 1

Manish Mahajan
Manish Mahajan

Reputation: 2092

Try this one

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

switch indexPath.section {
    case 0:
        //First Array
      let valueAt1stCell = tableView.dequeueReusableCell(withIdentifier: "FirstCell", for: indexPath) as! multipleCustomCellTableViewCell
    valueAt1stCell.Firstlabel.text =  mutipleArrayForFirstCell[indexPath.row]

    return valueAt1stCell

    default:
        //Second Array
     let valueAt2ndCell = tableView.dequeueReusableCell(withIdentifier: "SecondCell", for: indexPath) as! SecondCustomTableviewCell
    valueAt2ndCell.secondLabel.text = mutipleArrayForSecondCell[indexPath.row]
    return valueAt2ndCell

    } 
}

Upvotes: 1

Related Questions