Swift:使用UIView.animateWithDuration

时间:2015-10-08 00:04:15

标签: ios swift uitableview animatewithduration

我正在以编程方式添加UITableView作为视图的子视图,当从单个点单击按钮到完整窗口时,该视图使用UIView.animateWithDuration展开视图。基本上,一个以点开始并通过动画扩展到完整大小的框。我很难让桌子填充细胞。首先,正在创建一个单元格,但在动画完成后很快就会消失,在玩完它后,我已经让动画完成后单元格保留,但现在当我点击它时单元格消失了。我不明白这里发生了什么。有人可以帮忙吗?

这是我的代码。请注意,我删除了我认为与此问题无关的内容,以使代码更易于阅读。

class PokerLogSelectionView: UIViewController {

    let logSelectionTableViewController = LogSelectionTableViewController()
    let logSelectionTableView = UITableView()

    // Irrelevant class variables removed

    init(btn : PokerLogSelectionButton){
        // Irrelevant view initialization code removed

        // Display the subviews
        self.displayLogListScrollView()
    }

    func displayLogListScrollView() {

        // Frame is set to (0,0,0,0)
        let frame = CGRect(x: self.subviewClosed, y: self.subviewClosed, width: self.subviewClosed, height: self.subviewClosed)

        logSelectionTableView.delegate = self.logSelectionTableViewController
        logSelectionTableView.dataSource = self.logSelectionTableViewController

        // Set the frame of the table view
        logSelectionTableView.frame = frame

        // Give it rounded edges
        logSelectionTableView.layer.cornerRadius = 10

        // Remove the cell divider lines
        logSelectionTableView.separatorStyle = UITableViewCellSeparatorStyle.None

        logSelectionTableView.backgroundColor = logSelectionViewContentScrollViewColor

        self.view.addSubview(logSelectionTableView)

        //self.logSelectionTableView.reloadData()

        //self.addChildViewController(logSelectionTableViewController)

    }

    override func viewDidAppear(animated: Bool) {
        // Create animation
        let timeInterval : NSTimeInterval = 0.5
        let delay : NSTimeInterval = 0
        UIView.animateWithDuration(timeInterval, delay: delay, options: UIViewAnimationOptions.CurveEaseOut, animations: {

            // Irrelevant code removed

            // Set the size and position of the view and subviews after the animation is complete
            self.view.frame = CGRect(x: self.frameXopen, y: self.frameYopen, width: self.frameWopen, height: self.frameHopen)
            self.logSelectionTableView.frame = CGRect(x: self.subviewXopen, y: self.svYopen, width: self.subviewWopen, height: self.svHopen)


            }, completion: { finished in
                self.addChildViewController(self.logSelectionTableViewController)
        })
    }
}

class LogSelectionTableViewController : UITableViewController {

    override func viewDidLoad() {
        super.viewDidLoad()
        self.tableView.registerClass(LogSelectionCell.self, forCellReuseIdentifier: "logCell")
    }

    override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return pokerLibrary.logNames.count
    }

    override func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
        return 20
    }

    override func tableView(tableView: UITableView, canEditRowAtIndexPath indexPath: NSIndexPath) -> Bool {
        return true
    }

    override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
        print("Selected row: \(indexPath.row)")
    }

    override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
        if let cell : LogSelectionCell = self.tableView.dequeueReusableCellWithIdentifier("logCell") as? LogSelectionCell {

            cell.selectionStyle = UITableViewCellSelectionStyle.None


            cell.textLabel!.text = pokerLibrary.logNames[indexPath.row].name

            return cell
        }
        fatalError("Could not dequeue cell of type 'LogSelectionCell'")
    }

}

注意:动画完成后我可以看到tableview。颜色与背景视图中的视图不同,并且tableview不会消失,只是单元格。我希望有一个单元格,我已经打印出0节中的行数,它总是返回1.

感谢您的帮助!

编辑:

以下是单元格消失前视图层次结构的屏幕截图。 enter image description here

这是我点击单元格后视图层次结构的屏幕截图,它会消失。 enter image description here

我在我的自定义单元格中覆盖了touchesBegan方法,并没有调用它的超类方法。当我点击它时,这会阻止单元格消失,但是当我滚动tableView时它仍然会消失。

0 个答案:

没有答案