如何在一次UITableview中仅显示一个单元格

时间:2018-08-14 09:38:47

标签: ios iphone swift uitableview xcode8

enter image description here enter image description here

我正在从服务器中获取大约30个问题的数据,并希望一次仅在包含文本框,标签和下一个按钮的tableviewcell中显示一个问题。单击下一步按钮后,它应该显示另一个单元格,并带有下一个问题。

任何想法/建议都会有所帮助

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 

}

3 个答案:

答案 0 :(得分:1)

您可以执行以下操作:

采用两个变量,如下所示:

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

单击“下一步”按钮,将增加rowCount变量的计数。

rowCount += 1

使用cellForRowAt中的行计数变量来获取下一个问题。将Index变量替换为rowCount

希望这会对您有所帮助。

答案 1 :(得分:0)

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()
        }
    }

答案 2 :(得分:0)

您需要这样做

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)
        }
    }

}

输出:

初始负载

enter image description here

点击下一步按钮5次后

enter image description here