【问题标题】:Prevent reuse of custom cells in table view Swift防止在表格视图Swift中重用自定义单元格
【发布时间】:2015-08-27 20:35:23
【问题描述】:

我有一个不知道如何解决的问题。

我有两个自定义单元笔尖 - 两者的数据都是从单独的数组中获取的。

结构如下

nib1-cell line1
nib1-cell line2
...
nib1-cell line n
nib2-cell line1

在末尾总是有一个 nib2-cell,带有 uibutton
一旦按下uibutton - 就会附加 nib1 数组。
我想出了一种方法如何在tableview 的底部插入值,但是当我向下或向上滚动时,带有 nib2 的单元格被重用并替换为 nib1-cell 数据。

如何防止这些单元格被重复使用或保存它们的状态?

谢谢。

更新:数据源和 cellForRowAtIndexPath 代码

func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {

    return someTagsArray.count

}

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

    if(indexPath.row < someTagsArray.count - 1){
        var cell:TblCell = self.tableView.dequeueReusableCellWithIdentifier("cell") as! TblCell

        cell.lblCarName.text = someTagsArray[indexPath.row]


        return cell

    } else if (indexPath.row == someTagsArray.count - 1){
      var celle:vwAnswers = self.tableView.dequeueReusableCellWithIdentifier("cell2") as! vwAnswers
        celle.Answer1.setTitle(answersdict[answersdict.endIndex - 2], forState:UIControlState.Normal)

        answertitle1 = "\(celle.Answer1.currentTitle!)"

        celle.Answer2.setTitle(answersdict.last, forState:UIControlState.Normal)
        answertitle2 = "\(celle.Answer2.currentTitle!)"

        //println(answertitle2)

        return celle

    } else {
                    var cell2:TblCell = self.tableView.dequeueReusableCellWithIdentifier("cell") as! TblCell
        return cell2
    }

}

【问题讨论】:

    标签: ios swift uitableview uibutton custom-cell


    【解决方案1】:

    您必须确定您想要在cellForRowAtIndexPath 中使用哪种类型的单元格并将正确的可重用单元格出列。也许像if (indexPath.row + 1)%3 == 0 这样的东西然后出列一个答案单元格。

    但是,您可能希望考虑为此使用节标题。很难说没有看到您如何实现数据源。

    【讨论】:

    • if (indexPath.row + 1)%3 == 0 的问题是第二个 nib 不应该总是排在第三个,它可能是第五个,第六个 - 因为数组中第一个 nib 的对象数量会有所不同。我还在我的帖子中为数据源添加了代码。
    • 你应该只考虑你需要什么数据以及你想如何访问它。您要么需要跟踪每次有多少问题单元格并使用它来放置答案单元格,要么将其分成多个数组。我建议可能使用数组数组[Array&lt;String&gt;],这样数据源中的每个数组都代表一个问题列表。
    猜你喜欢
    • 1970-01-01
    • 2014-11-17
    • 1970-01-01
    • 1970-01-01
    • 2014-11-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-11-25
    相关资源
    最近更新 更多