简体   繁体   English

将indexpath标记传递给tableview原型单元内的按钮

[英]Passing indexpath tag to button inside prototype cell in tableview

I have created a tableview prototype cell in storyboard and I have added a button to cell and set its tag to indexpath.row. 我在情节提要中创建了一个tableview原型单元,并向该单元添加了一个按钮,并将其标记设置为indexpath.row。 When I scroll my cells the scrolled cell on the top of tableview always set tag to zero instead of correct tag. 当我滚动单元格时,tableview顶部的滚动单元格始终将标记设置为零,而不是正确的标记。

public func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
        let cell : UITableViewCell = tableView.dequeueReusableCellWithIdentifier("autoLoadReuseIndentifier", forIndexPath: indexPath)

        print("indexpath :\(indexPath.row)")


        cell.contentView.viewWithTag(100)?.layer.cornerRadius = 5
        tableView.separatorStyle = UITableViewCellSeparatorStyle.None
        let tempDict : NSDictionary = savedCardsArray.objectAtIndex(indexPath.row) as! NSDictionary


        let bankName = cell.contentView.viewWithTag(102) as! UILabel

        deleteButton = cell.contentView.viewWithTag(106) as? UIButton
        deleteButton?.tag = indexPath.row
        deleteButton?.addTarget(self, action: "deleteCard1:", forControlEvents: UIControlEvents.TouchUpInside)
        print("delete button:\(deleteButton)")
       // print("indexpath delete tag :\(deleteButton.tag)")
        if(self.isSetUpAutoloadSelected){
            deleteButton?.hidden = true
        }else{
            deleteButton?.hidden = false
        }
        return cell;
    }

Whenever I scroll the cells, delete button tag is always set to zero. 每当我滚动单元格时,“删除”按钮标记始终设置为零。

If you should go with other way so use follow code and get indexPath. 如果您应该采用其他方法,请使用以下代码并获取indexPath。

func deleteCard1(_ sender:deleteCard) {
     let buttonPosition:CGPoint = sender.convert(CGPointZero, to:self.tableView)
     let indexPath = self.tableView.indexPathForRow(at: buttonPosition)
 }
  1. I think you don't need to follow this approach because firstly you set button tag statically in storyboard and again you are change it's tag in cellforrowatindexpath so when you scroll, cell will never find button with tag 106.If you want to follow this approach then you need to create customButton and add Variable of type NSInteger or whatever you want and store indexpath.row into that variable of customButton. 我认为您不需要遵循这种方法,因为首先您在情节提要中静态设置了按钮标签,然后再次在cellforrowatindexpath中更改了它的标签,因此当您滚动时,单元格将永远不会找到带有标签106的按钮。那么您需要创建customButton并添加NSInteger类型的变量或任何您想要的变量,并将indexpath.row存储到customButton的变量中。

  2. Another Approach is Create CustomTableViewCell Class and create button outlet in this custom Cell class and set indexpath.row into button tag like this 另一种方法是创建CustomTableViewCell类并在此自定义Cell类中创建按钮出口,然后将indexpath.row设置为如下所示的按钮标签

    CustomCellClassObject.buttonName.tag = indexPath.row CustomCellClassObject.buttonName.tag = indexPath.row

As Sumit said, it's better to use a custom cell and create outlet for the buttons and labels, as fetching sub views using tags will be tough to maintain the code in the future. 正如Sumit所说,最好使用自定义单元格并为按钮和标签创建出口,因为将来使用标签来获取子视图将很难维护代码。

Additionally, you don't have to create the variable deleteButton, as I don't see a valid purpose. 另外,您不必创建变量deleteButton,因为我没有看到有效的目的。

Assign tag to the button and add target in cellForRowAtIndexPath, it should work fine. 将标记分配给按钮并在cellForRowAtIndexPath中添加目标,它应该可以正常工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM