Swift change cell background colors in SOME tableView cells

廉价感情. 提交于 2020-03-23 07:51:09

问题


Two arrays. 1st has all the events, 2nd has the conflicting events(all exist in the 1st array). TableView shows all the events. I want to highlight the elements of the 2nd array in the table view cells

1st Array 21 items

["Bicycling with Friends - Start: Nov 1, 18 6:00 AM - End: Nov 1, 18 9:30 AM", "Yoga - Start: Nov 1, 18 6:00 PM - End: Nov 1, 18 7:33 PM", "Local Pub with Friends - Start: Nov 1, 18 7:33 PM - End: Nov 1, 18 11:00 PM", "Football Game - Start: Nov 3, 18 6:14 PM - End: Nov 3, 18 9:44 PM", "Football Tailgate with John - Start: Nov 3, 18 6:14 PM - End: Nov 3, 18 9:44 PM", "Volleyball With Friends - Start: Nov 6, 18 12:00 PM - End: Nov 6, 18 1:26 PM", "Boba Tea Grand Opening - Start: Nov 6, 18 1:27 PM - End: Nov 6, 18 2:00 PM", "Evening Cookout with Friends - Start: Nov 6, 18 5:00 PM - End: Nov 6, 18 10:00 PM", "SF Coffee Festival - Start: Nov 6, 18 6:00 PM - End: Nov 6, 18 9:00 PM", "Roller Derby - Start: Nov 7, 18 12:00 PM - End: Nov 7, 18 2:30 PM", "Rock Concert - Start: Nov 7, 18 6:30 PM - End: Nov 7, 18 11:00 PM", "Lunch with Friends - Start: Nov 8, 18 11:30 AM - End: Nov 8, 18 12:59 PM", "Nap Break - Start: Nov 8, 18 12:56 PM - End: Nov 8, 18 1:30 PM", "Basketball Game - Start: Nov 8, 18 7:30 PM - End: Nov 8, 18 10:30 PM", "Lunch Meeting - Start: Nov 9, 18 12:30 PM - End: Nov 9, 18 2:30 PM", "Free Donuts - Start: Nov 9, 18 3:00 PM - End: Nov 9, 18 4:00 PM", "TV Show Marathon - Start: Nov 9, 18 4:30 PM - End: Nov 9, 18 9:00 PM", "Beer with Friends - Start: Nov 9, 18 8:00 PM - End: Nov 9, 18 9:30 PM", "Birthday Party - Start: Nov 10, 18 12:30 PM - End: Nov 10, 18 8:30 PM", "Dentist Appointment - Start: Nov 10, 18 1:45 PM - End: Nov 10, 18 2:30 PM", "Evening Picnic - Start: Nov 10, 18 6:00 PM - End: Nov 10, 18 7:00 PM"]
the ["Football Tailgate with John - Start: Nov 3, 18 6:14 PM - End: Nov 3, 18 9:44 PM", "Football Game - Start: Nov 3, 18 6:14 PM - End: Nov 3, 18 9:44 PM", "SF Coffee Festival - Start: Nov 6, 18 6:00 PM - End: Nov 6, 18 9:00 PM", "Evening Cookout with Friends - Start: Nov 6, 18 5:00 PM - End: Nov 6, 18 10:00 PM", "Nap Break - Start: Nov 8, 18 12:56 PM - End: Nov 8, 18 1:30 PM", "Lunch with Friends - Start: Nov 8, 18 11:30 AM - End: Nov 8, 18 12:59 PM", "Beer with Friends - Start: Nov 9, 18 8:00 PM - End: Nov 9, 18 9:30 PM", "TV Show Marathon - Start: Nov 9, 18 4:30 PM - End: Nov 9, 18 9:00 PM", "Dentist Appointment - Start: Nov 10, 18 1:45 PM - End: Nov 10, 18 2:30 PM", "Birthday Party - Start: Nov 10, 18 12:30 PM - End: Nov 10, 18 8:30 PM", "Birthday Party - Start: Nov 10, 18 12:30 PM - End: Nov 10, 18 8:30 PM"]

2nd Array

conflictingEvents # 11
the ["Football Tailgate with John - Start: Nov 3, 18 6:14 PM - End: Nov 3, 18 9:44 PM", "Football Game - Start: Nov 3, 18 6:14 PM - End: Nov 3, 18 9:44 PM", "SF Coffee Festival - Start: Nov 6, 18 6:00 PM - End: Nov 6, 18 9:00 PM", "Evening Cookout with Friends - Start: Nov 6, 18 5:00 PM - End: Nov 6, 18 10:00 PM", "Nap Break - Start: Nov 8, 18 12:56 PM - End: Nov 8, 18 1:30 PM", "Lunch with Friends - Start: Nov 8, 18 11:30 AM - End: Nov 8, 18 12:59 PM", "Beer with Friends - Start: Nov 9, 18 8:00 PM - End: Nov 9, 18 9:30 PM", "TV Show Marathon - Start: Nov 9, 18 4:30 PM - End: Nov 9, 18 9:00 PM", "Dentist Appointment - Start: Nov 10, 18 1:45 PM - End: Nov 10, 18 2:30 PM", "Birthday Party - Start: Nov 10, 18 12:30 PM - End: Nov 10, 18 8:30 PM", "Birthday Party - Start: Nov 10, 18 12:30 PM - End: Nov 10, 18 8:30 PM"]

cell code for the table Which displays the first array.

cell.textLabel!.text = stringArray[indexPath.row]

I want to highlight the elements of the second array in this table

I'm new to swift If you guys can help It would mean alot.


回答1:


