Quick context, I have a view controller that has the option to add friends by clicking a UIButton. I want to customize this controller rather than using peoplepicker so I created my own tableview I populate with info from the address book. First, I ask the user if its ok to access their address book. I then put their contacts names into an array to populate the tableview. The weirdest thing though is the names don't show up in the table cells until you first scroll on the tableview. When I stop and relaunch or just unwind and come back, the cells are populated like you'd expect. When I reset the content and restart, the same thing happens where the names don't show up in the cells until you scroll. Any thoughts on what could be happening? 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()
}
}
Run your .reloadData on the main thread:
dispatch_async(dispatch_get_main_queue()) {
self.namesTableView.reloadData()
}
and it should work.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With