对tableviews有点问题。我想在tableview中添加一个节,而不需要修改表中其他数据的内容。我想在每2 indexpath.row之后添加(cell3,thiscell)
我所拥有的是故事板上的一个部分,它是一个单元格,我想在每2个索引路径后添加它,而不需要处理所有其他信息。如何使用insert section函数?或者还有别的办法吗?

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
    // Table view cells are reused and should be dequeued using a cell identifier.
    if indexPath.section == 0 && cell1 == true{
        let cellIdentifier = “cell1"
        let cell = tableView.dequeueReusableCellWithIdentifier(cellIdentifier, forIndexPath: indexPath) as! SubCategoryTableViewCell
        cell.nameLabel.text = “cell1"
        cell.subNameLabel.text = ""
        return cell
    } else  if indexPath.section == 2 && vally == true{
        let cellIdentifier = "cell2"
        let cell = tableView.dequeueReusableCellWithIdentifier(cellIdentifier, forIndexPath: indexPath) as! SubCategoryTableViewCell
        cell.nameLabel.text = “Smiles"
        cell.subNameLabel.text = ""
        return cell
    } else if indexPath.row == 2 {
        let cellIdentifier = “cell3"
        let cell = tableView.dequeueReusableCellWithIdentifier(cellIdentifier, forIndexPath: indexPath) as! thisCell
        print("add this after indexpath 2")
        return cell
    } else if indexPath.section == 1 {
        let cellIdentifier = "SubCategoryTableViewCell"
        let cell = tableView.dequeueReusableCellWithIdentifier(cellIdentifier, forIndexPath: indexPath) as! SubCategoryTableViewCell
        cell.loading.hidden = false
        cell.loading.startAnimating()
        cell.nameLabel.text = subCat[indexPath.row].subCategoryName
        cell.subNameLabel.text = subCat[indexPath.row].subCategoryDesc
        return cell
    }
    let cell2: SubCategoryTableViewCell = tableView.dequeueReusableCellWithIdentifier("SubCategoryTableViewCell", forIndexPath: indexPath) as! SubCategoryTableViewCell
    cell2.userInteractionEnabled = false
    return cell2
}

最佳答案

indexPath.row值用于匹配基础支持数据数组的索引。用额外的“虚拟”单元格代替行并不会增加它们,它只是在相应的行上显示不同的单元格。
你有两个选择:
1)考虑“中间”单元格在subct[row]和IndexPath.row中创建的递增偏移量。
2)在每个节中使用实际节、段和mat项对indexPath.row 0和1。
对于#1,可以创建一对映射函数,并使用它们将indexPath.row与subCat[]中的索引进行转换

func indexFromRow(row:int) -> int?
{ return row % 3 == 2 ? nil : row - row/3 }

func rowFromIndex(index:Int) -> Int
{ return index + index/2 }

然后,在返回单元格时,可以使用函数来建立行和索引之间的对应关系:
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell
{
  if let subCatIndex = indexFromRow(indexPath.row)
  {
     // build a SubCategoryTableViewCell cell, feed it with subCat[subCatIndex] data and return it
  }
  else
  {
     // build a thisCell cell and return it
  }
}

您还需要调整为节返回的行数,以便为中间单元格腾出空间
 func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int
 { return rowForIndex(subCat.count-1) + 1 }

在代码中接收IndexPath并需要知道subCat[]中的索引的任何地方,都需要使用rowToIndex()函数
当您想知道给定索引处subCat[]中某个项的索引路径时,必须使用indexToRow()来构建index path
对于#2,您必须使用类似的技术在subCat[]中映射indexPath.row和索引,但是您还必须处理节,我认为您不能对节头使用可重用单元格。

关于ios - 尝试每2 indexPath.row swift添加一个节,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/34865148/

10-14 20:36
查看更多