【问题标题】:UITableViewController insert different cells into different sectionsUITableViewController 将不同的单元格插入不同的部分
【发布时间】:2016-12-21 10:04:19
【问题描述】:

我有一个 TableViewController,它有 3 个部分和它们自己的标题。 现在我想在插入任何单元格之前,检查一个属性,然后将单元格添加到不同的部分。

    override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    // Table view cells are reused and should be dequeued using a cell identifier.
    let cellIdentifier = "TasksTableViewCell"


    guard let cell = tableView.dequeueReusableCell(withIdentifier: cellIdentifier, for: indexPath) as? TasksTableViewCell  else {
        fatalError("The dequeued cell is not an instance of TasksTableViewCell.")
    }

    // Fetches the appropriate task for the data source layout.
    let task = tasks[indexPath.row]

    cell.nameLabel.text = task.name
    cell.photoImageView.image = task.photo
    cell.timeControl.text = task.lengthDisplay.replacingOccurrences(of: "Length: ", with: "")

    if(task.importanceLevel == 0){
         // add cell to section 0
    }
    else if(task.importanceLevel == 1){
         // add cell to section 1
    }

    // Configure the cell...

    return cell
}

你能看到评论吗,有什么办法吗? 非常感谢

【问题讨论】:

标签: swift uitableview


【解决方案1】:

您可以首先为数据源方法中的每个部分和行创建一个模型并传递空计数数组。 然后初始化计数并填充数组,然后重新加载您的表格视图。 我希望你也知道还有 numberOfSections 方法。

【讨论】:

    【解决方案2】:

    我认为这段代码会对你有所帮助:

    let arr = [5,2,7,10,2]
    override func viewDidLoad() {
        super.viewDidLoad()
    }
    // MARK: - Table view data source
    override func numberOfSectionsInTableView(tableView: UITableView) -> Int {
        // #warning Incomplete implementation, return the number of sections
        return 5
    }
    override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        // #warning Incomplete implementation, return the number of rows
        return arr[section]
    }
    override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCellWithIdentifier("celldata", forIndexPath: indexPath)
    
        cell.textLabel?.text = "section \(indexPath.section)"
        cell.detailTextLabel?.text = "Rows \(indexPath.row)"
    
        return cell
    }
    override func tableView(tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
        let view = UIView(frame: CGRect(x: 0, y: 0, width: self.tableView.frame.width, height: 20))
        let lbl = UILabel(frame: CGRect(x: 5, y: 5, width: self.tableView.frame.width, height: 15))
        lbl.text = "\(section)"
        view.backgroundColor = UIColor.grayColor()
        view.addSubview(lbl)
        return view
    }
    

    【讨论】:

      【解决方案3】:

      你可以这样试试

      override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
      
      
          if(indexPath.section == 0){
              let cellIdentifier = "TasksTableViewCell"
      
              guard let cell = tableView.dequeueReusableCell(withIdentifier: cellIdentifier, for: indexPath) as? TasksTableViewCell  else {
                  fatalError("The dequeued cell is not an instance of TasksTableViewCell.")
              }
      
           // Fetches the appropriate task for the data source layout.
      
              let task = tasks[indexPath.row]
              cell.nameLabel.text = task.name
              cell.photoImageView.image = task.photo
              cell.timeControl.text = task.lengthDisplay.replacingOccurrences(of: "Length: ", with: "")
      
              return cell
          }
          else if(indexPath.section == 1)
              let cellIdentifier = "TasksTableViewCell1"
      
              guard let cell = tableView.dequeueReusableCell(withIdentifier: cellIdentifier, for: indexPath) as? TasksTableViewCell1  else {
                  fatalError("The dequeued cell is not an instance of TasksTableViewCell.")
              }
      
           // Fetches the appropriate task for the data source layout.
              let task = tasks[indexPath.row]
              cell.nameLabel.text = task.name
              cell.photoImageView.image = task.photo
              cell.timeControl.text = task.lengthDisplay.replacingOccurrences(of: "Length: ", with: "")
      
              return cell
          }
      }
      

      【讨论】:

      • 感谢您的帮助。但是你知道 task.importanceLevel 只有在 let task = tasks[indexPath.row] 之后才能访问,对吗?而且我没有 TasksTableViewCell1,我只有 3 个不同的部分,我想将单元格添加到
      • @QuangDinhLuong 我已经更新了代码。您可以根据当前活动部分加载单元格
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-01-12
      相关资源
      最近更新 更多