UITableView无法滚动

时间:2015-09-02 02:06:47

标签: ios swift uitableview swift2

我的UITableView无法滚动,我尝试了我可能做的所有事情。 在这里有点绝望。

这里的tableview实现实际上非常简单,但无法使其正常工作......

将发布以下代码:

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

}

无法弄明白,有人可以帮帮我吗?

感谢。

EDITED

这个问题是由UITableViewCells的PanGesture引起的,我该如何解决这个问题?我仍然需要这个平移手势来移动单元格内容。

1 个答案:

答案 0 :(得分:0)

修复了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
}