为什么我在滚动后在UITableViewCell中获得双重内容? (迅速)

时间:2014-10-30 20:00:37

标签: ios uitableview swift

为什么我在滚动后在UITableViewCell中获得双重文本?

以下是初始化单元格的代码:

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

    cell.backgroundColor = UIColor.clearColor()
    cell.setCell(DanKanali, indexPath: indexPath)

    return cell

}

func setCell(danKanali:[DatePS], indexPath: NSIndexPath)
{

    let vseOddaje = danKanali[indexPath.section].programi[indexPath.row].oddaje
    let program:Program = danKanali[indexPath.section].programi[indexPath.row]
    logoWrapper.frame = CGRectMake(5, CGFloat(5+(vseOddaje.count-1) * 10), logoWrapper.frame.width, logoWrapper.frame.height)
    var sl = program.id.intValue
    sl = sl + 1

    var slik = String(sl)
    mali_logo.image = UIImage(contentsOfFile: boundle.pathForResource(slik, ofType: "png")!)

    ime_programa.text = program.ime
    var i = 0
    var base = 15
    if program.oddaje.count == 1
    {
        base = 30 //če je samo 1 show, da postavi na sredino celice
    }
    for oddaja in program.oddaje
    {
        var showWrapper = UIView(frame: CGRectMake(100, CGFloat(base + i * 45), 300, 40))
        var naslov = UILabel(frame: CGRectMake(0, 0, 300, 20))
        var kategorija = UILabel(frame: CGRectMake(0, 17, 250, 20))
        naslov.font = UIFont.boldSystemFontOfSize(14)
        naslov.text = "\(oddaja.ura) - \(oddaja.naslov)"
        naslov.textColor = UIColor.whiteColor()

        kategorija.text = "\(oddaja.kategorija) / \(oddaja.podkategorija)"
        kategorija.textColor = UIColor.whiteColor()
        kategorija.font = UIFont.systemFontOfSize(13)

        showWrapper.addSubview(naslov)
        showWrapper.addSubview(kategorija)
        self.contentView.addSubview(showWrapper)
        i++
    }
}

这就是我得到的:

Problem

我已经尝试过if if == nil,但是在swift中这种情况不再支持了,或者我错了吗?

1 个答案:

答案 0 :(得分:2)

setCell中,您正在调用addSubview,但在滚动时重新配置单元格时,您不会删除或重复使用它们。

修改: 添加到IskanjeShowTableViewCell NSMutableArrayshowWrapper@interface IskanjeShowTableViewCell : UITableViewCell // ... @property (strong) NSMutableArray *showWrappers; @end - (void)setCell:(...) { // ... [self.showWrappers makeObjectsPerformSelector:@selector(removeFromSuperview)]; [self.showWrappers removeAllObjects]; for (id oddaja in program.oddaje) { // ... [self.showWrappers addObject:showWrapper]; } } 个对象。 [目标-C遵循]

{{1}}