zl程序教程

您现在的位置是:首页 >  移动开发

当前栏目

iOS swift tableView插入cell

ios 插入 swift cell tableView
2023-09-14 09:04:14 时间

注意:count(返回的row数)要加1,在返回cell的代理方法中要返回你要插入的cell

        let indexPath1:IndexPath = IndexPath.init(row: 1, section: 0)
        tableView.beginUpdates()
        count += 1
        tableView.insertRows(at: [indexPath1], with: .fade)
        tableView.endUpdates()
var count = 4

func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        if section == 0 {
            return count
        } else {
            return 2
        }
    }

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
         if indexPath.section == 0 && indexPath.row == 1 {
            if count == 5 {
                let cell3:settingCell3 = tableView.dequeueReusableCell(withIdentifier: "cell3", for: indexPath) as! settingCell3
                return cell3
            }
            let cell2: settingCell2 = tableView.dequeueReusableCell(withIdentifier: "cell2", for: indexPath) as! settingCell2
            cell2.titleLabel.text = NSLocalizedString("softwareSecurity", comment: "")
            return cell2
        }
    }

参考博客:
UITableView 动态增加删除行