ios - 在表格单元格中隐藏选择元素 - Swift

标签 ios swift uitableview

我按照这个应用程序构建了一个包含自定义单元格的表格:https://github.com/awseeley/Custom-Table-Cell

我正在尝试展开一个单元格并在单击该单元格时显示不同的内容。

这是我的 View Controller 中的内容:

var cellTapped:Bool = true
var currentRow = 0;

func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
    //CODE TO BE RUN ON CELL TOUCH

    let selectedRowIndex = indexPath
    currentRow = selectedRowIndex.row

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

    cell.image.hidden = true

    tableView.beginUpdates()
    tableView.endUpdates()

}



func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
    if indexPath.row == currentRow {
        if cellTapped == false {
            cellTapped = true

            return 141
        } else {
            cellTapped = false
            return 70
        }
    }
    return 70
}

单元格在单击时展开,再次单击时缩小。但图像并没有隐藏。如何让图像隐藏?有没有更好的方法来在单元格被选中并具有展开/折叠效果时显示另一个自定义 .xib 文件?

最佳答案

  1. 您应该将此工具添加到您的 cellForRowAtIndexPath

    func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
    
        if indexPath.row == currentRow {
            if cellTapped == false {
                cell.image.hidden = false
            } else {
                cell.image.hidden = true
            }
        }
    
        return cell
    }
    

并且 didSelectRowAtIndexPath 您应该删除错误的代码:

func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
    //CODE TO BE RUN ON CELL TOUCH

    let selectedRowIndex = indexPath
    currentRow = selectedRowIndex.row
    tableView.reloadData()

}
  1. 如果你想使用tableView.beginUpdate()来获得更好的动画效果,可以引用我的演示:

    Demo hide image on cell

希望对您有所帮助!

关于ios - 在表格单元格中隐藏选择元素 - Swift,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34602532/

相关文章:

ios - 为什么从标签栏 Controller 推送时我的 View Controller 是黑色的?

运行时快速字符串插值

ios - 如何使用分段控件在一个 TableView 中维护两个自定义单元格?

ios - 动态单元格高度自动布局 IOS

javascript - 在 cordova iOS 应用程序中返回页面时,webkit-playsinline 停止工作

ios - 生成字典函数的问题

iphone - didUpdateLocations 会在稳定手机上调用还是现在调用?

ios - 在Xcode 4.5中执行分析后,iOS应用程序内存泄漏

ios - 如何更改 Xcode 8 中选项卡栏图标的高亮颜色?

iphone - 如何在 Xcode 中以编程方式更改 UITableViewController 的样式