UITableview cells do not appear until you scroll

Quick context. I have a view controller that has the ability to add friends by clicking the UIButton button. I want to configure this controller and not use peoplepicker, so I created my own tableview, which is populated with information from the address book. First, I ask the user whether it’s normal to access their address book. Then I put the names of my contacts in an array to populate the table. The strangest thing, however, is that the names do not appear in the table cells until you first scroll through the table. When I stop, start again, or just relax and return, the cells fill up as you expected. When I reset the contents and reload, the same thing happens when the names do not appear in the cells until you scroll through the list. Any thoughts onwhat can happen? Thanks in advance!

import UIKit
import AddressBook

class ContactsViewController: UIViewController, UITableViewDataSource, UITableViewDelegate {

    @IBOutlet weak var namesTableView: UITableView!

    var addressBook: ABAddressBook!
    var names:[String]?

    func createAddressBook() {

        var error: Unmanaged<CFErrorRef>?

        addressBook = ABAddressBookCreateWithOptions(nil, &error).takeRetainedValue()

    }

    override func viewDidLoad() {
        super.viewDidLoad()

        self.namesTableView.delegate = self
        self.namesTableView.dataSource = self

        accessAddressBook()

        self.namesTableView.backgroundColor = UIColor(red: 74/255, green: 144/255, blue: 226/255, alpha: 1)
        self.namesTableView.allowsMultipleSelection = true

    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }

    func accessAddressBook () {

        switch ABAddressBookGetAuthorizationStatus(){
        case .Authorized:
            println("Already authorized")
            createAddressBook()
            self.beginContactSearch()

        case .Denied:
            println("You are denied access to the address book")

        case .NotDetermined:
            createAddressBook()
            if let theBook: ABAddressBook = addressBook {

                ABAddressBookRequestAccessWithCompletion(theBook, {(granted: Bool, error: CFError!) in

                    if granted {

                        println("Access is granted")
                        self.beginContactSearch()
                    }
                    else {

                        println("Access is not granted")
                    }
                })
            }
        case .Restricted:
            println("Access is restricted")

        default:
            println("Unhandled")
        }

    }

    func beginContactSearch(){
        let records = ABAddressBookCopyArrayOfAllPeople(self.addressBook).takeRetainedValue() as NSArray as [ABRecord]
        names = Array()
        for record in records {
            let object = ABRecordCopyCompositeName(record)
            if object.toOpaque() == COpaquePointer.null() {

            } else {
                var name = object.takeRetainedValue() as NSString
                self.names!.append(name)
            }
            self.namesTableView.reloadData()
        }
    }

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

    func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
        let cell = namesTableView.dequeueReusableCellWithIdentifier("Tablecell") as UITableViewCell

        cell.textLabel!.text = names![indexPath.row]
        cell.textLabel?.textColor = UIColor.whiteColor()
        cell.backgroundColor = UIColor(red: 74/255, green: 144/255, blue: 226/255, alpha: 1)

        return cell
    }

    func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
        var selectedCell:UITableViewCell = tableView.cellForRowAtIndexPath(indexPath)!
        selectedCell.contentView.backgroundColor = UIColor.whiteColor()
        selectedCell.textLabel?.textColor = UIColor(red: 74/255, green: 144/255, blue: 226/255, alpha: 1)

        println("User selected: \(self.names?[indexPath.row])")

    }

    func tableView(tableView: UITableView, didDeselectRowAtIndexPath indexPath: NSIndexPath) {
        var cellToDeSelect:UITableViewCell = tableView.cellForRowAtIndexPath(indexPath)!

        cellToDeSelect.textLabel?.textColor = UIColor.whiteColor()

    }

}
+4
2

.reloadData :

dispatch_async(dispatch_get_main_queue()) {
     self.namesTableView.reloadData()
}

.

+5

self.namesTableView.reloadData() , tableview

async,

Swift

DispatchQueue.main.async {
    // RELOAD TABLE DATA
}

Objective-C

dispatch_async(dispatch_get_main_queue(), ^{
     // RELOAD TABLE DATA
});

, , , , .

0

All Articles