select一个单元格,并更改tableView中的所有单元格的alpha – Swift

我正在做一个需要我从未想象过的项目。 iOS的应用程序由于大小而定向到iPad。 对于这个问题,我做了一个小的原型,以显示其中一个更好的细节。

这是一个tableView的function和行动将发生。

在这里输入图像说明

这是Swift代码:

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! 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 cell.labelText.text = self.data[indexPath.row] return cell } func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) { let cell = self.tableView.cellForRowAtIndexPath(indexPath) cell!.alpha = 0.5 } } 

什么应用程序来完成?

那么,当选中一行时,它下面的行需要保持等于0.5的Alpha。

例子:

我摸了第3行

行动:

第1行,第2行和第3行将保持Alpha等于1.0

第4行,第5行和第6行将保持Alpha等于0.5

我在第四行中碰到

行动:

第1行,第2行,第3行和第4行将保持Alpha等于1.0

第5行第6行将保持Alpha等于0.5

有人能帮我吗?

你想要在cellForRowAtIndexPath中设置alpha值,然后只需重新加载该行。 这应该保留该单元格的alpha,并在每隔一个单元格上将alpha设置为1,即使用户将单元格从屏幕上滚动出来。

 var selectedIndexPath:NSIndexPath? func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { let cell:UITableViewCell = self.tableView.dequeueReusableCellWithIdentifier("cell") as! UITableViewCell if let selectedIndexPath = self.selectedIndexPath where indexPath.row == selectedIndexPath.row { cell.alpha = 0.5 } else { cell.alpha = 1 } cell.labelText.text = self.data[indexPath.row] return cell } func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) { self.selectedIndexPath = indexPath tableView.reloadRowsAtIndexPaths([indexPath], withRowAnimation: .Automatic) } 

设置一个整数variables“ selectedCell" ,并在didSelectRowAtIndexPath中设置它等于indexPath.row ,然后tableView.reloadData()

cellForRowAtIndexPath只需使用if语句来确定indexPath.row是否大于“ selectedCell ”。 如果是,则将alpha值设置为0.5 ,否则将其设置为1.0

cellForRowAtIndexPath更改单元格属性通常是一个很好的做法,因为每次调用它时都会覆盖对代码中其他位置的单元格所做的更改。 希望这有助于。

嗨,我build议在下面的解决scheme,请考虑

 import UIKit class ViewController: UIViewController, UITableViewDataSource, UITableViewDelegate { let data:[String] = ["Row 0","Row 1", "Row 2","Row 3","Row 4","Row 5","Row 6"] var rowSelected:Int @IBOutlet var tableView: UITableView! 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 cell.labelText.text = self.data[indexPath.row] if rowSelected <= indexPath.row cell!.alpha = 0.5; else cell!.alpha = 1.0; return cell } func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) { let cell = self.tableView.cellForRowAtIndexPath(indexPath) rowSelected = indexPath.row tableView.reloadData() } } 

我不确定你是在描述你想要做什么或者发生exception行为。

有两种可能性:

1)如果你想要select相关的阿尔法在一个单一的行上呈现,你可能想重写didDeselectRowAtIndexPath并设置alpha为1.0。

2)从dequeueReusableCellWithIdentifier获取单元格后,需要明确设置alpha,因为不能保证每次都得到新的单元格实例。

由于各种原因,即使在第一次显示之后,tableview也会调用cellForRowAtIndexPath。 可能发生的情况是,cellForRowAtIndexPath被取消select的行调用,dequeueReusableCellWithIdentifier可能会或可能不会重用现有的单元格对象。 当它重用一个单元格对象时,属性不会重置。