当前位置: 首页 > 面试题库 >

如何使用按钮标签快速访问自定义单元格的内容?

田永春
2023-03-14
问题内容

我有一个在自定义单元格中具有自定义按钮的应用程序。如果选择该单元格,它将显示到详细信息视图,这是完美的。如果选择单元格中的按钮,则下面的代码将单元格索引打印到控制台中。

我需要访问所选单元格的内容(使用按钮)并将其添加到数组或字典中。我对此很陌生,因此很难找到如何访问单元格的内容。我尝试使用didselectrowatindexpath,但是我不知道如何将索引强制为标签的索引…

因此,基本上,如果每个单元格中有3个单元格带有“ Dog”,“ Cat”,“
Bird”作为cell.repeatLabel.text,并且我选择了第1行和第3行(索引0和2)中的按钮,将“狗”和“鸟”添加到数组/字典中。

    // MARK: - Table View

override func numberOfSectionsInTableView(tableView: UITableView) -> Int {
    return 1
}

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

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

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

    // Configure the cell...
    var currentRepeat = postsCollection[indexPath.row]
    cell.repeatLabel?.text = currentRepeat.product
    cell.repeatCount?.text = "Repeat: " + String(currentRepeat.currentrepeat) + " of " + String(currentRepeat.totalrepeat)

    cell.accessoryType = UITableViewCellAccessoryType.DetailDisclosureButton

    cell.checkButton.tag = indexPath.row;

    cell.checkButton.addTarget(self, action: Selector("selectItem:"), forControlEvents: UIControlEvents.TouchUpInside)


    return cell

}

func selectItem(sender:UIButton){

    println("Selected item in row \(sender.tag)")

 }

问题答案:

选项1.委托一起 处理 __

处理从单元的子视图触发的事件的正确方法是使用 委托

因此,您可以按照以下步骤操作:

1. 在类定义上方,使用自定义单元格中的单个实例方法编写一个协议

protocol CustomCellDelegate {
    func cellButtonTapped(cell: CustomCell)
}

2. 在类定义中声明一个委托变量,并在委托上调用protocol方法:

var delegate: CustomCellDelegate?

@IBAction func buttonTapped(sender: AnyObject) {
    delegate?.cellButtonTapped(self)
}

3. 遵循表视图所在的类中的CustomCellDelegate:

 class ViewController: CustomCellDelegate

4. 设置您的单元格的代表

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

    return cell
}

5. 在视图控制器类中实现所需的方法。

编辑: 首先定义一个空数组,然后像这样修改它:

private var selectedItems = [String]()

func cellButtonTapped(cell: CustomCell) {
    let indexPath = self.tableView.indexPathForRowAtPoint(cell.center)!
    let selectedItem = items[indexPath.row]

    if let selectedItemIndex = find(selectedItems, selectedItem) {
        selectedItems.removeAtIndex(selectedItemIndex)
    } else {
        selectedItems.append(selectedItem)
    }
}

其中 items 是在我的视图控制器中定义的数组:

private let items = ["Dog", "Cat", "Elephant", "Fox", "Ant", "Dolphin", "Donkey", "Horse", "Frog", "Cow", "Goose", "Turtle", "Sheep"]

选项2. 使用 闭包 处理 __

我决定回来,向您展示另一种处理这类情况的方法。在这种情况下使用闭包将减少代码量,您将实现目标。

1. 在单元格类中声明一个闭包变量:

var tapped: ((CustomCell) -> Void)?

2. 调用按钮处理程序中的闭包。

@IBAction func buttonTapped(sender: AnyObject) {
    tapped?(self)
}

3.tableView(_:cellForRowAtIndexPath:)包含视图控制器的类中:

let cell = tableView.dequeueReusableCellWithIdentifier("Cell", forIndexPath: indexPath) as! CustomCell       
cell.tapped = { [unowned self] (selectedCell) -> Void in
    let path = tableView.indexPathForRowAtPoint(selectedCell.center)!
    let selectedItem = self.items[path.row]

    println("the selected item is \(selectedItem)")
}


 类似资料:
  • 问题内容: 我有一个带有几个IBOutlet的自定义单元类。我已经将课程添加到情节提要中。我已连接所有网点。我的cellForRowAtIndexPath函数看起来像这样: 这是我的自定义单元格类: 当我运行该应用程序时,我所有的单元格都为空。我已经注销,它包含所有正确的字符串。我也尝试过将等于标签的实际字符串放入,并产生相同的结果。 我想念什么?任何帮助表示赞赏。 问题答案: 感谢所有不同的建议

  • 我有一个FXML文档,其中包含我的JavaFX项目的视觉基础,我想通过使用一个窗格来创建一个自己的Topbar(X、最小值/最大值等等)。但我的程序将有多个页面(场景),为了保持代码干净,我想将自定义Topbar作为一个单独的类(有点像组件对象)。我只是不知道该如何在我使用的FXML基础上实现这个类(我使用的是Scene Builder)。

  • 问题内容: 在我的应用程序中,我有一个表格视图,每个单元格中都有一个图像,标签和文本视图。我希望能够根据文本视图中的内容量自动调整单元格的大小。(文本视图是最下面的文本。) 到目前为止,我已经添加了正确的约束。文本视图的前导,尾随,顶部和底部,并且已禁用滚动和编辑。 在我的tableViewController.swift文件中,我编写了以下代码: 但是,这不起作用,因为当我在文本视图中添加更多文

  • 我对Android开发非常陌生,这是我的第一个Android应用程序。 下面是我的MainActivity布局: 这是MainActivity的组件树

  • 问题的核心是,我不能刷新或更改一个场景的节点的内容(这里是TablesMain)从另一个类(这里是NamePriceCell)。 我正在使用主StackPane(TableMainController扩展StackPane)构建和应用程序,其中包含其他节点,其中一些节点是ListView。在一个特定的ListView(比如“readitemslistview”)中,我创建了一个自定义ListCel

  • 问题内容: 如何在flutter中创建这样的自定义单选按钮组 问题答案: 这是完整的代码 To use :