簡體   English   中英

選擇一個單元格並更改tableView中所有單元格的Alpha-Swift

[英]Selecting a cell and changing the alpha of all cells in tableView - Swift

我正在進行的項目需要我從未想過的東西。 由於大小,適用於iOS的應用程序定向到iPad。 對於這個問題,我制作了一個小型原型,以向各方展示其中的一個細節。

這是一個tableView,其中將發生功能和動作。

在此處輸入圖片說明

這是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行將使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)
}

創建一個整數變量“ selectedCell" ,然后在didSelectRowAtIndexPath中將其設置為等於indexPath.row ,然后將其設置為tableView.reloadData()

cellForRowAtIndexPath只需使用if語句來確定indexPath.row是否大於“ selectedCell ”。 如果是這樣,則將alpha值設置為0.5 ,否則將其設置為1.0

更改cellForRowAtIndexPath單元格屬性通常是一個好習慣,因為每次調用它都可能會覆蓋您對代碼中其他位置的單元格所做的更改。 希望這會有所幫助。

嗨,我正在提出以下解決方案,請考慮

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

}

我不確定您是要描述自己要執行的操作還是異常行為。

有兩種可能性:

1)如果希望與選擇相關的Alpha呈現在單行上,則可能要覆蓋didDeselectRowAtIndexPath並將那里的Alpha設置為1.0。

2)從dequeueReusableCellWithIdentifier獲取單元格后,您需要顯式設置Alpha,因為不能保證每次都獲得一個新的單元格實例。

即使是第一次顯示后,tableview仍會出於各種原因調用cellForRowAtIndexPath。 可能發生的情況是,已取消選擇的行將調用cellForRowAtIndexPath,並且dequeueReusableCellWithIdentifier可能會或可能不會重用現有的單元格對象。 重用單元格對象時,不會重置屬性。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM