0

I have a table view and each cell is made up of a view an image and two labels. when I create each cell i call a function called createPostActivityCell. This function returns the cell with a subview and other UI elements. when I call the function I also specify a title for that cell that I want it to display I specify it from an array of dictionaries. it works well at first but after scrolling up and down the titles start to overlay and create a weird effect.

MY cell class:

import UIKit

class ActivityCell: UITableViewCell{

@IBOutlet weak var cellView: UIView!

override func awakeFromNib() {
    super.awakeFromNib()
    // Initialization code
}

override func setSelected(_ selected: Bool, animated: Bool) {
    super.setSelected(selected, animated: animated)

    // Configure the view for the selected state
}

func createPostActivityCell(cell: ActivityCell, cellImage: UIImage, cellIndex: IndexPath, cellTitle: String) -> ActivityCell{
    let cell = cell
    let profileObject = ProfileObject(profileImage: cellImage, profileName: "Zachary Gameiro", tag: cellIndex.row, isClickable: false, x: 5, y: 5, width: 50, height: 50)
    cellView.layer.masksToBounds = true
    cellView.layer.cornerRadius = 30

    // Title Label
    let titleLabel = UILabel(frame: CGRect(x: 60, y: 10, width: 200, height: 20))
    titleLabel.text = cellTitle
    titleLabel.font = UIFont(name: "Roboto-Regular", size: 15.0)

    // Description Label
    let descriptionLabel = UILabel(frame: CGRect(x: 60, y: 30, width: 200, height: 20))
    descriptionLabel.text = "My Party is at 6:00"
    descriptionLabel.font = UIFont(name: "Roboto-Light", size: 12.0)

    cellView.addSubview(profileObject.createView())
    cellView.addSubview(titleLabel)
    cellView.addSubview(descriptionLabel)
    return cell
}


}

this I how I call the cell

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    var cell = activityTableView.dequeueReusableCell(withIdentifier: "activityCell") as! ActivityCell
    cell = cell.createPostActivityCell(cell: cell, cellImage: #imageLiteral(resourceName: "cosmonauts_playing_tennis_kit8-net_1x.png"), cellIndex: indexPath, cellTitle: self.activityCellData[indexPath.row]["title"] as! String)
    return cell
}

and the is the dicitonary

var activityCellData:[[String:Any]] = [["title":"Party"],["title":"wdq"],["title":"Party"],["title":"cow"],["title":"Party"],["title":"Party"],["title":"Party"],["title":"Party"],["title":"Party"],["title":"Party"]]

This Is What I Want To Happen This Is What Happens After A Couple Scrolls Up And Down

  • That happens because you keep adding subviews on cellView each time cellFoRow() method os calling. You did not catch the reuse part of the tableView. When upper cell dissapears (scrolling down) the tableView takes Existing cell (with old data on it ) and send it to cellFoRow method. And you must update this cell with new data, not creating new one – tereks Feb 19 '18 at 02:10
  • How do I do that – Zachary Gameiro Feb 19 '18 at 02:11
  • like https://stackoverflow.com/questions/34521953/how-to-reuse-cells-in-a-uitableview or https://stackoverflow.com/questions/25257378/how-to-use-reusable-cells-in-uitableview-for-ios – tereks Feb 19 '18 at 02:14

0 Answers0