在swift中滚动时自定义UITableViewCell更改字体

时间:2015-01-28 08:35:40

标签: ios uitableview swift

我有一个自定义UITableViewCell,其中有UILabel

这是我的代码:

 override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {

    var cell = tableView.dequeueReusableCellWithIdentifier(
        "PromoCell", forIndexPath: indexPath) as? SchedaCompletaCellTVC // it's my custom tableview cell

    if (cell == nil) {
        cell = SchedaCompletaCellTVC(style: UITableViewCellStyle.Default,reuseIdentifier:"PromoCell")
    }

    // Configure the cell...
    switch indexPath.row {
    case 0:
        cell!.valueLabel.text = String(htmlEncodedString: company.name)
        cell!.valueLabel.font = UIFont(name: "Helvetica-Bold", size: 17.0)

    case 1:
        cell!.valueLabel.text = String(htmlEncodedString: company.description)
    case 2:
        cell!.valueLabel.text = "Contatti"
        cell!.valueLabel.font = UIFont(name: "Helvetica-Bold", size: 15.0)
    case 3:
        cell!.valueLabel.text = "Indirizzo:  \(String(htmlEncodedString: company.address)),\(String(htmlEncodedString: company.city)),\(String(htmlEncodedString: company.province)) "
    case 4:
        var telString:NSString = String(htmlEncodedString: company.phone)
        cell!.valueLabel.text = "Tel: \(telString)"

    case 5:
        var email = String(htmlEncodedString: company.email)
        cell!.valueLabel.text = "Email: \(email)"

    case 6:
        var sitoweb = String(htmlEncodedString: company.site)
        cell!.valueLabel.text = "Sito: \(sitoweb)"

    case 7:
        cell!.valueLabel.text = "Fax: \(String(htmlEncodedString: company.fax))"
    default:
        cell!.valueLabel.text = ""

    }


    return cell!
}

当我向下滚动时,单元格会出错,并且每次都会更改。

希望有人可以提供帮助!谢谢。

1 个答案:

答案 0 :(得分:2)

看这里:

var cell = tableView.dequeueReusableCellWithIdentifier(
        "PromoCell", forIndexPath: indexPath) as? SchedaCompletaCellTVC // it's my custom tableview cell

if (cell == nil) {
    cell = SchedaCompletaCellTVC(style: UITableViewCellStyle.Default,reuseIdentifier:"PromoCell")
}

您正在重复使用该单元格。在某些单元格中,您正在设置字体,而在某些单元格中,您没有对字体属性进行任何更改。

switch indexPath.row {
    case 0:
        cell!.valueLabel.text = String(htmlEncodedString: company.name)
        cell!.valueLabel.font = UIFont(name: "Helvetica-Bold", size: 17.0)

    case 1:
        cell!.valueLabel.text = String(htmlEncodedString: company.description)
    case 2:
        cell!.valueLabel.text = "Contatti"
        cell!.valueLabel.font = UIFont(name: "Helvetica-Bold", size: 15.0)
    case 3:
        cell!.valueLabel.text = "Indirizzo:  \(String(htmlEncodedString: company.address)),\(String(htmlEncodedString: company.city)),\(String(htmlEncodedString: company.province)) "
    case 4:
        var telString:NSString = String(htmlEncodedString: company.phone)
        cell!.valueLabel.text = "Tel: \(telString)"

    case 5:
        var email = String(htmlEncodedString: company.email)
        cell!.valueLabel.text = "Email: \(email)"

    case 6:
        var sitoweb = String(htmlEncodedString: company.site)
        cell!.valueLabel.text = "Sito: \(sitoweb)"

    case 7:
        cell!.valueLabel.text = "Fax: \(String(htmlEncodedString: company.fax))"
    default:
        cell!.valueLabel.text = ""
}

在3,4,5,6,7中,您没有设置字体。在它将起作用的那些单元格中设置所需的字体大小。 :)