简体   繁体   中英

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

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.

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

在此处输入图片说明

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
    }

}

What the app to do exactly?

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

Examples:

I touched the row 3

Action:

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

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

I touched in row 4

Action:

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

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

.

.

.

Can someone help me?

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

Make an integer variable " selectedCell" , and in didSelectRowAtIndexPath , set it equal to indexPath.row , then tableView.reloadData()

In cellForRowAtIndexPath simply use an if statement to determine if indexPath.row is greater than " selectedCell ". If so then set the alpha value to 0.5 , otherwise set it to 1.0 .

It is generally a good practice to change cell properties in cellForRowAtIndexPath , because every time it is called you risk overwriting changes you make to cells elsewhere in the code. Hope this helped.

Hi I am proposing below solution, please consider

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

}

I'm not sure if you are describing what you want it to do or the anormal behaviour.

There are two possibilities:

1) If you want the selection related alpha to render on a single row, you may want to override didDeselectRowAtIndexPath and set the alpha to 1.0 there.

2) You need to explicitly set the alpha after getting the cell from dequeueReusableCellWithIdentifier because you are not guaranteed to get a fresh cell instance every time.

The tableview will call cellForRowAtIndexPath for a variety of reasons even after showing it for the first time. What is probably happening is that cellForRowAtIndexPath is called for rows that have been deselected and dequeueReusableCellWithIdentifier may or may not reuse an existing cell object. When it reuses a cell object, properties are not reset.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM