Jason247
Jason247

Reputation: 1024

Swift ScrollView In UITableViewCell not scrolling

I am new to swift and am trying to add a UIScrollview to a custom UITableViewCell. I can't seem to get the scrollview to scroll in the simulator though. I have tested both vertical and horizontal scrolling.

Here is the code for my custom UITableViewCell

import UIKit

class CustomTableViewCell: UITableViewCell, UIScrollViewDelegate {


@IBOutlet weak var winLoseValueLabel: UILabel!
@IBOutlet weak var dateLabel: UILabel!
@IBOutlet weak var newTotalLabel: UILabel!
@IBOutlet weak var locationLabel: UILabel!
@IBOutlet weak var playersLabel: UILabel!
@IBOutlet weak var playersScrollView: UIScrollView!

    override func awakeFromNib() {
        super.awakeFromNib()
        // Initialization code
        self.playersLabel.text = nil
        //self.playersScrollView.contentSize.height = 1000
        self.playersScrollView.contentSize.width = 1000

    }

    override func setSelected(selected: Bool, animated: Bool) {
        super.setSelected(selected, animated: animated)

        // Configure the view for the selected state
    }

    func addLabelToScrollView(str : String) {
        if self.playersLabel.text == nil{
            self.playersLabel.text = str
        }
        else{
            self.playersLabel?.text = self.playersLabel.text! + "\n\(str)"
        }
    }
}

My playersScrollView outlet has been properly connected to the scrollview in my storyboard. As far as I can see from all of the tutorials and sources online, I should just have to set the contentSize width or height (larger than the view area) to get it to scroll, which I have done in the awakeFromNib function.

Any ideas why this is not working in the simulator? Secondly, ideally I would like to have a vertical scrolling scrollview in the table cell, but I thought that the table view's vertical scrolling might have been interfering with the scrollview, so I changed it for horizontal scrolling for testing, which also didn't work.

Can you have a vertical scrollview in a vertically scrolling UITableView, or will the table view scrolling interfere too much with the scrollview?

Upvotes: 0

Views: 1736

Answers (1)

Jason247
Jason247

Reputation: 1024

Ok, it turns out that the constraints on my playersLabel, which is a subview of playersScrollView, was the culprit, although I am not sure why. I adjusted the constraints on the label and that allowed the horizontal scrolling to work.

The vertical scrolling still did not work, so I had to override the touchesBegan and touchesEnded methods in the scroll view so I could disable/re-enable the scrolling of the table view.

Upvotes: 1

Related Questions