How to display only one cell at a time of UITableview

Honey Source

enter image description hereenter image description here

I'm fetching data from server with about 30 questions and wanted to display only one question at a time in tableviewcell which contains textboxes,labels and a next button. Upon clicking of next button it should display another cell with the next question. How is it possible

Any ideas/suggestions would be helpful

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

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "MyCell", for: indexPath as IndexPath)
    let dic=self.arrQues[Index] as! NSDictionary

lblQues.frame = CGRect(x: 20, y: 10, width: 280, height: height)
        lblQues.textAlignment = .left
        lblQues.text = dic["Question"] as? String
        lblQues.font = UIFont(name:"ArialMT", size: 20.0)
        lblQues.numberOfLines = 0
        lblQues.lineBreakMode = .byWordWrapping
        lblQues.textColor = UIColor.black
        cell.contentView.addSubview(lblQues)
  btnnext.setTitle("Next", for: [])
            btnnext.setTitleColor(UIColor.white,for: [])
            btnnext.backgroundColor = UIColor(red: 0/255, green: 124/255, blue: 223/255, alpha:1)
            btnnext.layer.cornerRadius = 5

            btnnext.frame = CGRect(x: lblQues.frame.origin.x, y:txtQuestion.frame.origin.y+txtQuestion.frame.size.height+30, width: 200, height: 40)
            btnnext.addTarget(self, action: #selector(buttonNextClicked), for: .touchUpInside)
            cell.contentView.addSubview(btnnext)
        return cell

} 


@objc func buttonNextClicked(sender: UIButton!) {

        Index=Index+1;

// display next question data 

}
iosiphoneswiftuitableviewxcode8

Answers

answered 1 month ago iVarun #1

You can do something like below:

Take two variables like below:

var count = 1
var rowCount: Int = 1 {
    willSet {
        if count != arrQues.count {
           count = newValue
            tblView.reloadData()
        }
    }
}

On click of Next button increase count of rowCount variable.

rowCount += 1

Use row count variable in cellForRowAt to get next question. replace Index variable with rowCount.

Hope this will help you.

answered 1 month ago Tm Goyani #2

let arrData = ["one","two","three","four"] // Your Array
var displayNo = 1  //numberOfCellDisplay

    //MARK:- TableView DataSource & Delegate ..............................
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int{
        return displayNo
    }
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell{
        let cell = tableView.dequeueReusableCell(withIdentifier: "MyCell")as! MyCell
        cell.title.text = arrData[indexPath.row]

        if (arrData.count) == displayNo {
            cell.btnNext.isHidden = true
        }else{
            if (indexPath.row) + 1 == displayNo {
                cell.btnNext.isHidden = false
                cell.btnNext.addTarget(self, action: #selector(addNextButtonClick), for: .touchUpInside)
            }else{
                cell.btnNext.isHidden = true
            }
        }
        return cell
    }
    @objc func addNextButtonClick(sender: UIButton){
        let indexPath = self.tableView.indexPathForRow(at: sender.convert(CGPoint.zero, to: self.tblView))
        if(arrData.count >= (indexPath?.row)! + 1){
            displayNo = displayNo + 1
            self.tableView.reloadData()
        }
    }

answered 1 month ago Nikunj Kumbhani #3

You need to Do Like this

class QuestionListVC: UIViewController, UITableViewDelegate, UITableViewDataSource {

    @IBOutlet weak var tblVw: UITableView!

    var arrLoginList = [String:Any]()

    override func viewDidLoad() {
        super.viewDidLoad()

        // For Display First Cell
        arrLoginList.updateValue("Cell 0", forKey: "0")
    }

    //MARK: - UITableView Delegate Methods

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

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

        let cell: ListCell = tableView.dequeueReusableCell(withIdentifier: "ListCell", for: indexPath ) as! ListCell
        cell.lblTitle.text = (arrLoginList["\(indexPath.row)"] as! String)
        cell.btnnext.tag = indexPath.row
        cell.btnnext.addTarget(self, action: #selector(buttonNextClicked), for: .touchUpInside)

        return cell
    }

    // For Insert New Cell tap on Next Button
    @objc func buttonNextClicked(sender: UIButton!) {

        arrLoginList.updateValue("Cell \(sender.tag + 1)", forKey: "\(sender.tag + 1)")

        if tblVw.cellForRow(at: IndexPath(row: arrLoginList.count-1, section: 0)) == nil{
            tblVw.insertRows(at: [IndexPath(row: arrLoginList.count-1, section: 0)], with: .automatic)
        }
    }

}

Output :

Initial Load

enter image description here

After tap next button 5 Times

enter image description here

comments powered by Disqus