Ok, ich bin ziemlich diese Objective C Frage hatte das gleiche Problem = Cell Label text overlapping in cells, aber ich habe keine Antworten in Swift gefunden. Ich bin auch sehr neu zu Tableviews/Zellen und möchte nur den richtigen Weg, dies als klar zu tun tun es falsch -Swift: Tabellenansicht Zellinhalt wird immer wieder mit jedem Neuladen hinzugefügt?
Ich habe benutzerdefinierte Zellen in meiner Tabellenansicht, die ich im Storyboard erstellt. Ich muss den Inhalt meiner Zellen (Etiketten, usw.) programmatisch hinzufügen. Ich habe dies hier getan -
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
}
Und das funktioniert das erste Mal. Ich habe jedoch vom Ausdruck auf der Konsole gelernt, dass dies jedes Mal aufgerufen wird, wenn Sie blättern, und auch nachdem ich neue Elemente hinzugefügt habe, die neue Zellen in meiner Tabellenansicht aufnehmen. Wenn das passiert ich diese Überlappung erhalten -
Wie kann ich dieses Problem beheben? Ich schaute auch auf TableViewCell is piled up and appear again and again und versuchte, cell.contentView.removeFromSuperView()
am Anfang dieser Funktion zu setzen, damit es den alten Inhalt löschen würde, aber das resultierte in absolut nichts, das auftauchte.
Was ist der richtige Weg, um Inhalte programmatisch hinzuzufügen?
eingebaut werden Warum ist all dieser Code in Ihrer 'cellForRowAtIndexPath' Funktion? Sie haben eine benutzerdefinierte Zellklasse. Der größte Teil dieses Codes gehört in diese Klasse. Gib den Code dorthin, wo er hingehört. – rmaddy
Fügen Sie der Zelle in 'cellForRowAtIndexPath' keine Unteransichten hinzu. Lassen Sie jede Zelle ihre Beschriftungen/Unteransichten (entweder in Interface Builder oder programmatisch in 'init()' entworfen) und konfigurieren Sie sie dann **, indem Sie jedes Mal den 'Text' der Beschriftungen usw. aktualisieren. –
Würdest du meine Frage http://stackoverflow.com/questions/39148460/swift-cant-configure-custom-table-cell-programmatical-init-not- aufgerufen Ich kann nicht scheinen, nichts in der benutzerdefinierten Klasse zu konfigurieren – skyguy