Swift:每次重新加载时都会一次又一次地添加tableview单元格内容? [英] Swift: tableview cell content is added again and again with each reload?

查看:43
本文介绍了Swift:每次重新加载时都会一次又一次地添加tableview单元格内容?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

好吧,我认为这个 Objective C 问题有同样的问题 =

我该如何解决这个问题?我还看了TableViewCell 堆积起来,一次又一次地出现 并尝试将 cell.contentView.removeFromSuperView() 放在此函数的开头,以便清除旧内容,但这导致绝对没有显示任何内容.

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

解决方案

tableview 单元格会被回收,因此每次呈现一个单元格时,它都会有你最后放入的任何东西,你需要适当地处理一个单元格返回填充了您放入的标签.可能应该有某种单元格的 init 方法,每个 new 单元格只调用一次,并且在单元格回收时被忽略,然后只需编辑标签和其他一切正常.这种功能应该内置到单元格自定义类本身中,而不是在 cellForRowAtIndexPath

Ok, I am fairly this Objective C question had the same problem = Cell Label text overlapping in cells but I haven't found any answers in Swift. Im also very new to tableviews/cells and would just like to know the proper way to do this as clearly Im doing it wrong-

I have custom cells in my tableview that I created in storyboard. I need to add the content of my cells (labels, etc) programmatically. I have done this here -

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
    }

And this works the first time. However I learned from the print to console that this is called every time you scroll, and also after I add new items that take up new cells in my tableview. When that happens I get this overlapping -

How do I fix this? I looked also at TableViewCell is piled up and appear again and again and tried putting cell.contentView.removeFromSuperView() at the beginning of this function so it would clear out the old content but that resulted in absolutely nothing showing up.

What is the right way to add content programmatically?

解决方案

The tableview cells are recycled, therefore each time a cell is presented its going to have whatever you put in it last, you will need to appropriately handle a cell that comes back filled with the labels you have put in. Probably should have some kind of init method of the cell that is called only once per new cell, and is ignored when the cell is recycled, then just edit the labels and what ever else as normal. This kind of functionality should be built into the cells custom class itself instead of inside the cellForRowAtIndexPath

这篇关于Swift:每次重新加载时都会一次又一次地添加tableview单元格内容?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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