Ios Swift:tableview单元格内容在每次重新加载时都会不断添加?
好的,我很高兴这个客观的C问题也有同样的问题,但我在Swift中没有找到任何答案。我对TableView/cells也很陌生,只想知道正确的方法,因为我显然做错了- 我在“情节提要”中创建的tableview中有自定义单元格。我需要以编程方式添加单元格的内容(标签等)。我在这里做过-Ios Swift:tableview单元格内容在每次重新加载时都会不断添加?,ios,swift,uitableview,Ios,Swift,Uitableview,好的,我很高兴这个客观的C问题也有同样的问题,但我在Swift中没有找到任何答案。我对TableView/cells也很陌生,只想知道正确的方法,因为我显然做错了- 我在“情节提要”中创建的tableview中有自定义单元格。我需要以编程方式添加单元格的内容(标签等)。我在这里做过- func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell
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 to console中了解到,每当您滚动时,以及在我在tableview中添加占用新单元格的新项目后,都会调用此函数。当这种情况发生时,我得到这个重叠-
我该如何解决这个问题?我还查看并尝试将cell.contentView.removeFromSuperView()
放在这个函数的开头,这样可以清除旧内容,但结果是什么也没有显示
以编程方式添加内容的正确方法是什么?tableview单元格是循环使用的,因此每次显示单元格时,都会显示您最后放入的内容,您需要适当地处理返回的单元格,该单元格中填充了您放入的标签。可能应该有某种类型的单元初始化方法,每个新单元只调用一次,当单元被回收时会被忽略,然后只需编辑标签和其他正常的内容。这种功能应该内置到cells自定义类中,而不是在
cellForRowAtIndexPath
中。tableview单元格是循环使用的,因此每次显示单元格时,都会有您最后放入的内容,您需要适当地处理一个返回的单元格,该单元格中填充了您放入的标签。可能应该有某种类型的单元初始化方法,每个新单元只调用一次,当单元被回收时会被忽略,然后只需编辑标签和其他正常的内容。这种功能应该内置到cells自定义类本身中,而不是在CellForRowatineXpath
中。为什么所有这些代码都在CellForRowatineXpath
函数中?您有一个自定义单元类。这段代码的大部分都属于那个类。将代码放在它所属的位置。不要将子视图添加到cellforrowatinexpath
中的单元格中。让每个单元格都有其标签/子视图(在Interface Builder中设计,或在init()
上以编程方式设计),然后通过更新标签“文本”
,对其进行配置,每次都是这样。你能看看我的问题吗?我似乎无法在自定义类中配置任何东西。为什么所有这些代码都在你的cellforrowatinexpath
函数中?您有一个自定义单元类。这段代码的大部分都属于那个类。将代码放在它所属的位置。不要将子视图添加到cellforrowatinexpath
中的单元格中。让每个单元格都有自己的标签/子视图(可以在Interface Builder中设计,也可以在init()
上编程),然后通过每次更新标签“text
”等来配置它们。请看我的问题,我似乎无法在自定义类中配置任何内容OK,我在尝试在init中执行任何操作时遇到问题,因为它没有被调用。。你看一下好的,我在尝试在init中做任何事情时遇到问题,因为它没有被调用。。你能看看吗