标签 = 0 的按钮不起作用

Button with tag = 0 is not working

我的问题是标签=0 的按钮..所有按钮都工作正常,当我点击任何按钮时,以前的拇指颜色变为默认(白色),但如果我点击标签=0 的按钮然后当我点击下一个按钮时,标签为 0 的按钮仍然是红色并且没有更改为默认值..然后整个功能无法正常工作

这是函数 任何帮助pleeeeease

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

    var cell = myTable.dequeueReusableCellWithIdentifier("toWork") as UITableViewCell
    cell.textLabel?.text = Berufs[indexPath.row]
    var btn:UIButton = UIButton(frame: CGRectMake(10, 3, 40, 40))
    btn.tag = indexPath.row
    btn.backgroundColor = UIColor.whiteColor()
    btn.addTarget(self, action: "setNew:", forControlEvents: UIControlEvents.TouchUpInside)
    btn.setImage(UIImage(named:"transparent.png"), forState: .Normal)
    cell.indentationLevel = 1
    cell.indentationWidth = 45
    cell.addSubview(btn)
    btn.backgroundColor=UIColor.whiteColor()
    cell.selectionStyle = .None
    return cell
}

var old = 1000

func setNew(sender:UIButton)
{
    var tmpButton = view.viewWithTag(old) as? UIButton
    let btn = sender
    if (btn.backgroundColor == UIColor.redColor())
    {
        btn.backgroundColor = UIColor.whiteColor()
    }
    else if (btn.backgroundColor != UIColor.redColor())
    {
        tmpButton?.backgroundColor = UIColor.whiteColor()
        btn.backgroundColor = UIColor.redColor()
        selection =  Berufs[sender.tag]
        println("you are a \(selection)")
    }
    old = btn.tag
    println("button tag is \(btn.tag)")
}

0 是 tag 的默认值,因此 view.viewWithTag(0) 可能 returns 不是您的按钮。

你最好从 1 开始,所以

btn.tag = indexPath.row + 1

并相应地处理您的 old 计数器。


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

    var cell = myTable.dequeueReusableCellWithIdentifier("toWork") as UITableViewCell
    cell.textLabel?.text = Berufs[indexPath.row]
    var btn:UIButton = UIButton(frame: CGRectMake(10, 3, 40, 40))

    btn.tag = indexPath.row + 1  // <-- HERE

    btn.backgroundColor = UIColor.whiteColor()
    btn.addTarget(self, action: "setNew:", forControlEvents: UIControlEvents.TouchUpInside)
    btn.setImage(UIImage(named:"transparent.png"), forState: .Normal)
    cell.indentationLevel = 1
    cell.indentationWidth = 45
    cell.addSubview(btn)
    btn.backgroundColor=UIColor.whiteColor()
    cell.selectionStyle = .None
    return cell
}


var old = 1000

func setNew(sender:UIButton)
{
    var tmpButton = view.viewWithTag(old) as? UIButton
    let btn = sender
    if (btn.backgroundColor == UIColor.redColor())
    {
        btn.backgroundColor = UIColor.whiteColor()
    }
    else if (btn.backgroundColor != UIColor.redColor())
    {
        tmpButton?.backgroundColor = UIColor.whiteColor()
        btn.backgroundColor = UIColor.redColor()

        selection =  Berufs[sender.tag - 1]   // <-- HERE

        println("you are a \(selection)")
    }
    old = btn.tag
    println("button tag is \(btn.tag)")
}

默认情况下,按钮的标签为 0。 我建议将标签设置为 100 + indexPath.row 之类的值,然后在检查时从 UIButton 的标签中减去 100。

- (UIView *)viewWithTag:(NSInteger)tag

This method searches the current view and all of its subviews for the specified view

所以它可以return任何默认标签==0的东西。例如来自按钮或其他东西的标签