首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何编辑单元格的行?

如何编辑单元格的行?
EN

Stack Overflow用户
提问于 2017-07-06 15:47:44
回答 2查看 3.1K关注 0票数 1

我无法在单元格的第一行打开编辑模式。我尝试了这段代码,但它没有帮助。

代码语言:javascript
复制
   public func tableView(_ tableView: UITableView, canEditRowAt indexPath: IndexPath) -> Bool {
            if indexPath.row == 1{
                return true
            }
            return false
        }

有人能帮我吗?

EN

回答 2

Stack Overflow用户

发布于 2017-07-06 15:49:28

如果要编辑第一行,请替换

代码语言:javascript
复制
if indexPath.row == 1 

使用

代码语言:javascript
复制
if indexPath.row == 0 

因为indexPath.row从0开始,而不是从1开始。

希望这能有所帮助。

编辑:

因为您没有显示完整的代码,所以我在这里添加了示例代码。

检查以下代码:

代码语言:javascript
复制
import UIKit

class ViewController: UIViewController, UITableViewDataSource, UITableViewDelegate {

    @IBOutlet weak var tbl: UITableView!

    let arr = ["1", "2", "3"]

    override func viewDidLoad() {
        super.viewDidLoad()
        tbl.dataSource = self
        tbl.delegate = self
    }

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = self.tbl.dequeueReusableCell(withIdentifier: "cell")!

        cell.textLabel?.text = self.arr[indexPath.row]

        return cell
    }

    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return arr.count
    }

    func tableView(_ tableView: UITableView, canEditRowAt indexPath: IndexPath) -> Bool {

        //set 0 for first cell
        if indexPath.row == 0 {
            return true
        }
        return false
    }

    //Need this method for delete cell
    func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCellEditingStyle, forRowAt indexPath: IndexPath) {

        if editingStyle == UITableViewCellEditingStyle.delete {
            tbl.reloadData()
        }
    }
}
票数 0
EN

Stack Overflow用户

发布于 2017-07-06 16:00:18

首先,确保您添加了UITableViewDataSource协议。其次,您可能还需要以下实现。

代码语言:javascript
复制
func tableView(_ tableView: UITableView, editActionsForRowAt indexPath: IndexPath) -> [UITableViewRowAction]? {
    let myAction = UITableViewRowAction(style: .normal, title: "MY_ACTION") { (action, indexPath) in
        print("I'm here")
    }

    return [myAction]
}

func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCellEditingStyle, forRowAt indexPath: IndexPath) {

}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/44942787

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档