如何在不使用tag的情况下更改tableviewCell中的图像按钮

时间:2015-08-13 08:36:03

标签: ios iphone swift uitableview

我在tableview单元格中有一个按钮。我希望最初按钮有一个图像" A"当用户点击它时,它会变为" B"当用户再次点击它时它会变回&# 34; A"

让这两张照片成为" A"和" B"在这种情况下 我无法使用tag因为我使用它来确定用户使用indexPath.row点击了哪个按钮

下面是我的按钮功能的当前状态。

@IBAction func setNotification(sender: UIButton!) {

        cell = tableView.cellForRowAtIndexPath(NSIndexPath(forRow: sender.tag, inSection: 0)) as! CustomTableViewCell

        sender.setImage(UIImage(named: "B")!, forState: .Normal)
currentRegion = CLCircularRegion(center: CLLocationCoordinate2D(latitude: latitude ,longitude:longitude), radius: 10, identifier: "xyz")

这也是我cellForRowAtIndexPath函数的外观

cell.notifybutton.addTarget(self, action: "setNotification:", forControlEvents: UIControlEvents.TouchUpInside)
        cell.notifybutton.setImage(UIImage(named: "A"), forState: UIControlState.Normal)
        cell.notifybutton.tag = indexPath.row

3 个答案:

答案 0 :(得分:7)

您不使用标记更改按钮图像,请遵循此代码。

您的cellForRowAtIndexPath函数为不同的状态设置了两个按钮图像

cell.notifybutton.setImage(UIImage(named: "A"), forState: UIControlState.Normal)
cell.notifybutton.setImage(UIImage(named: "B"), forState: UIControlState.Selected)
cell.notifybutton.selected = false //default A image display

按下按钮的方法

@IBAction func setNotification(sender: UIButton!) {
    if (sender.selected)
    {
         sender.selected = false
    }
    else
   {
        sender.selected = true
   }
}

答案 1 :(得分:2)

试试这个:

你应该使用以下代码

<services>
  <service name="WorksWebService.WorksWebService">
    <endpoint address="" behaviorConfiguration="restful" binding="webHttpBinding" bindingConfiguration="HttpBinding"
      contract="WorksWebService.IWorksWebService" />
    <endpoint address="" behaviorConfiguration="restful" binding="webHttpBinding" bindingConfiguration="HttpsBinding"
      contract="WorksWebService.IWorksWebService" />
  </service>
</services>

答案 2 :(得分:0)

cell.notifybutton.setImage(UIImage(named:&#34; A&#34;),forState:UIControlState.Normal) cell.notifybutton.setImage(UIImage(named:&#34; B&#34;),forState:UIControlState.Selected) cell.notifybutton.selected = false //默认图像显示