UITableView滚动更改单元格背景颜色swift

时间:2017-04-28 16:16:38

标签: ios swift tableview cell

我的细胞有白色背景,它们之间通常是薄透明的分隔物,但是我已经让潜水员变红了以突出发生的事情。

它从右边开始,但是当我滚动它时,将单元格的颜色更改为分隔颜色并不断变化。

查看图片以进一步说明我想要的内容。

What i want when i scroll and what i start with

查看我最终的图像。

After scrolling up and down it updates to this

以下是我的cellForRowAtIndex的代码:

  func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {

    let cell  = self.tableView.dequeueReusableCell(withIdentifier: "Cell", for : indexPath) as! CustomCell


    let bgColorView = UIView()
    bgColorView.backgroundColor = UIColor.clear.withAlphaComponent(0.15)

    cell.selectedBackgroundView = bgColorView


    if indexPath.row % 2 == 0
    {
        if(indexPath.row == 0){
            cell.titleLabel.text = problems[(indexPath as NSIndexPath).row].getTitle()
            cell.descriptionLabel.text = problems[(indexPath as NSIndexPath).row].getMessage()

            //print("status is \(problems[(indexPath as NSIndexPath).row].status)")

            if(problems[(indexPath as NSIndexPath).row].status == 0){
                cell.statusLabel.textColor = UIColor.red
            }
            else{
                cell.statusLabel.textColor = UIColor(red: 28/255.0, green: 121/255.0, blue: 125/255.0, alpha: 1.0)
            }
            cell.statusLabel.text = problems[(indexPath as NSIndexPath).row].calculateSolved()
            cell.messageCountLabel.text = "\(problems[(indexPath as NSIndexPath).row].commentCount)"
            cell.distanceLabel.text = "\(problems[(indexPath as NSIndexPath).row].getDistance())km"
            cell.problemImage.image = #imageLiteral(resourceName: "NoImage")


            if(problems[(indexPath as NSIndexPath).row].image_url != ""){

                print("runs imageeee")
                var storage = FIRStorage.storage()

                // This is equivalent to creating the full reference
                let storagePath = "http://firebasestorage.googleapis.com\(problems[(indexPath as NSIndexPath).row].image_url)"
                var storageRef = storage.reference(forURL: storagePath)




                // Download in memory with a maximum allowed size of 1MB (1 * 1024 * 1024 bytes)
                storageRef.data(withMaxSize: 30 * 1024 * 1024) { data, error in
                    if let error = error {
                        // Uh-oh, an error occurred!
                    } else {
                        // Data for "images/island.jpg" is returned
                        DispatchQueue.main.async {
                            cell.problemImage.image  = UIImage(data: data!)!
                        }
                        print("returned image")
                    }
                }


            }

            else{

                cell.problemImage.image  = #imageLiteral(resourceName: "NoImage")

            }

            cell.backgroundColor = UIColor.white



            }


        else{

            cell.titleLabel.text = problems[(indexPath as NSIndexPath).row - indexPath.row / 2].getTitle()
            cell.descriptionLabel.text = problems[(indexPath as NSIndexPath).row - indexPath.row / 2].getMessage()
            //print("status is \(problems[(indexPath as NSIndexPath).row - indexPath.row / 2].status)")

            if(problems[(indexPath as NSIndexPath).row - indexPath.row / 2].status == 0){
                cell.statusLabel.textColor = UIColor.red
            }
            else{
                cell.statusLabel.textColor = UIColor(red: 28/255.0, green: 121/255.0, blue: 125/255.0, alpha: 1.0)
            }
            cell.statusLabel.text = problems[(indexPath as NSIndexPath).row - indexPath.row / 2].calculateSolved()
            cell.messageCountLabel.text = "\(problems[(indexPath as NSIndexPath).row - indexPath.row / 2].commentCount)"
            cell.distanceLabel.text = "\(problems[(indexPath as NSIndexPath).row - indexPath.row / 2].getDistance())km"
            cell.problemImage.image = #imageLiteral(resourceName: "NoImage")


            if(problems[(indexPath as NSIndexPath).row - indexPath.row / 2].image_url != ""){

                print("runs imageeee")
                var storage = FIRStorage.storage()

                // This is equivalent to creating the full reference
                let storagePath = "http://firebasestorage.googleapis.com\(problems[(indexPath as NSIndexPath).row - indexPath.row / 2].image_url)"
                var storageRef = storage.reference(forURL: storagePath)




                // Download in memory with a maximum allowed size of 1MB (1 * 1024 * 1024 bytes)
                storageRef.data(withMaxSize: 30 * 1024 * 1024) { data, error in
                    if let error = error {
                        // Uh-oh, an error occurred!
                    } else {
                        // Data for "images/island.jpg" is returned
                        DispatchQueue.main.async {
                            cell.problemImage.image  = UIImage(data: data!)!
                        }
                        print("returned image")
                    }
                }





            cell.backgroundColor = UIColor.white


        }


    }
    }
    else {

        cell.backgroundColor = UIColor.red
        cell.titleLabel.text = ""
        cell.descriptionLabel.text = nil
        cell.statusLabel.text = ""
        cell.distanceLabel.text = ""
        cell.problemImage.image = nil
        cell.selectionStyle = .none

    }


    return cell
}

1 个答案:

答案 0 :(得分:0)

使用此:

tableView(_:shouldHighlightRowAt:) 

而且:

tableView(_:didSelectRowAt:)  

并且没有突出显示

tableView(_:didUnhighlightRowAt:)

要修改单元格选择,请不要忘记遵循UITableViewDelegate。