簡體   English   中英

Swift:在代碼中但不在情節提要中包含UITableViewCell的同時在每個單元格中添加一個UIButton

[英]Swift: Adding a UIButton in each cell while having a UITableViewCell in the code but not in the storyboard

我有一個跟蹤列表,它表示存儲在陣列中的任務列表,無論如何,我需要在每個跟蹤列表旁邊有一個名為“ Track”的按鈕,以便用戶可以通過移至另一個具有以下內容的頁面來跟蹤此任務這個功能。 我的問題是我無法使按鈕顯示在每個單元格旁邊。 在表格視圖中顯示信息的代碼:

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell
{
    let cell: UITableViewCell = UITableViewCell(style: UITableViewCellStyle.Subtitle, reuseIdentifier: "cell")

    cell.textLabel?.text = trackMgr.tracks[indexPath.row].name

    cell.detailTextLabel?.text = String(trackMgr.tracks[indexPath.row].score)

    return cell



}

這是我的表格視圖在情節提要中的樣子: 這里

我試圖做的是聲明等於UIButton的“ mybutton”! 在頂部,然后調用cell.mybutton.text =“ text”,但是它不起作用。

您可以使用以下代碼在每個單元格上創建按鈕,因此可以在按下按鈕后執行跟蹤任務。

看下面的代碼

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
    let cell: UITableViewCell = UITableViewCell(style: UITableViewCellStyle.Subtitle, reuseIdentifier: "cell")

    cell.textLabel?.text = "hello"
    let track_Button = UIButton()
    track_Button.setTitle("Track", forState: .Normal)
    track_Button.setTitleColor(UIColor.blueColor(), forState: .Normal)
    track_Button.frame = CGRectMake(self.view.frame.size.width-100, 0, 100, 40)
    track_Button.backgroundColor = UIColor.grayColor()
    track_Button.addTarget(self, action: "track_Button_Pressed:", forControlEvents: .TouchUpInside)
    cell.addSubview(track_Button)

    return cell


}

func track_Button_Pressed(sender: UIButton!) {

    // Track Functionality
    println("Add Track Functionality here")
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM