2016-01-11 26 views
1

我正在繼續針對iOS的應用程序導向至iPad。今天早上asked a question在我的問題得到正確答案的網站上。但由於某種原因,阿爾法在我的桌子的單元格中不起作用。Alpha無法在UITableViewCell中工作

這是我的看法關係的TableView:

enter image description here

這是我的銀行代碼:

import UIKit 

class ViewController: UIViewController, UITableViewDataSource, UITableViewDelegate { 

    let data:[String] = ["Row 0","Row 1", "Row 2","Row 3","Row 4","Row 5","Row 6"] 

    @IBOutlet var tableView: UITableView! 
    var rowSelected:Int! = nil 

    override func viewDidLoad() { 
     super.viewDidLoad() 
    } 

    func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int { 
     return data.count 
    } 

    func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { 
     let cell:Cell = self.tableView.dequeueReusableCellWithIdentifier("cell") as! Cell 
     if rowSelected != nil { 
      if rowSelected > indexPath.row { 
       cell.alpha = 0.7 
      } else if rowSelected == indexPath.row { 
       cell.alpha = 1.0 
      } else { 
       cell.alpha = 0.3 
      } 
     } 

     cell.labelText.text = self.data[indexPath.row] 

     return cell 
    } 

    func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) { 
     self.rowSelected = indexPath.row 

     tableView.reloadRowsAtIndexPaths([indexPath], withRowAnimation: .Automatic) 
    } 
} 

當我選擇一排,沒有任何反應,阿爾法沒有。在這個例子中,我直接在單元格中應用Alpha(我禁用了單元格中的不透明選項 - 單元格不透明)。 在原始應用程序中,我有2個標籤,2個按鈕和一張圖片。爲了更實際,Alpha的必要性直接用於Cell。

有人可以幫我嗎?

+0

爲什麼當你選擇一行時你期望有什麼改變?您發佈的代碼只會更新所選行,並且所選行的設置爲1.0。每當選擇一行時,您應該重新加載所有可見的行。 – rmaddy

+0

@rmaddy當選擇一行時,我需要做什麼來改變所有單元格的Alpha(1.0/0.7/0.5)? – James

+0

對。這就是爲什麼我說你選擇一行時需要重新加載所有可見單元格的原因。您只重新加載選定的單元格。 – rmaddy

回答

7

有您需要照顧3個元素:

  1. UITableView:如果你的tableview的背景是白色的,無論你在單元格上設置了哪個 alpha,它都不會在UI上反映出來正確。嘗試將tableview的背景顏色設置爲透明。
  2. UITableViewCell:期待您正在使用此控件。
  3. UITableViewCell的contentview:嘗試將UITableViewCell的contentview背景顏色也設置爲透明。
+0

UITableViewCell的contentview:嘗試將UITableViewCell的contentview背景顏色設置爲透明也..爲我做了詭計,謝謝 – KarimIhab

+0

@KarimIhab:它在解決方案中的第3點提到。 –

+1

我指的是你的解決方案,它爲我做了訣竅:D 不像我建議一個新的解決方案,抱歉的混淆 – KarimIhab

1

tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath)只有在tableview第一次渲染單元格時才被調用。嘗試重新加載tableview的數據以獲得快速結果。

從長遠來看,您應該重構alpha修改,以便在發生選擇時也發生在所有可見單元格上。

+0

結果是一樣的。我還沒有找到解決方案。 – James

1

您可能仍然擁有tableView的白色背景。嘗試將tableView的背景去清除代碼:

tableView.backgroundColor = .clearColor() 

或在接口編輯:

enter image description here

+0

我按照你的說法應用了背景。並使用打印,我發現Alpha正在被應用,但在TableView中沒有任何反應。有些東西丟失了,但仍然不知道它是什麼。 – James

相關問題