滚动单元格时在每个单元格中显示相同的内容

时间:2018-10-15 04:43:47

标签: ios

我有5种类型的tableview单元。我有一个basecell。

BaseCell:-

导入UIKit

class BaseCell: UITableViewCell {
    // MARK: Internal Properties
    var type: CellType!
    var textChangedBlock: ((String) -> Void)?

    override func awakeFromNib() {
        super.awakeFromNib()
        // Initialization code
    }
    func setOptions(Options1:NH_OptionsModel){}
    func setOptions(OptionsSelected:NH_OptionsModel){}
    func setOptions1(OptionsSelected:NH_OptionsModel){}
    func setOptions(OptionsisSelected:NH_OptionsModel){}
    func setOptions1(OptionsisSelected:NH_OptionsModel){}

}

一个Tableviewcell:-

class RadioTypeCell:  BaseCell {
    var actionBlock: (() -> Void)?
    @IBOutlet weak var optionLabel: UILabel?
    @IBOutlet weak var buttonType: UIButton?

    override func setOptions(Options1:NH_OptionsModel) {
      self.optionLabel?.text = Options1.values
    }

    override func setOptions1(OptionsSelected:NH_OptionsModel) {
        self.buttonType?.isHidden = false
        buttonType?.setBackgroundImage(UIImage(named: "radio_check.png"), for: .normal)

        print(OptionsSelected.isSelected)
        OptionsSelected.isSelected = true
        print(OptionsSelected.isSelected) 
    }

     override func setOptions(OptionsisSelected:NH_OptionsModel) {   
        self.buttonType?.isHidden = false
        buttonType?.setBackgroundImage(UIImage(named: "radio_uncheck.png"), for: .normal)

        print(OptionsisSelected.isSelected)

        OptionsisSelected.isSelected = false
        print(OptionsisSelected.isSelected) 
    }

    @IBAction func onActionButton(btn: UIButton) {
        print("click the cell")
        actionBlock?()
    }

     override func awakeFromNib() {
        super.awakeFromNib()
    }

    override func prepareForReuse() {
        super.prepareForReuse()
    }   

}

在viewcontroller中:-

 func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {

        let model = questionViewModel.titleForHeaderInSection(atsection: indexPath.section)
        print(model.answerType)

        print(model.answerType?.rawValue)

        let c = model.answerType
        let cellClass = c?.cellType().getClass()
        print(cellClass)
        let cell = tableView.dequeueReusableCell(withIdentifier: (cellClass?.cellReuseIdentifier())!, for: indexPath) as! BaseCell
        print(cell)

        let optionModel = questionViewModel.datafordisplay(atindex: indexPath)
        cell.setOptions(Options1: optionModel)

        cell.type = c?.cellType()
        currentCell(c: cell, index: indexPath.row)
        return cell
    }

因此在tableview单元格中,它包含 以下内容。 1.一键式 2.标签(此处显示选项) 因此,在选择一个选项时,我将单击按钮,这时按钮将设置背景图像。当单击按钮后,按钮图像变为单选按钮选中状态时,该图像将最初为单选按钮,并且未选中。

然后我在tableview的didselect单元格中给出了代码。 现在我的问题是选择单元格然后滚动tableview之后。单元格的内容没有刷新。如何解决问题。 我的表格视图中有几节。

我的模特:-

import UIKit
typealias JSONDictionary = [String:Any]

enum AnswerType:Int
{
    case CheckBoxCell = 1
    case RadioTypeCell

    case SmileyTypeCell
    case StarRatingTypeCell
    case TextTypeCell

    func cellType() -> CellType{
        switch self {
        case .CheckBoxCell: return .checkboxtype
        case .RadioTypeCell :return .radiotype

        case .SmileyTypeCell: return .smileytype
        case .StarRatingTypeCell: return .starratingtype
        case  .TextTypeCell: return .textfieldtype
        default: return .radiotype
        }
    }



}
class QuestionListModel: NSObject {

    var dataListArray33:[NH_OptionsModel] = []


    var id:Int!
    var question:String!
    var buttontype:String!
    var options:[String]?
    var v:String?
    var answerType:NHAnswerType?


