滑动删除时如何让tableViewCell不展开

时间:2016-06-27 03:40:03

标签: ios uitableview swift2 xcode7

我已经设置了我的应用程序,以便我可以将一个单元格添加到ViewController,其中单元格出现在tableView中,但是当您单击单元格时,它会展开以显示更多信息。这是代码......

class ViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {

var selectedIndexPath: NSIndexPath? = nil

override func viewDidLoad() {
    super.viewDidLoad()

    tableView.registerNib(UINib(nibName: "CustomViewCell", bundle: nil), forCellReuseIdentifier: "Cell")

}

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {

    let cell = self.tableView.dequeueReusableCellWithIdentifier("Cell", forIndexPath: indexPath) as! CustomViewCell

    cell.clipsToBounds = true

    return cell

}

func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {

    if selectedIndexPath != nil {

        if indexPath == selectedIndexPath {
            return 140
        } else {
            return 38
        }

    } else {
        return 38
    }

}

func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
     // New
    switch selectedIndexPath {
    case nil:
        selectedIndexPath = indexPath
    default:
        if selectedIndexPath! == indexPath {
            selectedIndexPath = nil
        } else {
            selectedIndexPath = indexPath
        }
    }

    tableView.reloadRowsAtIndexPaths([indexPath], withRowAnimation: .Automatic)

}

除了我将单元格配置为滑动以删除之外,所有内容都可以正常扩展和关闭单元格,但是当我滑动删除时单元格只会展开。下面是代码:

func tableView(tableView: UITableView, editActionsForRowAtIndexPath indexPath: NSIndexPath) -> [UITableViewRowAction]? {


    let deleteAction = UITableViewRowAction(style: UITableViewRowActionStyle.Default, title: "Delete") { (action , indexPath ) -> Void in
        self.editing = false     
        let appDelegate = UIApplication.sharedApplication().delegate as! AppDelegate
        let moc = appDelegate.managedObjectContext
        moc.deleteObject(self.events[indexPath.row])
        appDelegate.saveContext()             
        self.events.removeAtIndex(indexPath.row)
        tableView.reloadData()

    }

    return [deleteAction]

}

Before swipe to delete

After swipe to delete

我不希望单元格像我刷卡删除时那样扩展,有什么想法吗?

1 个答案:

答案 0 :(得分:1)

我认为可能是正在扩展细胞的人。你在说:

func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
    if indexPath == selectedIndexPath {
        return 140
    }
}

在滑动删除期间是否调用了该方法?使用调试来查找。如果是,您需要重写该代码,以便在您轻扫以删除时,您不会返回140.