在单元格内单击时找出 UITableView 中属于哪个部分按钮
Find Out Which Section Button Belongs To In UITableView When Clicked Inside Cell
我正在尝试根据单击按钮的哪个部分更改数组元素的值。例如,假设我有这个数组 numbers = [0,0,0,0,0] 并且我想要将第一个元素更改为 5。我将 5 插入到第一部分的单元格中,然后在同一部分中单击“完成”,数组现在将显示为 [5,0,0,0,0]。有没有办法知道按钮属于哪个部分?
现在我有两个独立的 classes。一个用于自定义单元格,一个用于 table 视图。两者都有一个按钮的出口。单击该按钮时,自定义单元格 class 会将临时全局编号更改为插入的编号。在 table class 中,我希望按钮操作采用该全局编号并插入到与按钮所属部分编号相同的元素中。除了我不知道如何找出它属于哪个部分。
谁能帮我解决这个问题?我正在写 Swift 顺便说一句。
如果你想找到哪个单元格被 'clicked' 你可以使用 UITableViewDelegate
方法。
func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
}
如果您只需要包含点击按钮的单元格索引路径的部分(而不是部分和行),则可以在配置时用部分编号(Int)标记按钮 table 查看单元格:
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell
{
let cell = tableView.dequeueReusableCellWithIdentifier("MyIdentifier", forIndexPath:indexPath) as! MyCustomTableViewCell
cell.myCustomButton.tag = indexPath.section
// Remove all existing targets (in case cell is being recycled)
cell.myCustomButton.removeTarget(nil, action: nil, forControlEvents: .AllEvents)
// Add target
cell.myCustomButton.addTarget(self, action:"buttonAction:", forControlEvents:.TouchUpInside)
return cell
}
func buttonAction(sender:AnyObject)
{
if let button = sender as UIButton{
println("Tapped Button in section: \(button.tag)")
}
}
如果您同时需要 部分和 行,最好存储整个索引路径。为此,您可以使用带有类型 NSIndexPath
的 属性 的自定义 UIButton
子类,或者将其存储在 table 视图单元格中。
但是第二个解决方案有点混乱,因为您必须使用 superview
等从按钮访问 table 单元格(然后是索引路径),这依赖于 table 单元格的子视图结构。我不确定你的按钮的父视图是 table 单元格本身,还是它的 "content view"(我现在有点生疏...)
我正在尝试根据单击按钮的哪个部分更改数组元素的值。例如,假设我有这个数组 numbers = [0,0,0,0,0] 并且我想要将第一个元素更改为 5。我将 5 插入到第一部分的单元格中,然后在同一部分中单击“完成”,数组现在将显示为 [5,0,0,0,0]。有没有办法知道按钮属于哪个部分?
现在我有两个独立的 classes。一个用于自定义单元格,一个用于 table 视图。两者都有一个按钮的出口。单击该按钮时,自定义单元格 class 会将临时全局编号更改为插入的编号。在 table class 中,我希望按钮操作采用该全局编号并插入到与按钮所属部分编号相同的元素中。除了我不知道如何找出它属于哪个部分。
谁能帮我解决这个问题?我正在写 Swift 顺便说一句。
如果你想找到哪个单元格被 'clicked' 你可以使用 UITableViewDelegate
方法。
func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
}
如果您只需要包含点击按钮的单元格索引路径的部分(而不是部分和行),则可以在配置时用部分编号(Int)标记按钮 table 查看单元格:
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell
{
let cell = tableView.dequeueReusableCellWithIdentifier("MyIdentifier", forIndexPath:indexPath) as! MyCustomTableViewCell
cell.myCustomButton.tag = indexPath.section
// Remove all existing targets (in case cell is being recycled)
cell.myCustomButton.removeTarget(nil, action: nil, forControlEvents: .AllEvents)
// Add target
cell.myCustomButton.addTarget(self, action:"buttonAction:", forControlEvents:.TouchUpInside)
return cell
}
func buttonAction(sender:AnyObject)
{
if let button = sender as UIButton{
println("Tapped Button in section: \(button.tag)")
}
}
如果您同时需要 部分和 行,最好存储整个索引路径。为此,您可以使用带有类型 NSIndexPath
的 属性 的自定义 UIButton
子类,或者将其存储在 table 视图单元格中。
但是第二个解决方案有点混乱,因为您必须使用 superview
等从按钮访问 table 单元格(然后是索引路径),这依赖于 table 单元格的子视图结构。我不确定你的按钮的父视图是 table 单元格本身,还是它的 "content view"(我现在有点生疏...)