简体   繁体   English

UITableView无法滚动

[英]UITableView can't scroll

My UITableView can't scroll, I tried everything I could have possibly done. 我的UITableView无法滚动,我尝试了可能做的所有事情。 Getting a little desperate here. 在这里有点绝望。

the tableview implementation here is actually pretty simple, but can't make it work... 这里的tableview实现实际上非常简单,但是不能使其工作...

Will post the code below: 将发布以下代码:

class RepresentableViewController: DynamicBackgroundViewController, UITableViewDataSource, UITableViewDelegate, UISearchBarDelegate {

// MARK: - Properties
var representables: Array<TableCellRepresentable>? {
    didSet{
        if !self.isAnimatingTable {
            self.tableView.reloadData()
        }
        if self.representables != nil {
            self.stopAnimatingIndicator()
        }
        else {
            self.startAnimatingIndicator()
        }
    }
}
var cellReuseId: String {
    return RepresentableTableViewCell.reuseId
}
var isAnimatingTable: Bool = false
private(set) var isSearching: Bool = false
private(set) var searchResults: Array<TableCellRepresentable>? {
    didSet {
        self.tableView.reloadData()
    }
}
var searchResultsCount: Int {
    return self.searchResults != nil ? self.searchResults!.count : 0
}
var activeRepresentables: Array<TableCellRepresentable>? {
    return self.isSearching ? self.searchResults : self.representables
}
var activeRepresentablesCount: Int {
    return self.activeRepresentables != nil ? self.activeRepresentables!.count : 0
}
private(set) var lastSelectedIndex: NSIndexPath?

// MARK: Outlets
@IBOutlet weak var tableView: UITableView!
weak var activityIndicator: UIActivityIndicatorView?
@IBOutlet weak var searchBar: UISearchBar!

// MARK: - Methods
override func viewDidLoad() {
    super.viewDidLoad()

    // Do any additional setup after loading the view.
    self.setupCell()
    self.startAnimatingIndicator()
}

private func setupCell() {
    let nib = UINib(nibName: "RepresentableTableViewCell", bundle: nil)
    self.tableView.registerNib(nib, forCellReuseIdentifier: RepresentableTableViewCell.reuseId)
}

override func viewDidAppear(animated: Bool) {
    super.viewDidAppear(animated)
    if self.representables == nil {
        self.postRequestRepresentablesNotification()
    }
}

private func postRequestRepresentablesNotification() {
    NSNotificationCenter.defaultCenter().postNotificationName(RepresentableViewController.requestRepresentablesNotificationName, object: self, userInfo: nil)
}

override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated.
    self.clearRepresentablesIfNotVisible()
}

func insertToTable(item: TableCellRepresentable) {
    self.isAnimatingTable = true
    if self.representables == nil {
        self.self.representables = []
    }
    self.representables!.append(item)
    self.tableView.reloadData()
    self.isAnimatingTable = false
}

func removeFromTable(atIndexPath index: NSIndexPath) {
    self.isAnimatingTable = true
    self.representables!.removeAtIndex(index.row)
    self.tableView.deleteRowsAtIndexPaths([index], withRowAnimation: .Automatic)
    self.isAnimatingTable = false
}

private func clearRepresentablesIfNotVisible() {
    if !self.isViewLoaded() && !(self.view.window != nil) {
        self.representables = nil
    }
}

private func startAnimatingIndicator() {
    if self.activityIndicator == nil {
        let indicator = UIActivityIndicatorView(activityIndicatorStyle: UIActivityIndicatorViewStyle.WhiteLarge)
        indicator.hidesWhenStopped = true
        indicator.color = RGBColor(r: 35, g: 131, b: 250, alpha: 1.0)
        indicator.center = self.view.center
        self.activityIndicator = indicator
        self.view.addSubview(indicator)
        self.shouldBringToFront?.append(indicator)
        indicator.startAnimating()
    }
}

private func stopAnimatingIndicator() {
    if let indicator = self.activityIndicator {
        if let shouldBring = self.shouldBringToFront, index = find(shouldBring, indicator) {
            self.shouldBringToFront!.removeAtIndex(index)
        }
        indicator.stopAnimating()
        indicator.removeFromSuperview()
        self.activityIndicator = nil
    }
}

/*
// MARK: - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
    // Get the new view controller using segue.destinationViewController.
    // Pass the selected object to the new view controller.
}*/

// MARK: - Protocols
// MARK: UITableViewDataSource
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    return self.activeRepresentables != nil ? self.activeRepresentables!.count : 0
}
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCellWithIdentifier(self.cellReuseId, forIndexPath: indexPath) as! RepresentableTableViewCell

    cell.object = self.activeRepresentables![indexPath.row]

    return cell
}
// MARK: UITableViewDelegate
func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
    self.lastSelectedIndex = indexPath
    NSNotificationCenter.defaultCenter().postNotificationName(RepresentableViewController.didSelectARepresentableNotificationName, object: self, userInfo: [RepresentableViewController.representableKey: self.activeRepresentables![indexPath.row]])
}
func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
    return 60.0
}
// MARK: SearchBarDelegate
private func filterProducts(searchText: String) {
    if searchText != "" {
        self.searchResults = self.representables?.filter({ (cur: TableCellRepresentable) -> Bool in
            let lowerCase: String = searchText.lowercaseString
            let titleMatch: Bool = cur.title.lowercaseString.rangeOfString(lowerCase) != nil
            let subtitleMatch: Bool = cur.subtitle.lowercaseString.rangeOfString(lowerCase) != nil
            let detailMatch: Bool = cur.detail?.lowercaseString.rangeOfString(lowerCase) != nil
            return titleMatch || subtitleMatch || detailMatch
        })
    }
    else {
        self.searchResults = self.representables
    }
}
func searchBar(searchBar: UISearchBar, textDidChange searchText: String) {
    self.filterProducts(searchText)
}
func searchBarTextDidEndEditing(searchBar: UISearchBar) {
    self.isSearching = false
    self.searchBar.text = ""
    self.searchResults = nil
}
func searchBarTextDidBeginEditing(searchBar: UISearchBar) {
    self.isSearching = true
}
func searchBarCancelButtonClicked(searchBar: UISearchBar) {
    searchBar.endEditing(true)
}

// MARK: - Static Properties and Methods
class var didSelectARepresentableNotificationName: String {
    return "RepresentableViewController-didSelectARepresentableNotification"
}
class var requestRepresentablesNotificationName: String {
    return "RepresentableViewController-RequestRepresentablesNotification"
}
class var representableKey: String {
    return "Representable"
}

} }

can't figure it out, can someone help me? 无法弄清楚,有人可以帮助我吗?

thanks. 谢谢。


EDITED 已编辑

This issue is being caused by a PanGesture at the UITableViewCells, how can I solve this? 此问题是由UITableViewCells上的PanGesture引起的,我该如何解决呢? I still need this pan gesture to move content at the cell. 我仍然需要此平移手势才能在单元格中移动内容。

Fixed with this piece of code at the UITableViewCell 这段代码已在UITableViewCell上修复

override func gestureRecognizerShouldBegin(gestureRecognizer: UIGestureRecognizer) -> Bool {
    if let panGestureRecognizer = gestureRecognizer as? UIPanGestureRecognizer
    {
        let translation = panGestureRecognizer.translationInView(superview!)
        if fabs(translation.x) > fabs(translation.y) 
        {
             return true
        }
        return false
    }
    return false
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM