简体   繁体   English

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

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

Alright, so I'm creating this view where I bring all the contacts on the phone, and I'm simply adding them in an array and looping trough that array in order to create cells in my table view. 好吧,所以我创建了一个视图,将所有联系人都放在手机上,我只是将它们添加到一个数组中,然后循环通过该数组,以便在表格视图中创建单元格。 The problem is, when I scroll, the cell content changes on index or simply disappears from the table. 问题是,当我滚动时,单元格内容在索引上发生更改或仅从表中消失。 I'm creating my table and populating it like so: 我正在创建表并按如下方式填充它:

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

}

Where the vars results and numberofContacts are being retrieved by the CNContactStore() , that part works fine. 凡瓦尔resultsnumberofContacts正在检索CNContactStore()这部分工作正常。 The table gets populated with all the contacts and you can scroll them down perfectly, but once the cell get's scrolled out of the screen, the value when you scroll back gets reassigned. 该表中填充了所有联系人,您可以完美地向下滚动它们,但是一旦将单元格滚动出屏幕,则向后滚动时的值将被重新分配。

I'm also adding the feature of when you click a contact name you call their first assigned number like so: 我还添加了以下功能:当您单击联系人姓名时,您将呼叫其第一个分配的号码,如下所示:

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!)

    }

}

Please help, I have no idea where I'm messing this. 请帮忙,我不知道我在哪里弄糟。 Thank you. 谢谢。

You should be doing it this way: 您应该这样做:

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
}

Your cells are being reused, so if you rely on your i var you are going to end up increasing it's value beyond numberofContacts and that's what is giving you the empty cells. 你的细胞被再利用,所以如果你依靠你i VAR你会最终增加它的价值超越numberofContacts ,这就是是给你的空单元格。

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

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