停止动态表重新加载IndexPath - Swift

时间:2015-10-06 15:15:32

标签: ios swift uitableview dynamic-tables

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

名1

名称2

NAME3

NAME4

NAME5

Name6

Name7

Name8

然后我单击按钮并将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"
}

1 个答案:

答案 0 :(得分:2)

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很好,也很容易实现。