【问题标题】:Data Errors in UITableViewUITableView 中的数据错误
【发布时间】:2017-09-20 15:51:14
【问题描述】:

我正在开发一个公民参与应用程序,其中将包含一个 UITableView,列出所有 50 个州和华盛顿特区的选民登记信息。我编译了一个如下的 [[String]] 数组作为数据源:

[stateAbbv.,stateName,infoLink,onlineRegistrationLink]

示例:

["MO","Missouri","https://www.sos.mo.gov/elections/govotemissouri/register","https://s1.sos.mo.gov/votemissouri/request"]
["SD","South Dakota","https://sdsos.gov/elections-voting/voting/register-to-vote/default.aspx","nil"]

为了使 UITableView 更易于填充,我在 viewDidLoad 中将数组分解为 4 个单独的数组,如下所示:

override func viewDidLoad() {
    super.viewDidLoad()
    print(voterInformation.count)
    for items in voterInformation
    {
        stateAbbreviations.append(items[0])
        stateNames.append(items[1])
        voteInfoLinks.append(items[2])
        onlineLink.append(items[3])
    }


    resultsTable.delegate=self
    resultsTable.dataSource=self

在我的 tableView 单元格方法中,我对其进行了编码,以便如果在线注册链接不存在,“在线注册”按钮将被隐藏。

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let newCell=resultsTable.dequeueReusableCell(withIdentifier: "stateRegistrationCell", for: indexPath) as! VoterRegistraionCell

    print(indexPath.row)

    newCell.stateName.text=stateNames[indexPath.row]
    //print(voterInformation[indexPath.row][3])
   if(voteInfoLinks[indexPath.row] == "No Voter Registration")
    {
        newCell.infoLink.setTitle("No Voter Registration", for: UIControlState.normal)
        newCell.onlineRegistration.isHidden=true
    }
    else
    {
        newCell.infoLink.tag=indexPath.row
        //print(voterInformation[indexPath.row][3])
        if(onlineLink[indexPath.row] != "nil")
        {newCell.onlineRegistration.tag=indexPath.row}
        else
        {
            newCell.onlineRegistration.isHidden=true
        }
    }

    return newCell
}

我的问题是,当我在模拟器中进行测试时,链接最初显示正常,但当我向下滚动时,一些本应存在的“在线注册”链接不存在。当我向上滚动时,以前存在的在线链接不再存在。 (例如阿拉巴马州)。关于为什么这些数据似乎会丢失有什么想法吗?

【问题讨论】:

    标签: ios iphone swift uitableview


    【解决方案1】:

    这是因为单元格在滚动时被重复使用。

    在您的 Cell 类中实现 prepareForReuse() 函数,并将您的按钮设置为 isHidden=true

    【讨论】:

    • 所以为了澄清一下,onlineLink按钮默认是隐藏的,然后在tableView方法中,我会检查onlineLink是否存在,如果存在,然后将“isHidden”更改为false?跨度>
    • 没错!因此,每个呈现的单元格都会隐藏按钮,除非您明确更改
    • 谢谢,让我试一试,然后回复你。
    【解决方案2】:

    设置在此行下方if(onlineLink[indexPath.row] != "nil")此行newCell.onlineRegistration.isHidden=false

    【讨论】:

      猜你喜欢
      • 2012-02-03
      • 1970-01-01
      • 1970-01-01
      • 2014-03-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-08-14
      • 1970-01-01
      相关资源
      最近更新 更多