3

After upgrading to Xcode 7 and iOS 9 beta, I am getting this error in one of my UITableViews. Whenever I scroll, my second label overlaps the first label in most of the cells. What is weird is that two of the current cells don't have this issue. See pictures for details.

Normal Picture

Not Normal Picture

Some screwed up

 override func viewDidAppear(animated: Bool) {

    namesArray.removeAll(keepCapacity: false)
    locationsArray.removeAll(keepCapacity: false)


    let query = PFQuery(className: "Schools")
    // query.whereKey(key: String, containedIn: [AnyObject])

    query.findObjectsInBackgroundWithBlock { (objects: [AnyObject]?, error: NSError?) -> Void in
        if error == nil {
            query.orderByAscending("schoolName")

            if let objects = query.findObjects() as? [PFObject] {

                for object in objects {

                    self.namesArray.append(object.valueForKey("schoolName") as! String)
                    self.locationsArray.append(object.valueForKey("schoolLocation") as! String)   
                    self.schoolTable.reloadData()
                }
            }
        } else {
            print("Shit, it didn't work...")
        }
    }
}

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

    let cell:ChangeSchoolListTableViewCell = tableView.dequeueReusableCellWithIdentifier("Cell") as! ChangeSchoolListTableViewCell

    if namesArray.count > 0 {
        cell.nameLabel.text = namesArray[indexPath.row]
        cell.locationLabel.text = locationsArray[indexPath.row]
    }
    return cell
}

class ChangeSchoolListTableViewCell: UITableViewCell {

@IBOutlet var nameLabel: UILabel!
@IBOutlet var locationLabel: 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
}
}

Thank you very much.

enter image description here

enter image description here

Anthony Saltarelli
  • 345
  • 1
  • 4
  • 14

3 Answers3

0

I think you are adding one label over another in the table. You need to add one label and reuse it instead of adding another label. The solution is to use a custom UITableViewCell subclass. Also it will be helpful if you show the code of UITableView methods.

Fawad Masud
  • 11,427
  • 3
  • 23
  • 32
  • I added the relevant code. They are two separate labels, and it is using a custom class. How would I use one label and reuse it? Can you take me through that, i've never done that before / had the need to. – Anthony Saltarelli Sep 06 '15 at 23:09
  • Please show your ChangeSchoolListTableViewCell class. – Fawad Masud Sep 07 '15 at 05:21
0

I think, you call reloadData from a background thread. If it is so, then it is a main problem, to solve it, you should call reloadData from the main thread. You can see here how to call a block of code on the main thread from the background thread.

Community
  • 1
  • 1
Andrew Romanov
  • 4,058
  • 2
  • 21
  • 36
  • I am not sure what this means. Also, I have basically an identical view controller and class that presents the same data, and it is not presenting a problem. – Anthony Saltarelli Sep 08 '15 at 14:42
0

You need to remove subviews of uitableviewcell's contentview before adding subviews.

Nazik
  • 8,393
  • 26
  • 72
  • 115
Iya
  • 1,798
  • 17
  • 12