繁体   English   中英

滚动时,TableView重新排列/创建空白单元格。 迅速

[英]TableView rearranging / creating blank cells when scrolling. Swift

好吧,所以我创建了一个视图,将所有联系人都放在手机上,我只是将它们添加到一个数组中,然后循环通过该数组,以便在表格视图中创建单元格。 问题是,当我滚动时,单元格内容在索引上发生更改或仅从表中消失。 我正在创建表并按如下方式填充它:

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

}

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

我还添加了以下功能:当您单击联系人姓名时,您将呼叫其第一个分配的号码,如下所示:

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 VAR你会最终增加它的价值超越numberofContacts ,这就是是给你的空单元格。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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