Ios UITableView中的数据错误
我正在开发一个公民参与应用程序,它将包括一个UITableView,列出所有50个州和特区的选民登记信息。我编译了一个[[String]]数组作为数据源,如下所示:Ios UITableView中的数据错误,ios,iphone,swift,uitableview,Ios,Iphone,Swift,Uitableview,我正在开发一个公民参与应用程序,它将包括一个UITableView,列出所有50个州和特区的选民登记信息。我编译了一个[[String]]数组作为数据源,如下所示: [stateAbbv.,stateName,infoLink,onlineRegistrationLink] 例如: ["MO","Missouri","https://www.sos.mo.gov/elections/govotemissouri/register","https://s1.sos.mo.gov/votemiss
[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
}
我的问题是,当我在模拟器中测试时,链接最初显示为OK,但当我向下滚动时,一些本应存在的“在线注册”链接不存在。当我向上滚动时,以前存在的在线链接就不再存在了。(以阿拉巴马州为例)。有没有想过为什么这些数据会丢失
设置在此行下方
如果(onlineLink[indexPath.row]!=“nil”)
此行newCell.onlineRegistration.ishiden=false
这是因为单元格在滚动时被重用
在您的单元格类中实现
prepareforeuse()
函数,并将按钮设置为ishiden=true
,以便澄清一下,onlineLink按钮在默认情况下是隐藏的,然后在tableView方法中,我会检查onlineLink是否存在,如果存在,然后将“ishiden”更改为false?完全正确!因此,每个显示的单元格都会隐藏按钮,除非您明确更改按钮。谢谢,让我尝试一下,然后再给您回复。