更改按钮标题文本不会工作

时间:2016-04-19 07:43:47

标签: ios swift

我正在尝试更改UIButton标题文字,但它不起作用。我在UITableView中有它。这就是它的样子:

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

        let imageCell = tableView.dequeueReusableCellWithIdentifier("imageCell", forIndexPath: indexPath) as! StoryCell
        let videoCell = tableView.dequeueReusableCellWithIdentifier("videoCell", forIndexPath: indexPath) as! StoryCell
        let blankCell = tableView.dequeueReusableCellWithIdentifier("blankCell", forIndexPath: indexPath) as! StoryCell

        if (userFile[indexPath.item].url!.rangeOfString(".jpeg") != nil) {
            print("imageCell: \(userFile[indexPath.row].url)")

            imageCell.viewstoryButton.setTitle("Vis bildeeee - \(self.createdAt[indexPath.row].timeAgo)", forState: UIControlState.Normal)
            imageCell.viewstoryButton.titleLabel?.textColor  = UIColor.redColor()

            return imageCell
        }
        if (userFile[indexPath.row].url!.rangeOfString(".mp4") != nil) {
            print("videoCell: \(userFile[indexPath.row].url)")

            videoCell.viewstoryButton.setTitle("Vis video - \(self.createdAt[indexPath.row].timeAgo)", forState: UIControlState.Normal)

            return videoCell
        }
        else{
            print("blankCell")
            return blankCell
        }

    }

我有3个原型单元格:imageCellvideoCellblankCell

它们都连接到StoryCell,按钮会出现,但文字或颜色不会改变。有什么想法吗?

2 个答案:

答案 0 :(得分:0)

确保您的按钮也作为插座连接到您的Swift文件

答案 1 :(得分:0)

向官方apple doc解释正确的语法是:

  

setTitle(_ title:String?,            forState state:UIControlState)

对于titleLabel,doc说:

  

titleLabel:UILabel? {get}

所以,你的语法代码是正确的。

您可以检查是否调用 reloadData (),以及 IBOutlet 按钮是否正确连接到名为&#34; StoryCell&#34;的自定义单元库< / p>

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

        var imageCell = tableView.dequeueReusableCellWithIdentifier("imageCell", forIndexPath: indexPath) as! StoryCell
        if imageCell == nil {
            imageCell = StoryCell(style: UITableViewCellStyle.Default, reuseIdentifier: ("imageCell"))
        }

        var videoCell = tableView.dequeueReusableCellWithIdentifier("videoCell", forIndexPath: indexPath) as! StoryCell
        if videoCell == nil {
            videoCell = StoryCell(style: UITableViewCellStyle.Default, reuseIdentifier: ("videoCell"))
        }

        var blankCell = tableView.dequeueReusableCellWithIdentifier("blankCell", forIndexPath: indexPath) as! StoryCell
        if blankCell == nil {
            blankCell = StoryCell(style: UITableViewCellStyle.Default, reuseIdentifier: ("blankCell"))
        }        

        if (userFile[indexPath.item].url!.rangeOfString(".jpeg") != nil) {
            print("imageCell: \(userFile[indexPath.row].url)")

            imageCell.viewstoryButton.setTitle("Vis bildeeee - \(self.createdAt[indexPath.row].timeAgo)", forState: UIControlState.Normal)
            imageCell.viewstoryButton.titleLabel?.textColor  = UIColor.redColor()

            return imageCell
        }
        if (userFile[indexPath.row].url!.rangeOfString(".mp4") != nil) {
            print("videoCell: \(userFile[indexPath.row].url)")

            videoCell.viewstoryButton.setTitle("Vis video - \(self.createdAt[indexPath.row].timeAgo)", forState: UIControlState.Normal)

            return videoCell
        }
        else{
            print("blankCell")
            return blankCell
        }

    }