2016-01-11 97 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

相关问题