2015-09-02 2 views
0

Мое UITableView не может прокручиваться, я пробовал все, что мог бы сделать. Немного отчаянный.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

Данный вопрос, вызванный PanGesture в то UITableViewCells, как я могу решить эту проблему? Мне все еще нужен этот жест, чтобы переместить содержимое в ячейку.

+0

ли это отображается все, что вы ожидаете его, но просто не прокрутка? – NRitH

+0

Я тоже не вижу реализацию 'numberOfSectionsInTable()'. Значение по умолчанию 0. – NRitH

+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 
} 
Смежные вопросы