简体   繁体   中英

Scrolling UITableView in iOS 9 results in overlapped labels

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.

普通图片

不正常的图片

有些搞砸了

 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.

在此输入图像描述

在此输入图像描述

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.

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.

在添加子视图之前,您需要删除uitableviewcell的contentview的子视图。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM