Swift:每次重新加载都会一次又一次地添加tableview单元格内容?

时间:2016-08-25 01:51:01

标签: ios swift uitableview

好的,我相当这个Objective C问题有同样的问题= Cell Label text overlapping in cells但我在Swift中找不到任何答案。我也是tableviews / cells的新手,我想知道这样做的正确方法,因为我做错了 -

我在tableview中创建了自定义单元格,这是我在storyboard中创建的。我需要以编程方式添加我的单元格内容(标签等)。我在这里做了 -

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCellWithIdentifier("eventCell", forIndexPath: indexPath) as! EventTableCellTableViewCell
//        cell.eventTitle.text = names[indexPath.row]
//        cell.eventDescription.text = descriptions[indexPath.row]

        cell.contentView.clipsToBounds = false

        //cell UIX
        let eventTitleLabel = UILabel()
        let dateLabel = UILabel()
        let authorLabel = UILabel()
        let locationLabel = UILabel()

        let categoryView = UIImageView()

        //border
        let botBorder: CALayer = CALayer()
        botBorder.frame = CGRectMake(0.0, cell.frame.height-1, cell.frame.width, 1.0)
        botBorder.backgroundColor = colorWithHexString("#C5C7C9").CGColor

        //initalize cell items
        eventTitleLabel.text = names[indexPath.row]
        eventTitleLabel.frame = CGRectMake(0, 0, cell.frame.width * 0.5, cell.frame.height * 0.3)
        eventTitleLabel.tag = indexPath.row
        eventTitleLabel.textAlignment = .Left
        eventTitleLabel.font = UIFont(name: "Montserrat-Bold", size: screenSize.height * (24/568))
        eventTitleLabel.textColor = UIColor.blackColor()
        eventTitleLabel.center = CGPointMake(cell.contentView.frame.width * 0.35, cell.contentView.frame.height * 0.35)


        dateLabel.textColor = colorWithHexString("#C5C7C9")
        let dateString = "\(dates[indexPath.row]) \(times[indexPath.row])"
        dateLabel.text = dateString
        dateLabel.frame = CGRectMake(0, 0, cell.frame.width * 0.5, cell.frame.height * 0.3)
        dateLabel.tag = indexPath.row
        dateLabel.textAlignment = .Left
        dateLabel.font = UIFont(name: "Montserrat-Regular", size: screenSize.height * (10/568))
        dateLabel.center = CGPointMake(cell.contentView.frame.width * 0.35, cell.contentView.frame.height * 0.6)

        //for setting bottom label

        //Code sets label (yourLabel)'s text to "Tap and hold(BOLD) button to start recording."
        let boldAttribute = [
            //You can add as many attributes as you want here.
            NSFontAttributeName: UIFont(name: "Montserrat-Bold", size: 11.0)!]

        let regularAttribute = [
            NSFontAttributeName: UIFont(name: "Montserrat-Regular", size: 11.0)!]

        let beginningAttributedString = NSAttributedString(string: authors[indexPath.row], attributes: boldAttribute )
        //let boldAttributedString = NSAttributedString(string: locationNames[indexPath.row], attributes: boldAttribute)
        let boldAttributedString = NSAttributedString(string: "Monterey, CA USA", attributes: regularAttribute)
        let fullString =  NSMutableAttributedString()

        fullString.appendAttributedString(beginningAttributedString)
        fullString.appendAttributedString(NSAttributedString(string: "  ", attributes: regularAttribute)) //space
        fullString.appendAttributedString(boldAttributedString)
        //------

        authorLabel.attributedText = fullString

        authorLabel.textColor = colorWithHexString("#C5C7C9")
        authorLabel.frame = CGRectMake(0, 0, cell.frame.width, cell.frame.height * 0.3)
        authorLabel.tag = indexPath.row
        authorLabel.textAlignment = .Left
        authorLabel.center = CGPointMake(cell.contentView.frame.width * 0.5, cell.contentView.frame.height * 0.8)

        categoryView.frame = CGRectMake(0, 0, screenSize.width * (50/screenSize.width), screenSize.width * (50/screenSize.width))
        categoryView.layer.cornerRadius = categoryView.frame.width * 0.5
        categoryView.center = CGPointMake(cell.contentView.frame.width * 0.7, cell.contentView.frame.height * 0.35)
        categoryView.backgroundColor = colorWithHexString("#3dccff")
        cell.contentView.addSubview(categoryView)


        cell.contentView.addSubview(eventTitleLabel)
        cell.contentView.addSubview(dateLabel)
        cell.contentView.addSubview(locationLabel)
        cell.contentView.addSubview(authorLabel)
        cell.contentView.layer.addSublayer(botBorder)

        print("called cell")

        return cell
    }

这是第一次有效。但是我从print到console中了解到,每次滚动时都会调用它,并且在我添加新项目后会在我的tableview中调用新单元格。当发生这种情况时,我会发生这种重叠 -

enter image description here

我该如何解决这个问题?我也查看了TableViewCell is piled up and appear again and again,并尝试将cell.contentView.removeFromSuperView()放在此函数的开头,这样就可以清除旧内容,但这样就无法显示任何内容。

以编程方式添加内容的正确方法是什么?

1 个答案:

答案 0 :(得分:3)

tableview单元格被循环使用,因此每次呈现一个单元格时,它将包含最后放入的单元格,您需要适当地处理一个填充了您放入的标签的单元格。可能应该有某个单元的init方法,每个 new 单元只调用一次,并在单元格被回收时被忽略,然后只需编辑标签和其他正常情况。这种功能应该内置在单元格自定义类本身而不是cellForRowAtIndexPath