How about this?


class TableViewController: UITableViewController {

    var listA = ["Event 1", "Event 2", "Event 3", "Event 4"]
    var listB = ["Event 1", "Event 3"]

    override func numberOfSections(in tableView: UITableView) -> Int {
        return 1
    }

    override func tableView(_ tableView: UITableView,
                            numberOfRowsInSection section: Int) -> Int {
        return self.listA.count
    }


    override func tableView(_ tableView: UITableView,
                            cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView
            .dequeueReusableCell(withIdentifier: "reuseIdentifier", for: indexPath)

        let event = self.listA[indexPath.row]
        cell.textLabel?.text = event
        cell.backgroundColor = self.listB.contains(event) ? .secondarySystemFill : nil

        return cell
    }

}



回答2:


import UIKit

class ViewController: UIViewController,UITableViewDelegate,UITableViewDataSource {


@IBOutlet weak var myTableViewOutlet: UITableView!



let myArray1 =  ["Bicycling with Friends - Start: Nov 1, 18 6:00 AM - End: Nov 1, 18 9:30 AM", "Yoga - Start: Nov 1, 18 6:00 PM - End: Nov 1, 18 7:33 PM", "Local Pub with Friends - Start: Nov 1, 18 7:33 PM - End: Nov 1, 18 11:00 PM", "Football Game - Start: Nov 3, 18 6:14 PM - End: Nov 3, 18 9:44 PM", "Football Tailgate with John - Start: Nov 3, 18 6:14 PM - End: Nov 3, 18 9:44 PM", "Volleyball With Friends - Start: Nov 6, 18 12:00 PM - End: Nov 6, 18 1:26 PM", "Boba Tea Grand Opening - Start: Nov 6, 18 1:27 PM - End: Nov 6, 18 2:00 PM", "Evening Cookout with Friends - Start: Nov 6, 18 5:00 PM - End: Nov 6, 18 10:00 PM", "SF Coffee Festival - Start: Nov 6, 18 6:00 PM - End: Nov 6, 18 9:00 PM", "Roller Derby - Start: Nov 7, 18 12:00 PM - End: Nov 7, 18 2:30 PM", "Rock Concert - Start: Nov 7, 18 6:30 PM - End: Nov 7, 18 11:00 PM", "Lunch with Friends - Start: Nov 8, 18 11:30 AM - End: Nov 8, 18 12:59 PM", "Nap Break - Start: Nov 8, 18 12:56 PM - End: Nov 8, 18 1:30 PM", "Basketball Game - Start: Nov 8, 18 7:30 PM - End: Nov 8, 18 10:30 PM", "Lunch Meeting - Start: Nov 9, 18 12:30 PM - End: Nov 9, 18 2:30 PM", "Free Donuts - Start: Nov 9, 18 3:00 PM - End: Nov 9, 18 4:00 PM", "TV Show Marathon - Start: Nov 9, 18 4:30 PM - End: Nov 9, 18 9:00 PM", "Beer with Friends - Start: Nov 9, 18 8:00 PM - End: Nov 9, 18 9:30 PM", "Birthday Party - Start: Nov 10, 18 12:30 PM - End: Nov 10, 18 8:30 PM", "Dentist Appointment - Start: Nov 10, 18 1:45 PM - End: Nov 10, 18 2:30 PM", "Evening Picnic - Start: Nov 10, 18 6:00 PM - End: Nov 10, 18 7:00 PM"]
    let myArray2 = ["Football Tailgate with John - Start: Nov 3, 18 6:14 PM - End: Nov 3, 18 9:44 PM", "Football Game - Start: Nov 3, 18 6:14 PM - End: Nov 3, 18 9:44 PM", "SF Coffee Festival - Start: Nov 6, 18 6:00 PM - End: Nov 6, 18 9:00 PM", "Evening Cookout with Friends - Start: Nov 6, 18 5:00 PM - End: Nov 6, 18 10:00 PM", "Nap Break - Start: Nov 8, 18 12:56 PM - End: Nov 8, 18 1:30 PM", "Lunch with Friends - Start: Nov 8, 18 11:30 AM - End: Nov 8, 18 12:59 PM", "Beer with Friends - Start: Nov 9, 18 8:00 PM - End: Nov 9, 18 9:30 PM", "TV Show Marathon - Start: Nov 9, 18 4:30 PM - End: Nov 9, 18 9:00 PM", "Dentist Appointment - Start: Nov 10, 18 1:45 PM - End: Nov 10, 18 2:30 PM", "Birthday Party - Start: Nov 10, 18 12:30 PM - End: Nov 10, 18 8:30 PM", "Birthday Party - Start: Nov 10, 18 12:30 PM - End: Nov 10, 18 8:30 PM"]

override func viewDidLoad() {
    super.viewDidLoad()
    // Do any additional setup after loading the view, typically from a nib.
}

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

func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    return myArray1.count
}

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell = myTableViewOutlet.dequeueReusableCell(withIdentifier: "cell", for: indexPath) as! myTableViewCellClass
    cell.myLabel.text = myArray1[indexPath.row]

    return cell
}
func tableView(_ tableView: UITableView, willDisplay cell: UITableViewCell, forRowAt indexPath: IndexPath) {

    var searchValue = myArray1[indexPath.row]
    for v in myArray2
    {
        if v == searchValue {
            let highlightColor: UIColor? = UIColor(white: 0.7, alpha: 0.5)
            cell.backgroundColor = highlightColor
        }
    }
}





}


来源:https://stackoverflow.com/questions/60154926/swift-change-cell-background-colors-in-some-tableview-cells

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!