gpt4 book ai didi

ios - 尝试每 2 个 indexPath.row swift 添加一个部分

转载 作者:行者123 更新时间:2023-11-28 11:11:21 25 4
gpt4 key购买 nike

表格 View 有点问题。我想在不弄乱表中其他数据内容的情况下向 TableView 添加一个部分。我想在每 2 个 indexpath.row 之后添加 (cell3, thiscell)我所拥有的是 Storyboard上的一个部分,这是我想在每 2 个索引路径之后添加的一个单元格,而不会弄乱所有其他信息。我将如何使用插入部分功能?或者还有其他方法吗?

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
}

最佳答案

您想在所有部分或任何特定部分中显示 cell3 for indexPath.row = 2?

如果你想在所有部分显示:

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
// Table view cells are reused and should be dequeued using a cell identifier.

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 == 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.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 -> 2 的每个部分中添加新的单元格 cell3

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

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com