【问题标题】:UITableView can't scrollUITableView 无法滚动
【发布时间】:2015-09-02 02:06:46
【问题描述】:

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

}

想不通,谁能帮帮我?

谢谢。


已编辑

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

【问题讨论】:

  • 它是否显示了您期望的所有内容,但只是不滚动?
  • 我也没有看到numberOfSectionsInTable() 的实现。默认值为 0。
  • 是的,它显示所有内容,但不滚动。
  • 默认是1,先生
  • 如果您确定平移手势的问题,然后尝试使用 longpressgesture,您还可以通过平移手势执行您正在查看的内容。

标签: ios swift uitableview swift2


【解决方案1】:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-05-11
    • 2014-10-20
    • 2013-05-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多