gpt4 book ai didi

ios - TableView 行与 Swift 中的数据不一致

转载 作者:行者123 更新时间:2023-11-28 13:14:06 24 4
gpt4 key购买 nike

我正在用我的联系人列表中的数据加载一个 UITableViewController。在模拟器中使用少量数据一切正常,但在我有 82 个联系人的 iPhone 上,我加载了不同数量的行,从 0 到大部分数据,但从来没有所有数据,而且总是不同的数量.这几乎就像在数据数组完成之前加载和显示表格一样。如果我将 numberOfRowsInSection 手动返回设置为 82,它工作正常,但是当设置为 contactdatalist.count 时,它会得到一个较小的数字。是不是我做错了什么,或者我可以减慢 tableview 的加载速度,直到所有数据加载完成?它在操作上似乎是异步的。

import UIKit
import AddressBook

class TableViewControllerContacts: UITableViewController {

var contactdatalist = [ContactData]()

override func viewDidLoad() {
super.viewDidLoad()

// Uncomment the following line to preserve selection between presentations
// self.clearsSelectionOnViewWillAppear = false

// Uncomment the following line to display an Edit button in the navigation bar for this view controller.
// self.navigationItem.rightBarButtonItem = self.editButtonItem()

self.contactdatalist = []

let addressBook : ABAddressBookRef? = ABAddressBookCreateWithOptions(nil, nil).takeRetainedValue()
ABAddressBookRequestAccessWithCompletion(addressBook, { (granted : Bool, error: CFError!) -> Void in
if granted == true {
let allContacts : NSArray = ABAddressBookCopyArrayOfAllPeople(addressBook).takeRetainedValue()
for contactRef:ABRecordRef in allContacts { // first name
let myPBfirstname = ABRecordCopyValue(contactRef, kABPersonFirstNameProperty)?.takeRetainedValue() as! NSString? ?? ""
let myPBlastname = ABRecordCopyValue(contactRef, kABPersonLastNameProperty)?.takeRetainedValue() as! NSString? ?? ""
let phonesRef: ABMultiValueRef = ABRecordCopyValue(contactRef, kABPersonPhoneProperty)?.takeRetainedValue() as ABMultiValueRef? ?? ""
var phonesArray = Array<Dictionary<String,String>>()
for var i:Int = 0; i < ABMultiValueGetCount(phonesRef); i++ {
let myPhLabel = ABMultiValueCopyLabelAtIndex(phonesRef, i)?.takeRetainedValue() as NSString? ?? ""
let myPhValue = ABMultiValueCopyValueAtIndex(phonesRef, i)?.takeRetainedValue() as! NSString? ?? ""
if myPhLabel.containsString("Mobile") {
self.contactdatalist.append(ContactData(firstname:myPBfirstname as String, lastname:myPBlastname as String, phone:myPhValue as String))
}
}
}
}
})
self.tableView.reloadData()
}

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

// MARK: - Table view data source

override func numberOfSectionsInTableView(tableView: UITableView) -> Int {
// #warning Potentially incomplete method implementation.
// Return the number of sections.
return 1
}

override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
// #warning Incomplete method implementation.
// Return the number of rows in the section.
return self.contactdatalist.count
}

override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = self.tableView.dequeueReusableCellWithIdentifier("Cell", forIndexPath: indexPath) as! UITableViewCell
// Configure the cell...
let contactdata = self.contactdatalist[indexPath.row]
cell.textLabel!.text = ("\(contactdata.firstname) \(contactdata.lastname)")
return cell
}

最佳答案

更新您的 viewDidLoad,以便在单独的线程中获取联系人,并在获取联系人时加载 tableview。

override func viewDidLoad() {
super.viewDidLoad()

// Uncomment the following line to preserve selection between presentations
// self.clearsSelectionOnViewWillAppear = false

// Uncomment the following line to display an Edit button in the navigation bar for this view controller.
// self.navigationItem.rightBarButtonItem = self.editButtonItem()

self.contactdatalist = []

dispatch_async(dispatch_get_global_queue(Int(QOS_CLASS_USER_INITIATED.value), 0)) {
let addressBook : ABAddressBookRef? = ABAddressBookCreateWithOptions(nil, nil).takeRetainedValue()
ABAddressBookRequestAccessWithCompletion(addressBook, { (granted : Bool, error: CFError!) -> Void in
if granted == true {
let allContacts : NSArray = ABAddressBookCopyArrayOfAllPeople(addressBook).takeRetainedValue()
for contactRef:ABRecordRef in allContacts { // first name
let myPBfirstname = ABRecordCopyValue(contactRef, kABPersonFirstNameProperty)?.takeRetainedValue() as! NSString? ?? ""
let myPBlastname = ABRecordCopyValue(contactRef, kABPersonLastNameProperty)?.takeRetainedValue() as! NSString? ?? ""
let phonesRef: ABMultiValueRef = ABRecordCopyValue(contactRef, kABPersonPhoneProperty)?.takeRetainedValue() as ABMultiValueRef? ?? ""
var phonesArray = Array<Dictionary<String,String>>()
for var i:Int = 0; i < ABMultiValueGetCount(phonesRef); i++ {
let myPhLabel = ABMultiValueCopyLabelAtIndex(phonesRef, i)?.takeRetainedValue() as NSString? ?? ""
let myPhValue = ABMultiValueCopyValueAtIndex(phonesRef, i)?.takeRetainedValue() as! NSString? ?? ""
if myPhLabel.containsString("Mobile") {
self.contactdatalist.append(ContactData(firstname:myPBfirstname as String, lastname:myPBlastname as String, phone:myPhValue as String))
}
}
}
}
})

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

关于ios - TableView 行与 Swift 中的数据不一致,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29966753/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com