      var optionsModelArray:[OptionsModel] = []
    init(dictionary :JSONDictionary) {

        guard   let question = dictionary["question"] as? String,
            let typebutton = dictionary["button_type"] as? String,

                let id = dictionary["id"] as? Int
             else {
                return

        }

        self.answerType = AnswerType(rawValue: Int(typebutton)!)

        if let options = dictionary["options"] as? [String]{
            print(options)

           print(options)


            for values in options{

                print(values)

                let optionmodel = OptionsModel(values: values)
                self.optionsModelArray.append(optionmodel)

            }


        }


        self.buttontype = typebutton
        self.question = question
        self.id = id

                   }

}

我的视图模型:-

 var OptionListArray:Array<OptionsModel>? = []

    init(withdatasource  newDatasourceModel:QuestionDataSourceModel,withmodel model:HomeModel) {
        datasourceModel = newDatasourceModel
        homemodel1 = model
         print(homemodel1)


        print(datasourceModel.dataListArray)
    }

    func numberOfSections() -> Int{
        print((datasourceModel.dataListArray?.count)!)
        return (datasourceModel.dataListArray?.count)!
    }

    func titleForHeaderInSection(atsection section: Int) -> QuestionListModel {
        return datasourceModel.dataListArray![section]
    }

    func numberOfRowsIn(section:Int) -> Int {

        print( datasourceModel.dataListArray?[section].optionsModelArray.count ?? 0)
        return datasourceModel.dataListArray?[section].optionsModelArray.count ?? 0
        // return self.questionsModelArray?[section].optionsModelArray.count ?? 0
    }

    func datafordisplay(atindex indexPath: IndexPath) -> NH_OptionsModel{

        print(datasourceModel.dataListArray![indexPath.section].optionsModelArray[indexPath.row])


        return datasourceModel.dataListArray![indexPath.section].optionsModelArray[indexPath.row]

    }

