risa8
risa8

Reputation: 253

Adding two Custom cells in tableView

I have a tableView on mainStoryboard with two custom cells.

I would like to set two more cells at different row.

However When I implemented the code the added cells replaces original cells. (Custom cell of "Basic grammar3" and "Basic grammar5" are disappearing.)

I was trying to find the answer but could not find out.

I have image and code added below.

enter image description here

import UIKit
class HomeViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {

      @IBOutlet var tblStoryList: UITableView!      
      var array = PLIST.shared.mainArray

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

      func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
                return self.array.count + 1
            }

      func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
      if indexPath.row == 0 || indexPath.row == 3 || indexPath.row == 5 {
      let cell  = tableView.dequeueReusableCell(withIdentifier: "HeaderCell", for: indexPath) as! HeaderCell
     cell.headerTitle.text = indexPath.row == 0 ? "First Stage" : indexPath.row == 3 ? "Second Stage" : "Third Stage"
                    return cell
                }

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


                //making plist file
                let dict = self.array[indexPath.row - 1]
                let title = dict["title"] as! String
                let imageName = dict["image"] as! String
                let temp = dict["phrases"] as! [String:Any]
                let arr = temp["array"] as! [[String:Any]]
                let detail = "progress \(arr.count)/\(arr.count)"

                //property to plist file をつなぐ
                cell.imgIcon.image = UIImage.init(named: imageName)
                cell.lblTitle.text = title
                cell.lblSubtitle.text = detail

                cell.selectionStyle = UITableViewCellSelectionStyle.none


                return cell
            }

            func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
                if indexPath.row == 0 {
                    return
                }
                tableView.deselectRow(at: indexPath as IndexPath, animated:true)
                if indexPath.row == 3 {
                    return
                }
                tableView.deselectRow(at: indexPath as IndexPath, animated:true)
                if indexPath.row == 5 {
                    return
                }
                tableView.deselectRow(at: indexPath as IndexPath, animated:true)

                let messagesVc = self.storyboard?.instantiateViewController(withIdentifier: "SecondViewController") as! SecondViewController
                messagesVc.object = self.array[indexPath.row - 1]
                self.navigationController?.show(messagesVc, sender: self)
            }

Upvotes: 2

Views: 801

Answers (3)

Ravi
Ravi

Reputation: 2451

Try like this...

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

func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int 
{
  return numberOfRowsInCurrentStage
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell 
{
   return customizedCell                
}

func tableView(tableView: UITableView, heightForFooterInSection section: Int) -> CGFloat
{
  return requiredHeight
}

func tableView(tableView: UITableView, viewForFooterInSection section: Int) -> UIView?
{
  return stageCountView
}

You can use viewForHeaderInSection if you want to show stage count on top.

Upvotes: 1

kkakkurt
kkakkurt

Reputation: 2800

You could use sections for your table view. Now, you are returning 1 in your numberOfSections function. And it is creating only one section. If you want to use headers, you can use sections for your need. And also you can fill your table view cells with multidimendional arrays. For example:

For adjusting your section headers:

let lessonTitles = ["First Stage", "Second Stage"]

Titles for sections:

override func tableView(tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
    if section < lessonTitles.count {
        return lessonTitles [section]
    }
    return nil
}

For adjusting your sections and rows:

let lessons = [["Basic Grammar 1", "Basic Grammar 2"], ["Basic Grammar 3", "Basic Grammar 4"]]

Number of sections function should be:

override func numberOfSectionsInTableView(tableView: UITableView) -> Int {
    return lessons.count
}

Number of rows in section should be:

override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    return lessons[section].count
}

And creating your cells is like this:

override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
    let cellText = data[indexPath.section][indexPath.row]
    ...
}

Upvotes: 3

Gerriet
Gerriet

Reputation: 1350

edit: The comment by raki is the much better solution (use headers). I leave this here in case you want something closer to your existing implementation.

You have to change your numbering scheme in order to insert these additional rows (and not replace existing rows). So you might want to adjust the row for the "normal" elements like this:

func adjustRow(_ row: Int) -> Int {
   if row < 3 {
     return row
   } else if row < 5 {
     return row+1
   } else {
     return row+2
   }
}

Upvotes: 0

Related Questions