显示tableview的所有单元格的详细信息

时间:2017-11-09 15:53:32

标签: ios swift uitableview cells

我正在尝试实现一种效果,即按下详细信息按钮时,详细信息将滑入tableView的所有单元格。到目前为止,我已经通过表视图的可见单元格迭代实现了这个槽。每个单元格都有一个屏幕外的细节标签,可以通过动画滑动。这是它的外观:

  • 按下详细信息按钮

Before Detail Button Pressed

  • 按下详细信息按钮后 After detail button pressed

因为我正在迭代可见细胞,所以在按下细节按钮后向下滚动时,底部的细胞不具有细节。我知道屏幕上没有显示的单元格不在内存中,但是可能还有另一种方法吗?

修改 以下是用户按“详细信息”按钮

时执行的代码
let animationDuration = 0.4
    for cell in myTableView.visibleCells {
        let cell = cell as! MainCell

        if !detailView {
            sender.tintColor = UIColor.gray
            detailView = true

            UIView.animate(withDuration: animationDuration, animations: ({
                cell.noteLbl.center.x = cell.noteLbl.center.x + 100
            }))
            UIView.animate(withDuration: animationDuration, animations: ({
                cell.accessoryLbl.center.x = cell.accessoryLbl.center.x + 100
            }))
            UIView.animate(withDuration: animationDuration, animations: ({
                cell.timeLbl.center.x = cell.timeLbl.center.x + 100
            }))
        } else {
            detailView = false
            sender.tintColor = UIColor.white

            UIView.animate(withDuration: animationDuration, animations: ({
                cell.noteLbl.center.x = cell.noteLbl.center.x - 100
            }))
            UIView.animate(withDuration: animationDuration, animations: ({
                cell.accessoryLbl.center.x = cell.accessoryLbl.center.x - 100
            }))
            UIView.animate(withDuration: animationDuration, animations: ({
                cell.timeLbl.center.x = cell.timeLbl.center.x - 100
            }))
        }

    }

cellForRowAt的代码

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCell(withIdentifier: "MainCell") as! MainCell
    let shiftForRow = shifts[indexPath.section][indexPath.row]

    cell.noteLbl.text = shiftForRow.note
    cell.dateLbl.text = shiftForRow.date
    cell.accessoryLbl.text = calcTotalHours(STField: shiftForRow.startingTime!, ETField: shiftForRow.endingTime!, lunchTime: shiftForRow.lunchTime!)
    cell.timeLbl.text = shiftForRow.startingTime! + " - " + shiftForRow.endingTime!

    return cell
}

1 个答案:

答案 0 :(得分:0)

滚动表格视图(以及最初显示时),为每个需要显示的单元格调用cellForRowAt方法。

目前,您的cellForRowAt仅在“正常”布局中显示您的单元格,就像从未按过详细信息按钮一样。您需要更新cellForRowAt方法,以便根据是否显示详细信息来配置单元格。

您可能需要一个标记来跟踪当前是否显示详细信息。根据正在按下的详细信息按钮切换该标志。

然后,根据该标志的状态,您的cellForRowAt方法需要适当地设置单元格。