M
M
MarsFM2018-03-19 18:12:27
iOS
MarsFM, 2018-03-19 18:12:27

How to set text color for first two rows in tableView in sections?

Plz tell me what am I doing wrong?
It is necessary to paint over the first two digits in each section, but when scroll-e all digits are painted over.
Before scroll-a, everything is fine
5aafd1cbf3687940490366.png
After scroll-a is gone, all numbers are painted over(
5aafd1e3c621d858978717.png
TableViewCell.swift

let numberTeam: UILabel = {
        let title = UILabel()
        title.textAlignment = .center
        title.text = ""
        return title
 }()

func configureWithData(teams: ,
                           gamesPlayes: ,
                           rank: ,
                           points:  ,
                           country: ,
                           number: [Int],
                           indexPath: IndexPath) {
        
        self.nameTeam.text = teams[indexPath.section][indexPath.row]
        self.pl.text = gamesPlayes[indexPath.section][indexPath.row]
        self.gd.text = rank[indexPath.section][indexPath.row]
        self.countryImage.image = UIImage(named: country[indexPath.section][indexPath.row])
        self.numberTeam.text = "\(number[indexPath.row])"
        self.pts.text = points[indexPath.section][indexPath.row]
        
        roundNumber(number: number, index: indexPath.row) // Settings number
    }
    
    // Function settings number
    private func roundNumber(number: [Int], index: Int) {
        let strNumber = "\(number[index])"
        if strNumber == "1" || strNumber == "2" {
            self.numberTeam.layer.backgroundColor = #colorLiteral(red: 0.3668528247, green: 0.9922668147, blue: 0.4948421398, alpha: 0.7097335188)
            self.numberTeam.layer.cornerRadius = 12
            self.numberTeam.layer.borderColor = #colorLiteral(red: 0.7450980544, green: 0.1568627506, blue: 0.07450980693, alpha: 1)
            self.numberTeam.clipsToBounds = true
        }
    }

TableView.swift
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "cellId", for: indexPath) as! GroupsTableViewCell
        cell.numberTeam.text = "\(indexPath.row + 1)"
        cell.configureWithData(teams: teams,
                               gamesPlayes: gamesPlayes,
                               rank: rank,
                               points: points,
                               country: countrys,
                               number: number,
                               indexPath: indexPath)
        
        return cell
    }

Answer the question

In order to leave comments, you need to log in

1 answer(s)
I
Igor Cherny, 2018-03-19
@sportredwhite

because cells are reusable. In the cellForRow atIndexPath method, you can get a cell that has had its number filled in from a previous use. One possible solution: add else to if strNumber == "1" || strNumber == "2" and remove coloring there

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question