簡體   English   中英

動態單元格創建tableView的方法及程序設計

[英]Method to create tableView with dynamic cell and programmatically design

我已全方位解決了該問題,但找不到正確的方法來做自己想做的事情。

問題是我需要管理具有自己設計的6種類型的單元。 例如,我有一個這樣的枚舉:

enum state {
    case buy
    case select
    case go
    case waiting
    case work
    case ended
}

我有一個數據源,它是構建單元所需的數據數組。 該數組始終在我的代碼中更新,我希望該數組定義順序以及tableView中的單元格顯示。

在使用一個單元格並顯示設計之前,需要在cellForRowAt函數中switch state 但是問題是重用系統會將我替換的舊單元格保留在緩存中。

在我的情況下,我有時需要顯示狀態為[選擇,購買]的2個單元格,並在第1行-> [選擇,進行,購買]插入單元格[go]之后,然后像這樣添加2行[選擇,繼續,結束,購買]。

但是當我這樣做時,舊單元的設計就保留在這里。 最好的方法是什么?

編輯

這是我嘗試過的:

func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    return user.lobbySurvey.count
}

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCell(withIdentifier:"Celll", for: indexPath) as! CardCell
    for survey in user.lobbySurvey{
        let index = user.lobbySurvey.index(where: {
            //get the current index is nedeed else the cells reuse lazy
            $0 === survey
        })
        if indexPath.row == index{
            var surveyState : UserSurvey.state
            surveyState = survey.stateSurvey
            switch surveyState{
            case .selectPicture:
                cell.drawCard(statutOfCard: .selectPicture)
            case .goSurvey:
                cell.drawCard(statutOfCard: .goSurvey(picture: survey.picture))
            case .surveyEnded:
                print("survey Ended")
            case .surveyWork:
                print("survey in progress to vote")
            case .surveyWaiting:
                cell.drawCard(statutOfCard: .surveyWaiting(selfSurveyId: survey.id, timeLeft: survey.timeLeft, picture: survey.picture))
            case .buyStack:
                cell.drawCard(statutOfCard: .buyStack(bigView : self.view))
            }

        }
    }
    return cell
}

我的功能吸引人是一個開關,它像這種類型的函數一樣進行重定向:

func drawGoSurvey(image: UIImage){
    if(cardIsDraw == false){
        widthOfCard = UIScreen.main.bounds.size.width - (widthMarginConstraint*2)
        cardViewHeightCon.constant = widthOfCard!*ratioOfImage
        goSurvey.draw(countUserSurvey: user.surveyCount, cardView: self.cardView, widthOfCard : widthOfCard!, userPicture: image)
        goSurvey.delegate = self
        self.addCardShadow()
        cardIsDraw = true
    }
}

您似乎應該嘗試設置每個單元格,而應該只為該特定indexPath設置單元格。 表格視圖的工作方式是確定需要顯示多少個單元格,然后為需要渲染的多個單元tableView(_:, cellForRowAt:)

為了解決您的問題,您將需要做一些更接近此的事情:

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
   let cell = tableView.dequeueReusableCell(withIdentifier:"Cell", for: indexPath) as! CardCell
   let state = user.lobbySurvey[indexPath.row]
   switch state {
   case .selectPicture:
      // Customize for select picture

   case .goSurvey:
      // Customize for goSurvey

   case .surveyEnded:
      // Customize for surveyEnded

   case .surveyWork:
      // Customize

   case .surveyWaiting:
      // Customize

   case .buyStack:
      // Customize
   }
   return cell
}

暫無
暫無

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

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