ios - 默认 UITableViewCell 状态

标签 ios uitableview swift xcode6

我创建了一个自定义的 UITableViewRowAction。因此,如果我在 UITableViewCell 上向左滑动,则会出现一个自定义按钮(例如,没有自定义按钮是删除按钮)。如果我按下这个自定义按钮,一个 UIView 出现,但按下的按钮仍然存在。我希望在按下此按钮后看到没有此按钮的 UITableViewCell。你们中有人知道如何在 Swift 中执行此操作并愿意帮助我吗?感谢您的回答。

截图:https://www.dropbox.com/s/8g04kcvswujsn71/stackQuestion.png?dl=0

最佳答案

我使用 SWTableViewCell在我自己的应用程序中实现这些操作。看看它,它很棒!

你可以像这样使用这个类:

<强>1。导入 SWTableViewCell 类

点击上面的链接或在 github 上搜索 SWTableViewCell。下载 zip(或使用 cocoa pod ,如果您熟悉的话)。

打开解压后的目录,找到PodsFile目录。将此目录的内容拖到您的项目中。这样做应该会导致 Xcode 要求创建桥接 header 。同意它然后添加

#import "SWTableViewCell.h"

到那个桥接头文件。如果你编译你会得到一些解析问题错误:预期类型。要解决这些问题,只需添加

#import <UIKit/UIKit.h>

到 NSMutableArray+SWUtilityButtons.h。现在我们准备好摇滚了。

<强>2。创建一个 SWTableViewCell 子类

好的,您可以按原样使用该单元格,但很可能您希望将单元格打扮得比简单的默认单元格外观更漂亮。如果是这样,请创建一个新的 cocoa touch 类(快速)并使您的单元格成为 SWTableViewCell 的子类。它应该看起来像这样:

import UIKit

class MySWCell: SWTableViewCell {

}

如果您正在使用 Storyboard,您可以在此类的 TableView 中制作您的单元格,连接任何导出/ Action 等。您需要做的所有可爱的事情来制作您需要的单元格。

<强>3。在 TableView 中使用您的子类

对于这个例子,我刚开始使用 Master-Detail 基础项目。您更改 cellForRowAtIndexPath 方法以使用新的自定义单元格:

override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCellWithIdentifier("Cell", forIndexPath: indexPath) as MySWCell

    let object = objects[indexPath.row] as NSDate
    cell.textLabel!.text = object.description  
    return cell
}

虽然这很棒,但您可能想要添加右/左实用按钮,这就是我们进行所有这些操作的原因:

<强>4。添加实用程序按钮

您可以在 cellForRowAtIndexPath 中执行此操作,但更愿意将其放在单独的函数中:

func getRightUtilityButtonsToCell()-> NSMutableArray{
    var utilityButtons: NSMutableArray = NSMutableArray()

    utilityButtons.sw_addUtilityButtonWithColor(UIColor.redColor(), title: NSLocalizedString("Delete", comment: ""))
    utilityButtons.sw_addUtilityButtonWithColor(UIColor.blueColor(), title: NSLocalizedString("Email", comment: ""))
    return utilityButtons
}

现在在您的 cellForRowAtIndexPath 中使用此方法:

cell.rightUtilityButtons = self.getRightUtilityButtons();

如果您要在单元格上向左滑动,您将有两个按钮:

What it should look like

但是,这些按钮的作用不大。我们需要遵守委托(delegate)。

<强>5。响应按钮

首先,告诉细胞你是它的代表。同样,在 cellForRowAtIndexPath 中添加这一行:

cell.delegate = self;

然后将类定义调整为:

class MasterViewController: UITableViewController, SWTableViewCellDelegate

MasterViewController 将替换为您处理 tableview 数据源/委托(delegate)的类的名称。

现在实现didTriggerRightUtilityButtonWithIndex 函数:

func swipeableTableViewCell(cell: SWTableViewCell!, didTriggerRightUtilityButtonWithIndex index: Int) {
    if index == 0 {
        println("delete button")
    }else {
        println("print button")
    }
}

现在你准备好了!您还可以使用 didTriggerRightUtilityButtonWithIndex 方法中的 hideUtilityButtonsAnimated 方法告诉单元格做一些很酷的事情,例如在选择一个按钮后隐藏按钮:

cell.hideUtilityButtonsAnimated(true);

此函数将在 tableview 滚动时隐藏单元格:

func swipeableTableViewCellShouldHideUtilityButtonsOnSwipe(cell: SWTableViewCell!) -> Bool {
    return true
}

玩得开心,这是一组很棒的类(class)!

关于ios - 默认 UITableViewCell 状态,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29435690/

相关文章:

ios - 在 `UISearchbar` 中搜索姓名或号码

Swift,是否可以有一个 IBInspectable block ?

string - (swift) 从字符串中删除 "\"字符?

ios - 获取具有最近日期的对象

ios - 尝试创建一个 Xcode Objective-C 函数来记录我的 UIView 内容的视频捕获并保存到手机

ios - Swift - 只允许在 UITableViewCell 中选中一个复选框

ios - 在 UITableViewCell 中设置 UIScrollview 的内容

swift - JSONDecoder 的原始类型的自定义初始化程序

ios - 将数据从自定义表格 View 单元格传递到 UIView Controller

ios - 如何在保持堆栈 View 存在的同时以图形方式隐藏 View ?