选择一个单元格并更改 tableView 中所有单元格的 alpha - Swift [英] Selecting a cell and changing the alpha of all cells in tableView - Swift

查看:20
本文介绍了选择一个单元格并更改 tableView 中所有单元格的 alpha - Swift的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开展一个项目,该项目需要我从未想过的东西.由于尺寸原因,适用于 iOS 的应用程序针对 iPad.针对这个问题,我制作了一个小原型,向其中一方展示更好的细节.

I'm working on a project that needs something I never imagined to have. The app for iOS is directed to iPad due to size. To that question, I made a small prototype to show one of the parties to detail better.

这是一个 tableView,函数和动作将在其中发生.

This is a tableView where the functions and actions will happen.

这是 Swift 代码:

And this is the Swift code:

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
    }

}

应用究竟要做什么?

Well, when a row is selected, the rows below it need to stay with the Alpha equal to 0.5.

Well, when a row is selected, the rows below it need to stay with the Alpha equal to 0.5.

示例:

我摸到了第 3 行

操作:

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

Row 1, Row 2 and Row 3 will keep the Alpha equal to 1.0

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

Row 4, Row 5 and Row 6 will keep the Alpha equal to 0.5

我在第 4 行碰过

操作:

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

Row 1, Row 2, Row 3 and Row 4 will keep the Alpha equal to 1.0

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

Row 5, Row 6 will keep the Alpha equal to 0.5

.

.

.

有人可以帮我吗?

推荐答案

您希望在 cellForRowAtIndexPath 中设置 alpha 值,然后在点击时重新加载该行.这应该保留该单元格的 alpha 并将每个其他单元格的 alpha 设置为 1,即使用户在屏幕外滚动单元格也是如此.

You'd want to set the alpha value in the cellForRowAtIndexPath, then simply reload that row when its tapped. This should preserve the alpha for that cell and set alpha to 1 on every other cell, even if the the user scrolls the cell offscreen.

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

这篇关于选择一个单元格并更改 tableView 中所有单元格的 alpha - Swift的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