代码之家  ›  专栏  ›  技术社区  ›  Shivam Tripathi

重置uiTableViewCell工作不正常/未选中的单元格在滚动后显示为选中

  •  1
  • Shivam Tripathi  · 技术社区  · 7 年前

    问题是当我在选择CollectionViewCell(日期28为紫色)后滚动TableView时。TableView底部的少数CollectionViewCells显示为选中状态(此处,CollectionView位于TableView内部)。

    代码如下:

    类viewController:uiviewController{
    
    @IBoutlet弱var tblview:uiTableView!
    
    var storedoffsets=[int:cgfloat]()//将由jtappleCalender使用
    let formatter=dateformiter()//nsdateformiter
    
    
    重写func viewdidload()。{
    super.viewdidload()。
    
    self.tblview.delegate=自己
    self.tblview.datasource=自己
    self.tblview.reloaddata()。
    }
    
    func handlecell配置(cell:jtapplecell?,电池状态:电池状态){
    handlecellselection(视图:单元格,单元格状态:单元格状态)
    }
    
    
    func handlecell选择(视图:jtapplecell?,电池状态:电池状态){
    
    guard let mycustomcell=查看为?日历单元格其他返回
    如果选择CellState.is{
    mycustomcell.contentview.layer.cornerradius=10
    mycustomcell.contentview.backgroundcolor=uicolor.init(红色:0.26,绿色:0.10,蓝色:0.39,alpha:1.0)
    }否则{
    myCustomCell.ContentView.Layer.CornerRadius=0
    mycustomcell.contentview.backgroundcolor=uicolor.clear
    }
    }
    }
    < /代码> 
    
    

    uiTableView单元格:

    class tablecell:uiTableViewcell{
    
    @iboutlet弱var lbldate:uilabel!
    @IBoutlet弱var集合视图日期:jTappleCalendarView!
    
    
    重写func prepareForReuse()。{
    super.PrepareForReuse()。
    
    }
    }
    
    扩展表格单元格{
    
    func setcollectionviewdatasourcelegate<d:jtappleCalendarviewdatasource&jtappleCalendarviewdelegate>(u datasourcelegate:d,forrow row:int){
    
    collectionviewDate.calendarDelegate=数据源委派
    collectionviewDate.calendardatasource=数据源委托
    collectionviewDate.tag=行
    collectionviewdate.setContentOffset(collectionviewdate.contentOffset,动画:false)
    CollectionViewDate.ReloadData()。
    }
    
    var集合视图偏移量:cgfloat{
    设置collectionviewdate.contentoffset.x=newvalue_
    get返回collectionviewdate.contentoffset.x
    }
    }
    
    
    扩展视图控制器:UITableViewDataSource、UITableViewDelegate{
    
    func numberofsections(在TableView:uiTableView中)->int{
    返回1
    }
    
    func tableview(uTableView:uiTableView,numberofRowsPreagon部分:int)->int{
    返回7
    }
    
    func tableview(u tableview:uitableview,cellforrowat indexpath:indexpath)->uitableviewcell{
    
    让cell=self.tblview.dequeuerusablecell(withIdentifier:“tablecell”,for:indexpath)作为!表格单元
    
    cell.lbldate.text=“日期”+“\(indexPath.row)”
    返回单元
    }
    
    
    func tableview(u tableview:uitableview,willdisplay cell:uitableviewcell,forrowat indexpath:indexpath){
    
    guard let tableViewCell=单元格为?TableCell Else返回
    TableViewCell.setCollectionViewDataSourceDelegate(self,forRow:indexPath.row)
    tableviewscell.collectionviewoffset=storedoffsets[indexpath.row]?0?
    }
    
    func tableview(uTableView:uiTableView,didendDisplaying cell:uiTableViewCell,forrowat indexPath:indexPath){
    
    guard let tableViewCell=单元格为?TableCell Else返回
    storedOffsets[indexPath.row]=TableViewCell.CollectionViewOffset
    }
    
    }
    
    
    类CalendarCell:jTappleCell{
    
    @iboutlet弱var lbldate:uilabel!
    }
    
    
    
    
    扩展视图控制器:JTappleCalendarViewDataSource、JTappleCalendarViewDelegate{
    
    
    func日历(日历:jtappleCalendarView,将显示单元格:jtappleCell,Foritemat日期:日期,单元格状态:单元格状态,索引路径:索引路径){
    让myCustomCell=单元格为!钙质细胞
    sharedFunctionToConfigureCell(myCustomCell:myCustomCell,CellState:CellState,日期:日期)
    }
    
    func日历(日历:jTappleCalendarView,cellForItemAt日期:日期,cellstate:cellstate,indexPath:indexPath)->jTappleCell{
    
    让cell=calendar.dequeuersablejtapplecell(带有ReuseIdentifier:“calendarCell”,用于:indexPath)作为!钙质细胞
    cell.lbldate.text=单元格状态.text
    self.calendar(日历,将显示:单元格,foritemat:日期,单元格状态:单元格状态,索引路径:索引路径)
    
    返回单元
    }
    
    func calendar(u calendar:jtappleCalendarView,didDeselectDate日期:日期,单元格:jtappleCell?,电池状态:电池状态){
    handlecell配置(cell:cell,cellstate:cellstate)
    }
    
    func日历(u calendar:jtappleCalendarView,didselectDate:Date,cell:jtappleCell?,电池状态:电池状态){
    
    handlecell配置(cell:cell,cellstate:cellstate)
    }
    
    func sharedfunctiontoconfigureCell(myCustomCell:CalendarCell,CellState:CellState,日期:日期){
    handlecell配置(cell:mycustomcell,cellstate:cellstate)
    }
    
    func configureCalendar(u calendar:jtappleCalendarView)->配置参数{
    
    让当前日期=日期()。
    让enddate=calendar.current.date(通过添加:.month,value:4,to:date())
    让参数=配置参数(开始日期:当前日期,
    结束日期:结束日期!,
    行数:1,
    生成日期:.第一个月,
    GenerateOutdates:。关闭,
    hasstrictboundaries:错误)
    calendar.scrollToDate(当前日期,triggerscrollToDateDelegate:false,animatescroll:false)
    返回参数
    }
    
    func configurevisibleCell(myCustomCell:CalendarCell,CellState:CellState,日期:日期){
    handlecell配置(cell:mycustomcell,cellstate:cellstate)
    }
    
    }
    < /代码> 
    
    

    如果需要添加其他内容,请通知我。

    问题是当我在选择CollectionViewCell(日期28为紫色)后滚动TableView时。在TableView底部的一些CollectionViewCells似乎被选中(此处,CollectionView位于TableView内部)。

    代码如下:

    class ViewController: UIViewController {
    
        @IBOutlet weak var tblView: UITableView!
    
        var storedOffsets = [Int: CGFloat]()           // Will be used by JTAppleCalender
        let formatter = DateFormatter()                // NSDateFormatter
    
    
        override func viewDidLoad() {
            super.viewDidLoad()
    
            self.tblView.delegate = self
            self.tblView.dataSource = self
            self.tblView.reloadData()
        }
    
        func handleCellConfiguration(cell: JTAppleCell?, cellState: CellState) {
            handleCellSelection(view: cell, cellState: cellState)
        }
    
    
        func handleCellSelection(view: JTAppleCell?, cellState: CellState) {
    
            guard let myCustomCell = view as? CalenderCell else {return }
            if cellState.isSelected {
                myCustomCell.contentView.layer.cornerRadius = 10
                myCustomCell.contentView.backgroundColor = UIColor.init(red: 0.26, green: 0.10, blue: 0.39, alpha: 1.0)
            } else {
                myCustomCell.contentView.layer.cornerRadius =  0
                myCustomCell.contentView.backgroundColor = UIColor.clear
            }
        }
    }
    

    uiTableView单元格:

    class TableCell: UITableViewCell{
    
        @IBOutlet weak var lbldate: UILabel!
        @IBOutlet weak var collectionViewDate: JTAppleCalendarView!
    
    
        override func prepareForReuse() {
            super.prepareForReuse()
    
          }
    }
    
    extension TableCell {
    
        func setCollectionViewDataSourceDelegate<D: JTAppleCalendarViewDataSource & JTAppleCalendarViewDelegate>(_ dataSourceDelegate: D, forRow row: Int) {
    
            collectionViewDate.calendarDelegate = dataSourceDelegate
            collectionViewDate.calendarDataSource = dataSourceDelegate
            collectionViewDate.tag = row
            collectionViewDate.setContentOffset(collectionViewDate.contentOffset, animated:false)
            collectionViewDate.reloadData()
        }
    
        var collectionViewOffset: CGFloat {
            set { collectionViewDate.contentOffset.x = newValue }
            get { return collectionViewDate.contentOffset.x }
        }
    }
    
    
    extension ViewController : UITableViewDataSource, UITableViewDelegate{
    
        func numberOfSections(in tableView: UITableView) -> Int {
            return 1
        }
    
        func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
            return 7
        }
    
        func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    
            let cell = self.tblView.dequeueReusableCell(withIdentifier: "TableCell", for: indexPath) as! TableCell
    
            cell.lbldate.text = "date "+"\(indexPath.row)"
            return cell
        }
    
    
        func tableView(_ tableView: UITableView, willDisplay cell: UITableViewCell, forRowAt indexPath: IndexPath) {
    
            guard let tableViewCell = cell as? TableCell else { return }
            tableViewCell.setCollectionViewDataSourceDelegate(self, forRow: indexPath.row)
            tableViewCell.collectionViewOffset = storedOffsets[indexPath.row] ?? 0
        }
    
        func tableView(_ tableView: UITableView, didEndDisplaying cell: UITableViewCell, forRowAt indexPath: IndexPath) {
    
            guard let tableViewCell = cell as? TableCell else { return }
            storedOffsets[indexPath.row] = tableViewCell.collectionViewOffset
        }
    
    }
    
    
    class CalenderCell: JTAppleCell{
    
        @IBOutlet weak var lblDate: UILabel!
    }
    
    
    
    
    extension ViewController: JTAppleCalendarViewDataSource , JTAppleCalendarViewDelegate {
    
    
        func calendar(_ calendar: JTAppleCalendarView, willDisplay cell: JTAppleCell, forItemAt date: Date, cellState: CellState, indexPath: IndexPath) {
            let myCustomCell = cell as! CalenderCell
            sharedFunctionToConfigureCell(myCustomCell: myCustomCell, cellState: cellState, date: date)
        }
    
        func calendar(_ calendar: JTAppleCalendarView, cellForItemAt date: Date, cellState: CellState, indexPath: IndexPath) -> JTAppleCell {
    
            let cell = calendar.dequeueReusableJTAppleCell(withReuseIdentifier: "CalenderCell", for: indexPath) as! CalenderCell
            cell.lblDate.text = cellState.text
            self.calendar(calendar, willDisplay: cell, forItemAt: date, cellState: cellState, indexPath: indexPath)
    
            return cell
        }
    
        func calendar(_ calendar: JTAppleCalendarView, didDeselectDate date: Date, cell: JTAppleCell?, cellState: CellState) {
            handleCellConfiguration(cell: cell, cellState: cellState)
        }
    
        func calendar(_ calendar: JTAppleCalendarView, didSelectDate date: Date, cell: JTAppleCell?, cellState: CellState) {
    
            handleCellConfiguration(cell: cell, cellState: cellState)
        }
    
        func sharedFunctionToConfigureCell(myCustomCell: CalenderCell, cellState: CellState, date: Date) {
            handleCellConfiguration(cell: myCustomCell, cellState: cellState)
        }
    
        func configureCalendar(_ calendar: JTAppleCalendarView) -> ConfigurationParameters {
    
            let currentDate = Date()
            let endDate = Calendar.current.date(byAdding: .month, value: 4, to: Date())
            let parameters = ConfigurationParameters(startDate: currentDate,
                                                     endDate: endDate!,
                                                     numberOfRows: 1,
                                                     generateInDates: .forFirstMonthOnly,
                                                     generateOutDates: .off,
                                                     hasStrictBoundaries: false)
            calendar.scrollToDate(currentDate, triggerScrollToDateDelegate: false, animateScroll: false)
            return parameters
        }
    
        func configureVisibleCell(myCustomCell: CalenderCell, cellState: CellState, date: Date) {
            handleCellConfiguration(cell: myCustomCell, cellState: cellState)
        }
    
    }
    

    如果需要添加其他内容,请通知我。

    3 回复  |  直到 7 年前
        1
  •  1
  •   Jen Jose    7 年前

    这不是使用jTappleCalendar CollectionView的建议方法。 您必须将所选日期存储在某个数据源变量中,并且当选择/取消选择某个单元格时。重新加载时,根据存储的值,必须相应地突出显示单元格。

    //This will store the selected dates against the cell rows.
    var selectedDatesVsIndex = [Int : Date]()
    

    在选择/取消选择JTappleCalendar委托的方法时,将所选日期添加到维护rowIndex->dateSelected的本地字典。

       func calendar(_ calendar: JTAppleCalendarView, didSelectDate date: Date, cell: JTAppleCell?, cellState: CellState) {
            cell?.isSelected = true
            selectedDatesVsIndex.merge([calendar.tag : date]) { (d1, d2) -> Date in return d1 }
    
            calendar.reloadData()
    //        handleCellConfiguration(cell: cell, cellState: cellState)
        }
    
    
      func calendar(_ calendar: JTAppleCalendarView, didDeselectDate date: Date, cell: JTAppleCell?, cellState: CellState) {
            cell?.isSelected = false
            selectedDatesVsIndex.removeValue(forKey: calendar.tag)
            calendar.reloadData()
    
    //        handleCellConfiguration(cell: cell, cellState: cellState)
        }
    

    除此之外,我在TableViewCells中将标记设置为indexPath.row,还添加了Collection View的Reload方法。这样地

     func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    
            let cell = self.tblView.dequeueReusableCell(withIdentifier: "TableCell", for: indexPath) as! TableCell
            cell.tag = indexPath.row
            cell.collectionViewDate.tag = indexPath.row
            cell.collectionViewDate.reloadData()
    
            cell.lbldate.text = "date "+"\(indexPath.row)"
            return cell
        }
    

    显示所选背景色的单元格自定义应在CellForRowat索引中完成。

     func calendar(_ calendar: JTAppleCalendarView, cellForItemAt date: Date, cellState: CellState, indexPath: IndexPath) -> JTAppleCell {
    
            let cell = calendar.dequeueReusableJTAppleCell(withReuseIdentifier: "CalenderCell", for: indexPath) as! CalenderCell
            cell.lblDate.text = cellState.text
            cell.backgroundColor =  UIColor.clear
    
            if selectedDatesVsIndex.keys.index(of: calendar.tag) != nil {
                if let object = selectedDatesVsIndex[calendar.tag] , object.compare(date) == .orderedSame {
                    cell.backgroundColor = UIColor.gray
                }
            }
     //       self.calendar(calendar, willDisplay: cell, forItemAt: date, cellState: cellState, indexPath: indexPath)
    
            return cell
        }
    

    请删除其他杂乱方法,如

     override func prepareForReuse() {
            super.prepareForReuse()
    
    //        let hasContentView = self.subviews .contains(self.contentView)
    //        if(hasContentView){
    //            self.contentView.removeFromSuperview()
    //        }
        }
    
        2
  •  0
  •   Varsha Parmar    7 年前

    请检查是否存在特定索引路径的值。如果存在值,则按所选方式生成该值,它将消除重叠问题。

        3
  •  -1
  •   Prakash salawria    7 年前

    只需更改(cellForRowat indexPath:indexPath)方法中的颜色,而不更改handleCellSelection(view:jtappleCell?),CellState:CellState)

    像这样:

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    
            let cell = self.tblView.dequeueReusableCell(withIdentifier: "TableCell", for: indexPath) as! TableCell
    
            cell.lbldate.text = "date "+"\(indexPath.row)"
    
             if cell.isSelected == true
            {
             cell.contentView.layer.cornerRadius = 10
                cell.contentView.backgroundColor = UIColor.init(red: 0.26, green: 0.10, blue: 0.39, alpha: 1.0)   
    
            }
       else
       {
    cell.contentView.layer.cornerRadius = 0
     cell.contentView.backgroundColor = UIColor.clear
        }
            return cell
     }
    

    并编写代码以在didselect方法上选择单元格。

    推荐文章