【问题标题】:Stop Dynamic Table Reloading IndexPath - Swift停止动态表重新加载 IndexPath - Swift
【发布时间】:2015-10-06 23:37:46
【问题描述】:

我有一个如下的动态 tableView,它根据indexpath.row 显示数组的名称。在每个单元格中,我都有一个按钮,可以更改名称作为单元格的删除,如下面的代码所示。当我加载表格时,假设行加载如下:

姓名1

名字2

名字3

名字4

姓名5

名字6

名字7

名字8

然后我单击按钮并将 Name4 更改为 NewName。单击按钮时它会更改,但是当您在表格中滚动时,当再次遇到 Name4 的 indexpath.row(在本例中为 indexpath.row==3)时,NewName 会变回 Name4。当indexpath.row 发生变化时,如何停止加载表?或者我怎样才能找到解决这个问题的其他方法?

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath:   NSIndexPath) -> UITableViewCell {
    let cell:NamesCell = tableView.dequeueReusableCellWithIdentifier("Cell") as! NamesCell

    cell.NameCell1003 = self
    cell.nameLbl.text = self.resultsNameArray[indexPath.row]

    return cell
}

func NameCell1003(cell: NamesCell)
{
    cell.nameLbl.text= "NewName"
}

【问题讨论】:

  • 单元格的文本基于您的resultsNameArray 中的数据。按下按钮时,您需要更新该数组。

标签: ios swift uitableview dynamic-tables


【解决方案1】:

rmaddy 是正确的,您想更改数组中的底层数据并重新加载 TableView 以实现您想要的行为。

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath:   NSIndexPath) -> UITableViewCell {
    let cell:NamesCell = tableView.dequeueReusableCellWithIdentifier("Cell") as! NamesCell

    cell.nameLbl.text = self.resultsNameArray[indexPath.row]

    return cell
}

func NameCell1003(cell: NamesCell)
{
    self.resultsNameArray[indexYouWantToChange] = "NewName"
    self.tableView.reloadData()
}

您将需要对 UITableView 的引用,通常这是一个 IBOutlet,以便在其上调用 reloadData。在代码中,我只是将其称为“tableView”。如果您的 resultsNameArray 非常大,考虑超过数百个项目,您可以使用以下方法进行调查:

func reloadRowsAtIndexPaths(_ indexPaths: [NSIndexPath],
           withRowAnimation animation: UITableViewRowAnimation)

这将使您只更新所需的行。对于您在问题中陈述的少量行,reloadData 很好并且更易于实现。

【讨论】:

  • 很高兴我能帮上忙。祝你的项目好运!
猜你喜欢
  • 2021-10-11
  • 2021-05-28
  • 1970-01-01
  • 2020-05-25
  • 1970-01-01
  • 2013-07-30
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多