gpt4 book ai didi

ios - 滚动时 TableView 重新排列/创建空白单元格。 swift

转载 作者:行者123 更新时间:2023-11-29 01:16:03 25 4
gpt4 key购买 nike

好的,所以我正在创建这个 View ,我将所有联系人都放在手机上,我只是将它们添加到一个数组中并循环遍历该数组,以便在我的表格 View 中创建单元格。问题是,当我滚动时,单元格内容在索引上发生变化,或者只是从表格中消失。我正在创建我的表并像这样填充它:

public func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int{
return Int(numberofContacts)
}

var i = 0

public func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell
{
print(i)
let cell = UITableViewCell(style: UITableViewCellStyle.Default, reuseIdentifier: "cell")

if(i < numberofContacts){
cell.textLabel?.text = results[i].givenName + " " + results[i].familyName

}
i++

return cell

}

CNContactStore() 正在检索变量 resultsnumberofContacts,那部分工作正常。表格中填充了所有联系人,您可以完美地向下滚动它们,但是一旦单元格滚出屏幕,您向后滚动时的值就会重新分配。

我还添加了这样的功能:当您单击联系人姓名时,您会调用他们分配给他们的第一个号码,如下所示:

func tableView(tableView: UITableView!, didSelectRowAtIndexPath indexPath: NSIndexPath!) {

if results[indexPath.row].isKeyAvailable(CNContactPhoneNumbersKey){
let selected = (results[indexPath.row].phoneNumbers[0].value as! CNPhoneNumber).valueForKey("digits") as! String

let urlPhone = NSURL(string: "tel://\(selected)")

UIApplication.sharedApplication().openURL(urlPhone!)

}

}

请帮忙,我不知道我在哪里搞砸了。谢谢。

最佳答案

你应该这样做:

public func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return Int(numberofContacts)
}

public func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = UITableViewCell(style: UITableViewCellStyle.Default, reuseIdentifier: "cell")

cell.textLabel?.text = results[indexPath.row].givenName + " " + results[indexPath.row].familyName
return cell
}

你的单元格正在被重用,所以如果你依赖你的i变量,你最终会增加它的值超过numberofContacts,这就是给你空的原因细胞。

关于ios - 滚动时 TableView 重新排列/创建空白单元格。 swift ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35162570/

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