Ссылка на ячейку вне функции cellForRowAIndexPatht - PullRequest
0 голосов
/ 04 марта 2020

Я спрашивал об этом раньше, но думаю, что мой вопрос был неясным. Поэтому я переписал в надежде, что кто-то может помочь.

Я пытаюсь изменить цвет фона некоторых UILabels на основе значения одной из меток.

У меня 6 ярлыков и если значение одного из ярлыков ie. type_BG «Пост», тогда фон всех остальных меток станет желтым.

Для этого я создал пользовательскую функцию с именем «Background_Color». Именно здесь происходят коды для изменения цвета фона надписей. Примерно так:

if cell.type_BG.text == «Пост» {

cell.type_BG.backgroundColor = UIColor.yellow

cell.read_BG.backgroundColor = UIColor.yellow

cell.read_index.backgroundColor = UIColor.yellow

                …. and so on.
}

Когда я запускаю коды, ничего не происходит. Я считаю, что способ ссылки на ячейку, указывающую на метку, неверен. Если так, как я должен исправить это?

Мой фактический код:

импорт UIKit импорт CoreData

класс BGTest008V C: UIViewController, UITableViewDelegate, UITableViewDataSource, NSFetchedResultsControlDell 1021 *

`fun c numberOfSections (в tableView: UITableView) -> Int {return 1}

fun c tableView (_ tableView: UITableView, раздел numberOfRowsInSection: Int) -> Int {

  return BGTest008.count
  }

весело c tableView (_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {`

let cellIdentifier = "BGTest008"

let cell = tableView008.dequeueReusableCell(withIdentifier: cellIdentifier, for: indexPath) as! VKTestTableViewCell

    if let date_vk = BGTest008[indexPath.row].date_BG{

                let date_Format = DateFormatter()
                    date_Format.dateFormat = "yyy-mm-dd hh:mm a"
                let date_Format_Print = DateFormatter()
                    date_Format_Print.dateFormat = "EEE, dd-MMM"
                let date_label = date_Format_Print.string(from: date_vk)

                    cell.date_BG.text = date_label
                }

if let time_vk = BGTest008[indexPath.row].time_BG{

                let time_Format = DateFormatter()
                    time_Format.dateFormat = "yyy-mm-dd hh:mm a"
                    time_Format.timeZone = TimeZone(secondsFromGMT: 0)
                let time_to_str = time_Format.string(from: time_vk)
                let date_to_str = time_Format.date(from: time_to_str)
                    time_Format.dateFormat = "hh:mm:a"
                let time_back_to_string = time_Format.string(from: date_to_str!)
                let time_label = time_back_to_string

                    cell.time_BG.text = time_label
                }

let index_vk = BGTest008[indexPath.row].index_BG
                    cell.index_BG.text = "\(String(describing: index_vk))"

let BG_level = BGTest008[indexPath.row].read_BG
                    cell.read_BG.text = "\(String(describing: BG_level))"

let type_BG = BGTest008[indexPath.row].type_BG

            if let type_BG1 = type_BG{
                cell.type_BG.text = "\(String(describing: type_BG1))"

                if cell.type_BG.text == "Fasting"{
                    Background_Color()
            }else {
                    print("cell is not Fasting")
               // cell.type_BG.text = ""
                }
            }

возвращаемая ячейка

}

веселье c Background_Color () {

    let indexPath = indexPath_X
    let cell = tableView008.dequeueReusableCell(withIdentifier: "BGTest008", for: indexPath) as! VKTestTableViewCell
         cell.type_BG.backgroundColor = UIColor.yellow
        cell.index_BG.backgroundColor = UIColor.yellow
        cell.date_BG.backgroundColor = UIColor.yellow
        cell.time_BG.backgroundColor = UIColor.yellow
        cell.notes_BG.backgroundColor = UIColor.yellow
        cell.read_BG.backgroundColor = UIColor.yellow
}

func getRecordsCount() {
   let fetchRequest = NSFetchRequest<NSFetchRequestResult>(entityName: "BGTest008")

    if let appDelegate = (UIApplication.shared.delegate as? AppDelegate) {
        let context = appDelegate.persistentContainer.viewContext

   do {
       let recordCount = try context.count(for: fetchRequest)
                self.recordCount1 = recordCount
                print("recordCount = ", recordCount)
        } catch {
            print(error.localizedDescription)
        }
    }
}

}

Заранее спасибо.

...