找到你要的答案

Q:Swift: UITableView Cell Disappears When Using UIView.animateWithDuration

Q:斯威夫特:UITableView细胞消失时使用uiview.animatewithduration

I am programmatically adding a UITableView as a subview of a view that uses UIView.animateWithDuration to expand the view when a button is clicked from a single point to a full window. Basically, a box that starts as a point and expands to full size with an animation. I am having difficulties getting the table to populate with cells. At first, a cell was being created, but would disappear after quickly after the animation completed, after playing around with it, I have gotten the cell to remain after the animation is complete, but now the cell disappears when I tap on it. I don't understand what is going on here. Can someone please help?

Here is my code. Note, I have removed what I believe to be irrelevant to this problem to make the code easier to read.

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'")
    }

}

Note: I can see the tableview after the animation is complete. The color is different than the view in the background view and the tableview does not disappear, just the cell. I expect there to be 1 cell, and I have printed out the number of rows in section 0 and it always returns 1.

Thanks for the help!

Edit:

Here is a screenshot of the view hierarchy before the cell disappears. enter image description here

Here is a screenshot of the view hierarchy after I tap the cell and it disappears. enter image description here

I overrode the touchesBegan method in my custom cell and did not call its superclass method. This stopped the cell from disappearing when I tap it, but it still disappears when I scroll the tableView.

我以编程方式添加一个表格作为子视图的视图,使用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没有消失,只是细胞。我希望有1个单元格,我已经打印出了第0节的行数,它总是返回1。

谢谢你的帮助!

编辑:

Here is a screenshot of the view hierarchy before the cell disappears. enter image description here

Here is a screenshot of the view hierarchy after I tap the cell and it disappears. enter image description here

我不顾touchesBegan方法在我的自定义单元格并没有调用其基类的方法。这阻止了细胞的消失,当我点击它,但它还是消失当我滚动tableView。

ios  swift  uitableview  animatewithduration