Ranjan Pathak
Ranjan Pathak

Reputation: 1

I am trying to create multiple custom cells, with different classes

extension DashboardView: UITableViewDataSource, UITableViewDelegate {
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return recents.count

    }


func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    return contents.count
}


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

    let recent = recents[indexPath.row]
    let cell = tableView.dequeueReusableCell(withIdentifier: "RecentCell") as! RecentCell

    cell .setRecent(recent: recent)


    cell.delegate = self

    return cell
}

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

        let content = contents[indexPath.row]
        let cell = tableView.dequeueReusableCell(withIdentifier: "ContentCell") as! ContentCell

        cell .setContent(content: content)


        cell.delegate = self

        return cell
    }
}

I have tried creating other extensions but does not work

Upvotes: 0

Views: 72

Answers (4)

Ranjan Pathak
Ranjan Pathak

Reputation: 1

I am seeing only one value by applying this one method

extension DashboardView: UITableViewDataSource, UITableViewDelegate {

    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return recents.count
//      return contents.count
    }

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

        let recent = recents[indexPath.row]
        let content = contents[indexPath.row]

        if indexPath.row == 0 {
            let cell = tableView.dequeueReusableCell(withIdentifier: "ContentCell") as! ContentCell
            cell.setContent(content: content)
            cell.delegate = self as? ContentCellDelegate
            return cell
        }
        else {
            let cell = tableView.dequeueReusableCell(withIdentifier: "RecentCell") as! RecentCell
            cell.setRecent(recent: recent)
            cell.delegate = self
            return cell
        }        
    }

}

enter image description here

The value you see in the screenshot is only the value of ContentCell

Upvotes: 0

Hilalkah
Hilalkah

Reputation: 1035

If you want to see two array you can add both in numberOfRowsInSection function. But don't forget the indexes are can change in cellForRowAt function. I assume recents count is 1.

extension DashboardView: UITableViewDataSource, UITableViewDelegate {

    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return recents.count + contents.count
    }

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

        if indexPath.row == 0 {
            let recent = recents[indexPath.row]

            let cell = tableView.dequeueReusableCell(withIdentifier: "ContentCell") as! ContentCell
            cell.setContent(content: content)
            cell.delegate = self as? ContentCellDelegate
            return cell
        }
        else {
            let content = contents[indexPath.row - 1]

            let cell = tableView.dequeueReusableCell(withIdentifier: "RecentCell") as! RecentCell
            cell.setRecent(recent: recent)
            cell.delegate = self
            return cell
        }        
    }

}

Upvotes: 0

ishwar lal janwa
ishwar lal janwa

Reputation: 524

You need to return cell according your requirement like below.

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


    let info = arrOffers[indexPath.row]

    switch info.valueForString(key: "type") {

    case "coupon":

        if let cell = tableView.dequeueReusableCell(withIdentifier: "OfferCouponTblCell") as? OfferCouponTblCell {

            cell.configure(info)
            return cell
        }            

    default:

        if let cell = tableView.dequeueReusableCell(withIdentifier: "PaymentOfferTblCell") as? PaymentOfferTblCell {
            cell.configure(info)                           
            return cell
        }
    }        

}

Upvotes: 1

Shehata Gamal
Shehata Gamal

Reputation: 100503

I have tried creating other extensions but does not work

You need to change cellForRowAt e.x for first cell with different layout use

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

    let recent = recents[indexPath.row]

    if indexPath.row == 0 {
      let cell = tableView.dequeueReusableCell(withIdentifier: "RecentCell1") as! RecentCell1
      cell .setRecent(recent: recent) 
      cell.delegate = self 
      return cell
   }
   else {
      let cell = tableView.dequeueReusableCell(withIdentifier: "RecentCell2") as! RecentCell2
      cell .setRecent(recent: recent) 
      cell.delegate = self 
      return cell
   }

}

Upvotes: 1

Related Questions