Zsolt
Zsolt

Reputation: 49

UIView inside UITableViewCell position issue - UPDATED

This is killing me.. (.mov from my issue)

I'd like to position my custom view inside my custom tableViewCell. Made this simple expression for positioning: If : indexpath.row % 2 == 0 ---> set view on the left (like x: 10, y: 10) else: to the right.

When the app launches, all the bubbles are exactly below each other. When I scroll down to the end, it repositions itself. Not all of them, but some of them.

I started my code, and haven't finished with that though , because of this tiny little thing. My code below:

TableViewController

class QuestionsTableViewController: UITableViewController {

    let c = "cell"
    var users = [User]()

    override func viewDidLoad() {
        super.viewDidLoad()
        for i in 0..<6 {
        let user = User(firstName: "\(i) Karl", lastName: "May")
            users.append(user)
        }
    }
    // MARK: - Table view data source

    override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return users.count ?? 0
    }


    override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> QuestionTableViewCell {

        let cell = tableView.dequeueReusableCellWithIdentifier(c, forIndexPath: indexPath) as! QuestionTableViewCell

          if indexPath.row % 2 == 0 {
        cell.bubleView.frame.origin =  CGPoint(x: origin.x + 10, y: origin.y + 10)
        cell.bubleView.backgroundColor = UIColor.blueColor()
    } else {
        cell.bubleView.frame.origin =  CGPoint(x: origin.x + 50, y: origin.y + 10)
        cell.bubleView.backgroundColor = UIColor.greenColor()
    }
        // Configure the cell...
        cell.nameLabel.text = users[indexPath.row].firstName + " " + users[indexPath.row].lastName

        return cell
    }
}

TableViewCell

class QuestionTableViewCell: UITableViewCell {

    @IBOutlet weak var bubleView: UIView!
    @IBOutlet weak var nameLabel: UILabel!

    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 positionBuble() {
        bubleView.frame.origin = CGPoint(x: contentView.frame.origin.y+10, y: contentView.frame.origin.y+10)
    }
}

As far as I know, this code supposed to work, right? Am I missing something on Xcode Utilites pane? I use Xcode 7.1.1

screenshot from tableview

Upvotes: 0

Views: 555

Answers (2)

Zsolt
Zsolt

Reputation: 49

So, after numerous tries, I've created an other prototype cell, in which I set the design elements and runtime attributes, then called the new cell's identifier. Although, still don't know why my other approach did not work out...

override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> QuestionTableViewCell {
        var cella: QuestionTableViewCell!

        if indexPath.row % 2 == 0 { //c is an identifier String
            let cell = tableView.dequeueReusableCellWithIdentifier(c, forIndexPath: indexPath) as! QuestionTableViewCell
            cella = cell
        } else { //b is also
            let cell = tableView.dequeueReusableCellWithIdentifier(b, forIndexPath: indexPath) as! QuestionTableViewCell
            cella = cell
        }
        // Configure the cell...
        cella.nameLabel.text = users[indexPath.row].firstName + " " + users[indexPath.row].lastName
        //cella.tralala.blabla = ...other properties
        return cella
    }

Upvotes: 0

matt
matt

Reputation: 535355

The problem is that you are missing the else:

    if indexPath.row % 2 == 0 {
        let origin = cell.bounds.origin
        //cell.positionBuble()
        cell.bubleView.frame.origin =  CGPoint(x: origin.x + 10, y: origin.y + 10)
        cell.bubleView.backgroundColor = UIColor.blueColor()
    } else {
        // what????
    }

You need this because cells are reused. A cell where indexPath.row % 2 == 0 will be used again where indexPath.row % 2 != 0, but you are not making any change, so they will all end up looking the same.

Upvotes: 4

Related Questions