在滚动UITableView上重复UIFont和UIColor [英] Repeating UIFont and UIColor on Scroll UITableView

查看:73
本文介绍了在滚动UITableView上重复UIFont和UIColor的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我遇到了问题,我创建了一个简单的mvp来向大家展示.

I`m having a problem and I created a simple mvp to show for you guys.

当我在桌子上滚动时,其他索引会获取颜色和字体.

When I scroll on the table other index gets the color and the font.

示例:

import UIKit

class ViewController: UIViewController,UITableViewDataSource,UITableViewDelegate{
    var texto: [String] = []

    @IBOutlet weak var tableView: UITableView!

    override func viewDidLoad() {
        super.viewDidLoad()

        tableView.delegate = self
        tableView.dataSource = self
        tableView.rowHeight = UITableViewAutomaticDimension
        tableView.estimatedRowHeight = 44.0

        texto.append("Title 1")
        texto.append("Content 1")
        texto.append("Read More 1")

        texto.append("Title 2")
        texto.append("Content 2")
        texto.append("Read More 2")

        texto.append("Title 3")
        texto.append("Content 3")
        texto.append("Read More 3")

        texto.append("Title 4")
        texto.append("Content 4")
        texto.append("Read More 4")

        texto.append("Title 5")
        texto.append("Content 5")
        texto.append("Read More 5")

        texto.append("Title 6")
        texto.append("Content 6")
        texto.append("Read More 6")

        texto.append("Title 7")
        texto.append("Content 7")
        texto.append("Read More 7")

        self.tableView!.reloadData()
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()

    }

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

    func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCellWithIdentifier("cell2")! as UITableViewCell

        if [0,3].contains(indexPath.row){
            cell.textLabel?.font = UIFont(name: "Helvetica", size: 28)
        }
        if [2,5].contains(indexPath.row){
            cell.textLabel?.textColor = UIColor(hue: 0.575, saturation: 1, brightness: 0.89, alpha: 1.0)

        }
        cell.textLabel?.text = self.texto[indexPath.row]
        return cell
    }


}

在我写的cellForRowAtIndexPath中,如果texto的索引为0或3,则它们的字体会更改,但是当我使用滚动时,其他索引的索引也将获得相同的字体.

In the cellForRowAtIndexPath I write, if index of texto is 0 or 3 they get font changed, but when i use the scroll other index`s get the same font too.

推荐答案

单元被重用.当为一个条件设置单元格的属性时,您需要针对其他条件将其重置.将您的代码更新为类似这样的内容:

Cells get reused. When you set a cell's attribute for one condition, you need to reset it for the other conditions. Update your code to something like this:

    if [0,3].contains(indexPath.row){
        cell.textLabel?.font = UIFont(name: "Helvetica", size: 28)
    } else {
        cell.textLabel?.font = // Some default font
    }
    if [2,5].contains(indexPath.row){
        cell.textLabel?.textColor = UIColor(hue: 0.575, saturation: 1, brightness: 0.89, alpha: 1.0)
    } else {
        cell.textLabel?.textColor = // some default color
    }

这篇关于在滚动UITableView上重复UIFont和UIColor的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