我正在繼續針對iOS的應用程序導向至iPad。今天早上asked a question在我的問題得到正確答案的網站上。但由於某種原因,阿爾法在我的桌子的單元格中不起作用。Alpha無法在UITableViewCell中工作
這是我的看法關係的TableView:
這是我的銀行代碼:
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。
有人可以幫我嗎?
爲什麼當你選擇一行時你期望有什麼改變?您發佈的代碼只會更新所選行,並且所選行的設置爲1.0。每當選擇一行時,您應該重新加載所有可見的行。 – rmaddy
@rmaddy當選擇一行時,我需要做什麼來改變所有單元格的Alpha(1.0/0.7/0.5)? – James
對。這就是爲什麼我說你選擇一行時需要重新加載所有可見單元格的原因。您只重新加載選定的單元格。 – rmaddy