Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/108.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 选择单元格并更改tableView中所有单元格的alpha-Swift_Ios_Swift_Uitableview - Fatal编程技术网

Ios 选择单元格并更改tableView中所有单元格的alpha-Swift

Ios 选择单元格并更改tableView中所有单元格的alpha-Swift,ios,swift,uitableview,Ios,Swift,Uitableview,我正在做一个项目,需要一些我从未想过会有的东西。iOS版的应用程序由于尺寸原因被定向到iPad上。对于这个问题,我制作了一个小型原型,以更好地向其中一方展示细节 这是一个表视图,其中将发生函数和操作 这是Swift代码: import UIKit class ViewController: UIViewController, UITableViewDataSource, UITableViewDelegate { let data:[String] = ["Row 0","Row

我正在做一个项目,需要一些我从未想过会有的东西。iOS版的应用程序由于尺寸原因被定向到iPad上。对于这个问题,我制作了一个小型原型,以更好地向其中一方展示细节

这是一个表视图,其中将发生函数和操作

这是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
    }

}
应用程序到底要做什么?

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

示例:

我碰了第3排

行动:

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

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

我在第4行触到了

行动:

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

第5行、第6行将保持α等于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)
}

您好,我建议以下解决方案,请考虑

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()
    }

}
导入UIKit
类ViewController:UIViewController、UITableViewDataSource、UITableViewDelegate{
let数据:[字符串]=[“第0行”、“第1行”、“第2行”、“第3行”、“第4行”、“第5行”、“第6行”]
所选变量:Int
@IBVAR表格视图:UITableView!
重写func viewDidLoad(){
super.viewDidLoad()
}
func tableView(tableView:UITableView,numberofrowsinssection:Int)->Int{
返回数据.count
}
func tableView(tableView:UITableView,cellForRowAtIndexPath:nsindepath)->UITableView单元格{
让cell:cell=self.tableView.dequeueReusableCellWithIdentifier(“cell”)作为!cell
cell.labelText.text=self.data[indexPath.row]
如果rowSelected生成一个整数变量“
selectedCell”
,并在
didselectrowatinexpath
中,将其设置为等于
indexPath.row
,然后
tableView.reloadData()

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


通常,在
cellforrowatinedexpath
中更改单元格属性是一种很好的做法,因为每次调用它时,您都有可能覆盖您对代码中其他单元格所做的更改。希望这有所帮助。

我不确定您是否描述了希望它做什么或异常行为

有两种可能性:

1) 如果希望在单行上渲染与选择相关的alpha,则可能需要覆盖DidDeceloseRowAtIndexPath并将alpha设置为1.0

2) 从dequeueReusableCellWithIdentifier获取单元后,需要显式设置alpha,因为不能保证每次都获得一个新的单元实例


即使在第一次显示CellForRowatineXpath之后,tableview也会出于各种原因调用CellForRowatineXpath。可能发生的情况是,CellForRowatineXpath是为已取消选择的行调用的,而dequeueReusableCellWithIdentifier可能会也可能不会重用现有的单元格对象。当它重用单元格对象时,属性e未重置。

此代码在swift-2中是否有效?我测试了它,但在
cell:cell
中失败,错误:
使用未声明的类型“cell”
我从给出的示例代码中复制了它。您希望将其转换为UITableViewCell而不是cell。答案已更新。哦,似乎我劫持了它,我不是所有者。我的错,假设您是所有者:-p谢谢或者注意到错误。@Chris这似乎解决了我的问题,但仍然不正确。每当我选择一个单元格时,表格充电会消除选择,然后它不会应用alpha 0.5。我如何解决这个问题?