简体   繁体   English

动态单元格创建tableView的方法及程序设计

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

I have returned the problem in all directions and I do not find the right way to do what I want. 我已全方位解决了该问题,但找不到正确的方法来做自己想做的事情。

The problem is that I need to manage 6 type of cells who have their own design. 问题是我需要管理具有自己设计的6种类型的单元。 For example I have a enum like that: 例如,我有一个这样的枚举:

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

I have a data source which is an array of the data I need to build the cell. 我有一个数据源,它是构建单元所需的数据数组。 This array is always update in my code and I want the array define the order and the cell display in the tableView. 该数组始终在我的代码中更新,我希望该数组定义顺序以及tableView中的单元格显示。

Before I use one cell and display the design I need with a switch of the state in the cellForRowAt function. 在使用一个单元格并显示设计之前,需要在cellForRowAt函数中switch state But the problem a have is the reuse system who keep in cache my old cell that I had replaced. 但是问题是重用系统会将我替换的旧单元格保留在缓存中。

In my case I need to display sometimes 2 cell with the state [select, buy] and after insert the cell [go] at row 1 -> [select, go, buy] ... and after add 2 row like that [select, go, go, ended, buy]. 在我的情况下,我有时需要显示状态为[选择,购买]的2个单元格,并在第1行-> [选择,进行,购买]插入单元格[go]之后,然后像这样添加2行[选择,继续,结束,购买]。

But when I do that, the design of the old cell keep here. 但是当我这样做时,旧单元的设计就保留在这里。 What is the best way to do that ? 最好的方法是什么?

Edit 编辑

This is what I have tried: 这是我尝试过的:

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
}

My function drawcard is a switch who redirect like this type of function: 我的功能吸引人是一个开关,它像这种类型的函数一样进行重定向:

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

You seem to be trying to set up every single one of your cells when you should instead only be setting up the cell for that particular indexPath. 您似乎应该尝试设置每个单元格,而应该只为该特定indexPath设置单元格。 The way a table view works is it will determine how many cells need to display and then call tableView(_:, cellForRowAt:) for however many cells it needs to render. 表格视图的工作方式是确定需要显示多少个单元格,然后为需要渲染的多个单元tableView(_:, cellForRowAt:)

To fix your issue, you're going to want to do something closer to this: 为了解决您的问题,您将需要做一些更接近此的事情:

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