簡體   English   中英

單元格上的自定義tableview滾動問題

[英]Custom tableview scrolling issue on cells

我有一個以編程方式創建的tableview

var tableView: UITableView  =   UITableView()
var items = ["1","2","3","4","5","6"," 1","L 2","La 3","La 4","La 5","La 6","L 7","La 8","La 9","La 10","La 11","Lab 12","Lab 13","Lab 14","Lab 15","Lab 16","Lab 17","Lab 18","Lab 19","Lab 20","La 1","L 2","La 3"]

   override func viewDidLoad() {
    super.viewDidLoad()
    tableView.frame         = CGRectMake(0, 50, self.view.frame.width,100);  //If we give self.view.frame.height It worked because no need to scroll
    tableView.delegate      = self
    tableView.dataSource  =  self
    tableView.estimatedRowHeight = 30
   tableView.registerClass(UITableViewCell.self, forCellReuseIdentifier: "Cell")
    self.view.addSubview(tableView)
    }

這是有問題的代碼

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
    var cell:UITableViewCell = tableView.dequeueReusableCellWithIdentifier("Cell") as! UITableViewCell
        if indexPath.row == 2 || indexPath.row == 3 {
            var redBtn = UIButton()
            redBtn = UIButton(frame: CGRectMake(0, 0, 40, 40))
            redBtn.backgroundColor = UIColor.redColor()
            redBtn.setTitle("das", forState: UIControlState.Normal)         
            cell.contentView.addSubview(redBtn)
        }else {
        cell.textLabel?.textAlignment = NSTextAlignment.Center
        cell.textLabel?.text = items[indexPath.row]
       }
       return cell
   }
}

在第2行和第3行中,我只需要顯示按鈕。但是當滾動tableview時它會顯示在所有單元格中。如果將tableview高度增加到查看高度,那么沒有問題,因為沒有滾動發生。

我從這里讀到這個問題UITableView滾動和重繪問題我發現這是因為滾動時重用單元格問題。但是這個解決方案對我不起作用。

歡迎使用合適的解決方案。我提供了完整的代碼,因此您可以將其復制並粘貼到項目中,並解決問題。

提前致謝

當您重復使用單元格時,舊值仍然存在,因此每次使用dequeueReusableCellWithIdentifier時,都需要重置為默認值或仍在其中緩存的最后值。 在您的特定情況下,您需要刪除從單元格創建的按鈕或將其設置為隱藏在else語句中。

從蘋果文檔:

tableView的表視圖的數據源實現:cellForRowAtIndexPath:應該在重用單元時重置所有內容。

實現此目的的最佳方法是在您根據需要創建一個包含所有組件的自定義單元格(按鈕,標簽等),然后根據需要將隱藏屬性隱藏的新自定義單元格設置為true或false。

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
    var cell:UITableViewCell = tableView.dequeueReusableCellWithIdentifier("Cell") as! MyCustomCell
        if indexPath.row == 2 || indexPath.row == 3 {
            redBtn.hidden = false
            cell.contentView.addSubview(redBtn)
        }else {
            redBtn.hidden = true
            cell.textLabel?.textAlignment = NSTextAlignment.Center
            cell.textLabel?.text = items[indexPath.row]
      }
      return cell

在這種情況下,以編程方式動態創建按鈕不是一個好主意,因為您必須循環每個可重用單元格的單元格中的所有子視圖,以確定它是否已存在以決定是否需要創建或刪除,如下所示:

for button:AnyObject in subviews{
    if(button .isKindOfClass(UIButton)){
       if(button.accessibilityIdentifier == "MyButton"){
            println("Button Already Exists")
        }else{
            println("Create new button")
        }
    }
}

我希望能幫助你

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM