按钮没有注册水龙头

时间:2015-08-22 09:42:00

标签: ios swift uibutton

此单元格中的按钮未注册水龙头。 downloadEpisode()中的打印声明不会打印,并且按钮不会突出显示/更改颜色。但是,当我点击按钮时,会选择表格视图单元格。我认为单元格位于按钮视图下方,但情况并非如此(我已检查过UI视图层次结构)。

    override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath, object: PFObject?) -> PFTableViewCell? {
    let cell: PFTableViewCell = tableView.dequeueReusableCellWithIdentifier("Cell", forIndexPath: indexPath) as! PFTableViewCell
    if let title = object?["title"] as? String {
        cell.textLabel?.text = title
    }
    let downloadButton = UIButton(type: .RoundedRect)
    downloadButton.frame = CGRectMake(cell.contentView.bounds.width - 100, cell.contentView.bounds.height / 2, 100, 0)
    downloadButton.setTitle("Download", forState: .Normal)
    downloadButton.setTitleColor(UIColor.blueColor(), forState: .Normal)
    downloadButton.setTitleColor(UIColor.redColor(), forState: .Selected)
    downloadButton.tag = indexPath.row
    downloadButton.addTarget(self, action: "downloadEpisode:", forControlEvents: .TouchUpInside)
    cell.contentView.addSubview(downloadButton)
    cell.contentView.bringSubviewToFront(downloadButton)
// using cell.addSubview(downloadButton) doesn't work either.


    return cell
}

1 个答案:

答案 0 :(得分:1)

不确定这是否会对其他人有所帮助,但这里是解决方案:

    downloadButton.frame = CGRectMake(cell.contentView.bounds.width - 100, cell.contentView.bounds.height / 2, 100, 0)

应该是

     downloadButton.frame = CGRectMake(cell.contentView.bounds.width - 100, cell.contentView.bounds.height / 2, 100, 40)

按钮的高度为0,这是故意的,因为文本仍会正常显示,我认为触摸区域只是文本,原来它不会像那样工作。