【问题标题】:How to display only one cell at a time of UITableviewUITableview如何一次只显示一个单元格
【发布时间】:2018-08-14 09:38:47
【问题描述】:

我正在从服务器获取大约 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 

}

【问题讨论】:

  • CollectionView 分页可以满足您的要求。
  • 你能提供设计的截图吗?
  • 您可以使用全宽容器视图和UIButton点击您需要scrollToRow查看下一个问题的水平集合视图
  • 为什么这里需要表格视图?你可以展示一下 ui 设计吗?
  • 当按钮被点击时,使用 UITableView.numberOfRows 和 UITableView.cellForRow 浏览 tableView。对于每个单元格,除了您需要的单元格之外,将 contentView.isHidden 设置为 true。

标签: ios iphone swift uitableview xcode8


【解决方案1】:

您可以执行以下操作:

取两个变量,如下所示:

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

点击下一步按钮增加rowCount变量的计数。

rowCount += 1

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

希望这会对你有所帮助。

【讨论】:

  • 在哪里写这个var count = 1 var rowCount: Int = 1 { willSet { if count != arrItems.count { count = newValue tblView.reloadData() } } }
  • @Honey 就在您的网点下方。
  • 不,它不能正常工作。第二个问题显示相同的问题两次,第三个问题显示三次等。我只需要一次只显示一个问题
  • @Honey 你的问题数组在哪里?请添加代码
  • Questionarray 是来自 server.plz 检查的键和值的组合
【解决方案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()
        }
    }

【讨论】:

    【解决方案3】:

    你需要这样做

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

    输出:

    初始加载

    点击下一个按钮 5 次后

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-07-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多