![](/img/trans.png)
[英]Swift : Redundant conformance of Viewcontroller to protocol UIGestureRecognizerDelegate
[英]How to solve the error "Redundant conformance of viewController to protocol UIScrollViewDelegate in swift?
我是stackOverflow的新手,學習迅捷。 我在使用Stretch headers.UIScrollViewDelegate時遇到錯誤“viewController與協議的冗余一致性。我在下面指定我的代碼。請更正任何一個。
class ViewController: UITableViewController , UIScrollViewDelegate {
private let kTableHeaderHeight : CGFloat = 300.0
// Using Implicitly Unwrapped Optional, UIView!
var headerView:UIView!
let items = [
NewsItem(category: .World, summary: "Climate Change protests, Need to preserve our Ecosysytem"),
NewsItem(category: .India, summary: "Climate Change protests, Need to preserve our Ecosysytem"),
NewsItem(category: .America, summary: "Climate Change protests,Need to preserve our Ecosysytem"),
NewsItem(category: .Japan, summary: "Climate Change protests, Need to preserve our Ecosysytem"),
NewsItem(category: .China, summary: "Climate Change protests, Need to preserve our Ecosysytem"),
NewsItem(category: .Nepal, summary: "Climate Change protests, Need to preserve our Ecosysytem")]
override func viewDidLoad() {
super.viewDidLoad()
headerView = tableView.tableHeaderView
tableView.tableHeaderView = nil
tableView.addSubview(headerView)
tableView.contentInset = UIEdgeInsetsMake(kTableHeaderHeight, 0, 0, 0)
tableView.contentOffset = CGPointMake(0, -kTableHeaderHeight)
updateHeaderView()
}
func updateHeaderView(){
var HeaderRect = CGRectMake(0,-kTableHeaderHeight, tableView.bounds.width, kTableHeaderHeight)
if tableView.contentOffset.y < -kTableHeaderHeight{
HeaderRect.origin.y = tableView.contentOffset.y
HeaderRect.size.height = -tableView.contentOffset.y
}
headerView.frame = HeaderRect
}
override func didReceiveMemoryWarning() {enter code here
super.didReceiveMemoryWarning()
}
override func scrollViewDidScroll(scrollView: UIScrollView) {
updateHeaderView()
}
override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return items.count
}
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let item = items[indexPath.row]
let cell = tableView.dequeueReusableCellWithIdentifier("Cell" forIndexPath: indexPath) as! NewsItemTableViewCell
cell.newsItem = item
return cell
}
您收到此錯誤是因為您的類ViewController
以兩種方式符合協議UIScrollViewDelegate
。 UITableViewController
已經符合該協議,您不需要再次添加它。 所以從那里刪除UIScrollViewDelegate
,你很好。
這就是UITableViewController
符合UITableViewDelegate
,它符合UIScrollViewDelegate
。 這就足夠了
class ViewController: UITableViewController{
}
最簡單的解釋是您的UITableViewController帶有內置的滾動視圖,因此您不需要 Scrollview的委托。 刪除UIScrollViewDelegate
,你會沒事的。
隨意問任何問題:)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.