【问题标题】:Having issue in reuseable cell in UITableView cellUITableView 单元格中的可重用单元格存在问题
【发布时间】:2017-02-23 13:03:55
【问题描述】:

我在单个UITableViewCell 中有两种设计。这是我在加载 viewController 时想要并得到的设计

但滚动后我得到以下结果。

我认为这个问题是由 UITableViewCell 的可重用性引起的。这是我的 cellForRowAtIndexPath 代码

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

        let cell = tableView.dequeueReusableCell(withIdentifier: "notificationCell", for: indexPath) as! NotificationTableViewCell
        cell.selectionStyle = .none
        cell.btnClose.tag = indexPath.row
        let noti_flag = (arrayNotificationList.object(at: indexPath.row) as! NSDictionary).object(forKey: "noti_flag") //as! String
         let noti_flag_string = NSString(format: "%@", noti_flag as! CVarArg) as String

        cell.lbl_From.text = (arrayNotificationList.object(at: indexPath.row) as! NSDictionary).object(forKey: "caller_id") as? String ?? ""

        if noti_flag_string == "0" {
            cell.lblTime.isHidden = true
            cell.imgThumbIcon.isHidden = true
            cell.lbl_remaining.isHidden = true
            cell.lbl_mm_text.text = "Missed call"
            cell.lbl_mm_text.font = cell.lbl_mm_text.font.withSize(23)

        }
        else{

            var startAttributedText = (arrayNotificationList.object(at: indexPath.row) as! NSDictionary).object(forKey: "rebound_start_time") as! String
            var endAttributedText = (arrayNotificationList.object(at: indexPath.row) as! NSDictionary).object(forKey: "rebound_end_time") as! String
            startAttributedText = Model.shared.convertLocalTimeToServer(timeString: startAttributedText,isTimeFromServer: true)
            endAttributedText = Model.shared.convertLocalTimeToServer(timeString: endAttributedText,isTimeFromServer: true)
            let dateString:String = startAttributedText + " - " + endAttributedText

            cell.lblTime.attributedText = convertStringToAttr(dateString: dateString)

            cell.lbl_remaining.text = (arrayNotificationList.object(at: indexPath.row) as! NSDictionary).object(forKey: "remaining_time") as? String ?? ""

            cell.lbl_mm_text.text = (arrayNotificationList.object(at: indexPath.row) as! NSDictionary).object(forKey: "mm_text") as? String ?? ""

            //cell.lbl_From.text = (arrayNotificationList.object(at: indexPath.row) as! NSDictionary).object(forKey: "caller_id") as? String ?? ""

            let mm_type = (arrayNotificationList.object(at: indexPath.row) as! NSDictionary).object(forKey: "mm_type") as! String//"img"
            switch mm_type {
            case "img":
                cell.imgThumbIcon.image = UIImage(named: "thumb_camera")
            case "vid":
                cell.imgThumbIcon.image = UIImage(named: "thumb_video")
            case "aud":
                cell.imgThumbIcon.image = UIImage(named: "thumb_audio")
            case "str":
                cell.imgThumbIcon.image = UIImage(named: "thumb_sticker")
            case "txt":
                cell.imgThumbIcon.image = UIImage(named: "thumb_text")
            case "brd":
                cell.imgThumbIcon.image = UIImage(named: "thumb_brand")
            default:
                cell.imgThumbIcon.image = UIImage(named: "thumb_camera")
            }
        }


        return cell
    }

所以请帮我解决这个问题。提前致谢。

【问题讨论】:

  • 我认为问题在arrayNotificationList 。请调试并检查您的数组值。
  • @nirav 您可以轻松管理使用两个单元格。
  • cell.lblTime.isHidden = true 在 else 情况下你不做 cell.lblTime.isHidden = false。每次修改属性时,请考虑在其他情况下该怎么做:隐藏/显示/更改值等。否则,您可以覆盖 prepareForReuse() 来执行此操作,并隐藏/显示/重置值。
  • 非常感谢@Larme。这对我很有帮助。
  • 请将此作为答案发布,以便我标记为已接受。@Larme

标签: ios swift uitableview swift3 reuseidentifier


【解决方案1】:

问题是当你滚动时,

        cell.lblTime.isHidden = true

行将标签隐藏在单元格中。重复使用时,它仍然是隐藏的,所以中间的标签放大以填满剩余空间。

解决办法是,

A.创建另一个单元子类,它可以显着清理代码,并且不再需要显示或隐藏标签。

B.确保设置

cell.lblTime.isHidden = false

在 else 子句中。

我希望这会有所帮助。

【讨论】:

    猜你喜欢
    • 2016-05-13
    • 2013-04-07
    • 1970-01-01
    • 2014-12-02
    • 1970-01-01
    • 2018-11-27
    • 1970-01-01
    • 2016-07-08
    • 1970-01-01
    相关资源
    最近更新 更多