didselect表格视图:-

  func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath){

        if questionViewModel.isselected == true{

            let selectedIndexPathAtCurrentSection = questionViewModel.selectedIndexPaths.filter({ $0.section == indexPath.section})
            for indexPath in selectedIndexPathAtCurrentSection {
                tableview.deselectRow(at: indexPath, animated: true)

                if let indexOf = questionViewModel.selectedIndexPaths.index(of: indexPath) {

                    questionViewModel.selectedIndexPaths.remove(at: indexOf)

                    let questionModel = questionViewModel.titleForHeaderInSection(atsection: indexPath.section)

                    questionViewModel.question = questionModel.question

                    questionViewModel.insertQuestList(answer: questionViewModel.question!, Index: indexPath.section)


                    let c = questionModel.answerType
                    print(c)
                    let cellClass = c?.cellType().getClass()
                    print(cellClass)
                    let cell = tableview.cellForRow(at: indexPath) as? BaseCell
                    print(cell)

                  //  let cell = tableview.cellForRow(at: indexPath) as? NH_QuestionListCell


                    questionViewModel.button = questionModel.buttontype

                    let value: Int = questionModel.id

                    let string = String(describing: value)

                    questionViewModel.question_id = string

                    questionViewModel.question = questionModel.question

                    let model = questionViewModel.datafordisplay(atindex: indexPath)


                    if questionModel.buttontype == "2" {
                        questionViewModel.insertStrng(answer: model.values!, Index: indexPath.section)
                        cell?.setOptions(OptionsisSelected:questionViewModel.datafordisplay(atindex: indexPath))

                    }

                    else if questionModel.buttontype == "1"{
                        questionViewModel.insertStrng(answer: model.values!, Index: indexPath.section)
                        cell?.setOptions1(OptionsisSelected:questionViewModel.datafordisplay(atindex: indexPath))
                    }

                    questionViewModel.isselected = model.isSelected!
                }
            }

            questionViewModel.selectedIndexPaths.append(indexPath)

            let questionModel = questionViewModel.titleForHeaderInSection(atsection: indexPath.section)
            let c = questionModel.answerType
            print(c)
            let cellClass = c?.cellType().getClass()
            print(cellClass)
            let cell = tableview.cellForRow(at: indexPath) as? BaseCell
            print(cell)
            let model = questionViewModel.datafordisplay(atindex: indexPath)

            if questionModel.buttontype == "1"{
                questionViewModel.insertStrng(answer: model.values!, Index: indexPath.section)
                cell?.setOptions(OptionsSelected:questionViewModel.datafordisplay(atindex: indexPath))

            }

            else if  questionModel.buttontype == "2"{
                questionViewModel.insertStrng(answer: model.values!, Index: indexPath.section)
                cell?.setOptions1(OptionsSelected:questionViewModel.datafordisplay(atindex: indexPath))
                //   }
                print(questionViewModel.answers1)
            }



            questionViewModel.isselected = model.isSelected!


            questionViewModel.question = questionModel.question

            questionViewModel.questionlist(answer: questionViewModel.question!)

            questionViewModel.insertQuestList(answer: questionModel.question, Index: indexPath.section)

            let value: Int = questionModel.id

            let string = String(describing: value)

            questionViewModel.question_id = string

            questionViewModel.question(answer: questionViewModel.question_id!)

            questionViewModel.questIdList(answer: questionViewModel.question_id!, Index: indexPath.section)

            questionViewModel.button = questionModel.buttontype


        }

            // False condition

        else if questionViewModel.isselected == false{


        let questionModel = questionViewModel.titleForHeaderInSection(atsection: indexPath.section)

        questionViewModel.question = questionModel.question

        questionViewModel.insertQuestList(answer: questionViewModel.question!, Index: indexPath.section)


        let c = questionModel.answerType
        print(c)
        let cellClass = c?.cellType().getClass()
        print(cellClass)
        let cell = tableview.cellForRow(at: indexPath) as? BaseCell
        print(cell)
        let model = questionViewModel.datafordisplay(atindex: indexPath)
        print(model.values)


        if questionModel.buttontype == "1"{
            questionViewModel.insertStrng(answer: model.values!, Index: indexPath.section)
            cell?.setOptions(OptionsSelected:questionViewModel.datafordisplay(atindex: indexPath))
              print(questionViewModel.answers1)
        }

            //Radio Button

        else if  questionModel.buttontype == "2"{

            questionViewModel.insertStrng(answer: model.values!, Index: indexPath.section)
            cell?.setOptions1(OptionsSelected:questionViewModel.datafordisplay(atindex: indexPath))
            //   }
            print(questionViewModel.answers1)
        }

        questionViewModel.isselected = model.isSelected!

        let value: Int = questionModel.id

        let string = String(describing: value)

        questionViewModel.question_id = string
        questionViewModel.question(answer: questionViewModel.question_id!)

        questionViewModel.questIdList(answer: questionViewModel.question_id!, Index: indexPath.section)

        questionViewModel.button = questionModel.buttontype

        questionViewModel.selectedIndexPaths.append(indexPath)

        }

    }

1 个答案:

答案 0 :(得分:0)

确保您的模型对象值是在按钮单击事件上更新,并且您需要在Cell的索引处更新单元ui。

此处是有关如何在索引的单元格处更新ui的参考代码。 注意:未在xcode中检查代码,因此可能是一些小错误。

import re
import glob
path = '/home/python/resumes/*.txt'
files = glob.glob(path)
x=[]
y=[]
info=dict()
regex1=re.compile(r'[\w\.+_-]+@[\w\.-]+\.[a-zA-Z0-9-.]')
#regex2=re.compile(r'(\+91)?[789][0-9]{9}')
for name in files:
    file_name=open(name,"r")
    for line in file_name:
        email_list=regex1.findall(line)
        #phone_list=re.findall(r'^(\+91[\-\s]?)?[89]\d{9}$',line)
        x.extend(email_list)
for name1 in files:
    file_name=open(name1,"r")
    for line1 in file_name:
        phone_list=re.findall(r'^(\+91[\-\s]?)?[89]\d{9}$',line1)
        y.extend(phone_list)
#info['email']=x
#info['phone']=y
#y.append(x)
#print(info)
print(x)
print(y)