底部5行的UITableView背景颜色

时间:2018-01-11 03:09:34

标签: ios swift uitableview

我知道如何为我的行输入背景颜色,但我真的不知道如何只通过底部5行来过滤它是" cell.backgroundColor = UIColor.red;&# 34;而其余的保持不变。感谢那些可以帮助我的人,谢谢!

P.S:抱歉,我的快速生锈了。

UITableView控制器

    import UIKit
    import FirebaseDatabase


    var postData2 = [String]()
    var postData3 = [String]()
     var tableDataArray = [tableData]()



    class ResultsController: UITableViewController {


        override func viewDidLoad() {
            super.viewDidLoad()

            ref = Database.database().reference() //set the firebase reference
            // Retrieve the post and listen for changes
            databaseHandle = ref?.child("Posts3").observe(.value, with: { (snapshot) in
                postData2.removeAll()
                postData3.removeAll()
                tableDataArray.removeAll()

                for child in snapshot.children {
                    let snap = child as! DataSnapshot
                    let key = snap.key
                    let value = String(describing: snap.value!)
                    let rating = (value as NSString).integerValue


                    postData2.append(key)
                    postData3.append(value)
                    tableDataArray.append(tableData(boothName: key, boothRating: rating))


                }
                postData2.removeAll()
                postData3.removeAll()
                let sortedTableData = tableDataArray.sorted(by: { $0.boothRating > $1.boothRating })
                for data in sortedTableData {
                    postData2.append(data.boothName)

                    let value = String(describing: data.boothRating)
                    postData3.append(value)
                }

                self.tableView.reloadData()
            })
        }

        override func didReceiveMemoryWarning() {
            super.didReceiveMemoryWarning()
            // Dispose of any resources that can be recreated.
        }





        override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {

            return postData2.count

        }

        override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
            let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath)
            cell.textLabel?.font = UIFont.init(name: "Helvetica", size: 23)
            cell.textLabel?.text = postData2[indexPath.row]
            cell.detailTextLabel?.text = postData3[indexPath.row] + " ♥"
            cell.detailTextLabel?.textColor = UIColor.red;
            cell.detailTextLabel?.font = UIFont.init(name: "Helvetica", size: 23)
//        cell.backgroundColor = UIColor.red;
            return cell

        }

        override func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat
        {
            return 80
        }

    }

    class tableData {
        var boothName: String
        var boothRating: Int
        init(boothName: String, boothRating: Int) {
            self.boothName = boothName
            self.boothRating = boothRating
        }
}

enter image description here

5 个答案:

答案 0 :(得分:4)

一种简单的方法是进行条件检查,以查看indexPath.row值是否在最后五个范围内。

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath)
    if(indexPath.row >= postData2.count-5){
        cell.backgroundColor = UIColor.red
    }else{
        cell.backgroundColor = UIColor.white /* Remaining cells */
    }
    return cell
}

答案 1 :(得分:4)

其他一些答案也可行 - 但是当它们被cellForRowAt出列时使用具有已知配置的单元格更好,而不是每次出列单元格时都处理一堆可能的起始条件。为此,请将UITableViewCell子类化并覆盖prepareForReuse()。在dequeueReusableCell返回单元格之前调用此函数。然后可以在配置单元格之前将单元格设置为已知的起始点。如果可以在cellForRowAt中以任何可能的方式接收单元格,那么很快就会出现很长的函数,并且有很多if / else条件。

条件

if indexPath.row >= postData2.count - 5 {
    cell.backgroundColor = UIColor.red
}

可以按原样使用,prepareForReuse负责处理细胞在回收时不保留任何设置。这是一个例子:

override func prepareForReuse() {
    super.prepareForReuse()
    backgroundColor = UIColor.white
}

使用这一个简单的设置,无论您是使用if / else方法还是使用子类化来充分利用prepareForReuse,都可以进行清洗。但是一旦你在一个单元格中设置了不止一个东西,你会发现使用这个函数要复杂得多,并且导致细胞出现的错误要少得多 - 考虑如果有多个可能的话会发生什么单元格可以是颜色,或者单元格中有多个元素要配置多个可能的值...

答案 2 :(得分:2)

您可以添加简单的逻辑

if indexPath.row >=(postData2.count-5) {
    cell.backgroundColor = UIColor.red
}else {
    cell.backgroundColor = UIColor.white
}

答案 3 :(得分:0)

只需检查设置最后五行的红色的条件。

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath)
    if(indexPath.row >= postData2.count-5){
        cell.backgroundColor = UIColor.red;
    }else {
        cell.backgroundColor = UIColor.white; //white colour for other rows
    }
    return cell
}

答案 4 :(得分:0)

系统推荐使用此方法,此方法在某些情况下更可以避免重用(例如,当您修改单元格表面中控件的内容时)

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath)
    if(indexPath.row >= postData2.count-5){
        cell.backgroundColor = UIColor.red
    }else{
        cell.backgroundColor = UIColor.white /* Remaining cells */
    }
    return cell
}

如果它涉及数据处理,你可以创建一个新的NSMutableSet(),用于存储你的操作(普通数据丢失,存储在indexPath里面的didSelecetRow中)保存,无论如何,一个唯一的标记。

这些只是解决多路复用问题,处理变色,参考上述解决方案。

{{1}}
相关问题