Swift 2 tableview 滚动更改数据

编程入门 行业动态 更新时间:2024-10-27 10:33:11
本文介绍了Swift 2 tableview 滚动更改数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

限时送ChatGPT账号..

我无法正确加载数据!然后当我向下滚动并备份时,顶部的单元格加载良好,必须点亮电话号码的曾经黑暗的图标会亮起,当它们被触摸时,它们会呼叫一个随机号码.这很奇怪,因为具有电话号码的其他单元格不会只更改剩下等于"的单元格.

I am having trouble getting the data to load correctly! The top cells load well then when I scroll down and back up the once dark icons that had to phone number are lit and when they are touched they call a random number. This is odd, as the other cells with a phone number do not change only the ones that are left equalling "".

这是来自 ViewController.swift

This is from the ViewController.swift

func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    return contactList.count
}

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {

    let cell = self.tableView.dequeueReusableCellWithIdentifier("cell", forIndexPath: indexPath) as! TableViewCell

    let contact = contactList[indexPath.row]

    cell.name.text = "\(contact.name)"

    cell.id.text = "\(phoneFormat(contact.callPhone))"

    if contact.callPhone != "" {

        cell.callButton.setImage(UIImage(named: "phone.png"), forState: UIControlState.Normal)

        cell.callButton.userInteractionEnabled = true

        cell.callButton.tag = indexPath.row

        cell.callButton.addTarget(self, action: "call:", forControlEvents: .TouchUpInside)

    }

   /* if contact.smsPhone != nil {

        cell.smsButton.setImage(UIImage(named: "chat.png"), forState: UIControlState.Normal)

        cell.smsButton.userInteractionEnabled = true

        cell.smsButton.tag = indexPath.row
    }

    if contact.email != nil {

        cell.emailButton.setImage(UIImage(named: "email.png"), forState: UIControlState.Normal)

        cell.emailButton.userInteractionEnabled = true

        cell.emailButton.tag = indexPath.row

    }*/

    return cell
}

这是 tableviewcell.swift 文件

this is the tableviewcell.swift file

class TableViewCell: UITableViewCell {

@IBOutlet var name: UILabel!

@IBOutlet var id: UILabel!

@IBOutlet var callButton: UIButton!

override func awakeFromNib() {
    super.awakeFromNib()
    // Initialization code
}

override func setSelected(selected: Bool, animated: Bool) {
    super.setSelected(selected, animated: animated)

    // Configure the view for the selected state
}}

推荐答案

您看到此行为的原因是您的 if 缺少 else 分支,其中您清除了 if 中设置的视觉内容:

The reason that you see this behavior is that your if is missing an else branch, in which you clear out the content of visuals being set in the if:

if contact.callPhone != "" {
    cell.callButton.setImage(UIImage(named: "phone.png"), forState: UIControlState.Normal)
    cell.callButton.userInteractionEnabled = true
    cell.callButton.tag = indexPath.row
    cell.callButton.addTarget(self, action: "call:", forControlEvents: .TouchUpInside)
} else {
    cell.callButton.setImage(nil, forState: UIControlState.Normal)
    cell.callButton.userInteractionEnabled = false
    cell.callButton.tag = 0
    cell.callButton.hidden = true
}

这将防止从屏幕滚动的重复使用的单元格中的按钮显示在缺少电话号码的单元格中.

This would prevent buttons from reused cells scrolled off the screen from showing up in cells for which the phone number is missing.

这篇关于Swift 2 tableview 滚动更改数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

更多推荐

[db:关键词]

本文发布于:2023-05-01 11:16:24,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1408782.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:数据   Swift   tableview

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!