swift:如何在两个 UITableViewCell 之间翻转单元格 [英] swift: How to Flip Cell between two UITableViewCell

查看:36
本文介绍了swift:如何在两个 UITableViewCell 之间翻转单元格的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 UITableViewController 中有两个 UITableViewCell.

我想在点击 didSelect 方法时翻转 indexPaths.

I want to flip indexPaths on click didSelect method.

当我点击 tableViewCellOne 中的 indextPath.row 0" 时,它会显示 tableViewCellTwo 的 indexPath.row 0".

When I will click indextPath.row "0" in tableViewCellOne it will show indexPath.row "0" of tableViewCellTwo.

现在它不能正确翻转.在 didSelect 方法上单击几次后也会使应用程序崩溃.

Now it is not flip properly. Also crash apps after click a few time on didSelect method.

错误信息是:

//Terifying app 由于未捕获的异常 'NSInternalInconsistencyException',原因:'尝试将多个单元格用于同一索引路径,这是不允许的.如果您确实需要出列比表视图请求的更多的单元格,请使用 -dequeueReusableCellWithIdentifier: 方法(不带索引路径).单元格标识符:FlipTableViewCellZero,索引路径:

//Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'Attempted to dequeue multiple cells for the same index path, which is not allowed. If you really need to dequeue more cells than the table view is requesting, use the -dequeueReusableCellWithIdentifier: method (without an index path). Cell identifier: FlipTableViewCellZero, index path:

var isOpen = true

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {

    if isOpen{

        let cell = tableView.dequeueReusableCell(withIdentifier: "FlipTableViewCellZero", for: indexPath) as! FlipTableViewCellZero

        var categoryObject = arrNewsList[indexPath.section]

        cell.profileImage = categoryObject[indexPath.row].textView
        return cell

    }else{

        let cell = tableView.dequeueReusableCell(withIdentifier: "FlipTableViewCellOne", for: indexPath) as! FlipTableViewCellOne

        var categoryObject = arrNewsList[indexPath.section]

        cell.newsTextView.text = categoryObject[indexPath.row].details

        return cell

    }
    return tableView.dequeueReusableCell(withIdentifier: "FlipTableViewCellZero", for: indexPath)

}


func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
    isOpen = true

    let selectedCell = tableView.dequeueReusableCell(withIdentifier: "FlipTableViewCellOne", for: indexPath) as! FlipTableViewCellOne
    UIView.transition(with: selectedCell, duration: 0.6, options: .transitionFlipFromLeft, animations: nil, completion: nil)
}

func tableView(_ tableView: UITableView, didDeselectRowAt indexPath: IndexPath) {
    isOpen = false

    let deselectedCell = tableView.dequeueReusableCell(withIdentifier: "FlipTableViewCellZero", for: indexPath) as! FlipTableViewCellZero
    UIView.transition(with: deselectedCell, duration: 0.6, options: .transitionFlipFromLeft, animations: nil, completion: nil)
}

推荐答案

您似乎希望以两种不同的方式显示您的类别:开放式和封闭式.

It looks like you want to display your category in two different ways: open and closed.

您遇到了问题,因为您试图通过将 didSelectRowAtdidDeselectRowAt 中的新单元格出列来更新显示.这是不正确的.

You're running into problems because you are trying to update the display by dequeueing a new cell in didSelectRowAt and didDeselectRowAt. This is incorrect.

更新显示的正确方法是调用:

The proper way to update the display is to call either:

  • tableView.reloadData,重新加载整个表格视图,或者
  • tableView.reloadRows(at:with:),它只重新加载指定的行
  • tableView.reloadData, which reloads the entire table view, or
  • tableView.reloadRows(at:with:), which reloads just the specified rows

为简单起见,我将在本例中使用第一个选项.

For simplicity, I'm going to use the first option in this example.

您的代码的另一个问题是您试图使用类级变量 isOpen 来记住您的类别的状态.当您拥有多个类别时,这将不起作用.

Another problem with your code is that you are trying to use a class-level variable isOpen to remember the state of your category. This isn't going to work when you have more than one category.

更好的方法是将 isOpen 添加为 categoryObject 类的属性.您最初将其设置为 false,然后在点击行时切换它.这是一个可能的解决方案:

A better approach is to add isOpen as a property of your categoryObject class. You set it initially to false and then toggle it when the row is tapped. Here is a possible solution:

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {

    let categoryObject = arrNewsList[indexPath.section]

    if categoryObject.isOpen {
        let cell = tableView.dequeueReusableCell(withIdentifier: "FlipTableViewCellZero", for: indexPath) as! FlipTableViewCellZero

        cell.profileImage = categoryObject[indexPath.row].textView
        return cell
    }
    else {
        let cell = tableView.dequeueReusableCell(withIdentifier: "FlipTableViewCellOne", for: indexPath) as! FlipTableViewCellOne

        cell.newsTextView.text = categoryObject[indexPath.row].details
        return cell
    }
}

func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {    
    let categoryObject = arrNewsList[indexPath.section]
    categoryObject.isOpen = true
    tableView.reloadData()
}

func tableView(_ tableView: UITableView, didDeselectRowAt indexPath: IndexPath) {
    let categoryObject = arrNewsList[indexPath.section]
    categoryObject.isOpen = false
    tableView.reloadData()
}

现在,这并没有为您提供您想要的动画,但它解决了您当前代码的核心问题.

Now, this doesn't give you the animation you wanted, but it addresses the core problem with your current code.

要制作动画,请查看文档以了解<代码>reloadRows(at:with:).使用此方法,而不是 reloadData(),您可以动画显示类别的显示变化.

To do the animation, check into the documentation for reloadRows(at:with:). Using this method, instead of reloadData(), you can animate the change in display of your category.

这篇关于swift:如何在两个 UITableViewCell 之间翻转单元格的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