简体   繁体   中英

Avoid Data repetition in UITableView cells when scrolling

I need an auto scrolling Image slider in top of viewcontroller followed by list of some entities(dynamic cells with image and title). To implement that I have taken a uitableview and I'm adding scrollview to my first cell, and my code is as follows

public func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {

    if indexPath.row  == 0{
       return 200
    }
    else {
       return 50
    }    
}

public func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    return 20    
}

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

    let cell  = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath)

    if indexPath.row  == 0 {

       let sv = UIScrollView(frame: CGRect(x: 0, y: 0, width: cell.frame.width, height: cell.frame.height))

       sv.auk.show(url: "url for image1")
       sv.auk.show(url: "url for image2")

       cell.addSubview(sv)
       print("inside if")
      }
      else {

       print("else")
       cell.textLabel?.text = "cool"
      }
      return cell                
}

I'm using this repository to create image slider which creates slider on a scrollview, So for first cell I have added scrollview. But as you can see in the picture the image slider reappears on multiple rows. Please tell me what is the mistake that I'm doing.In case if there is any better approaches please suggest .

在此处输入图片说明

在此处输入图片说明

Try taking a separate class for dynamic cells. Dequeue both the cells (static and dynamic cells) in the cellForRow method as follows:

func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {

    return indexPath.row == 0 ? 200 : 50
}

func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { return 20 }

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

    if indexPath.row  == 0 {

        let cell  = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath)

        scrollViewWidth = cell.frame.width
        scrollViewHeight = cell.frame.height
        let scrollView = prepareScrollView(width: scrollViewWidth, height: scrollViewHeight)
        cell.addSubview(scrollView )

        print("First row")
        return cell
       }
       else {

        let myCustomCell: MyCustomTableViewCellClass = tableView.dequeueReusableCell(withIdentifier: "MyCustomTableViewCellIdentieier", for: indexPath) as! MyCustomTableViewCellClass 
        myCustomCell.textLabel?.text = "Cool"

        print("Other dynamic rows")
        return myCustomCell
      }
}

func prepareScrollView(_ width: Float, height: Float) -> UIScrollView {

    let scrollViewFrame = CGRect(x: 0, y: 0, width: width, height: height)
    let scrollView = UIScrollView(frame: scrollViewFrame)
    scrollView.auk.show(url: "url for image1")
    scrollView.auk.show(url: "url for image2") 

    return scrollView
}

Take a separate class as MyCustomTableViewCellClass of type UITableViewCell and subclass your dynamic cell with this class. Don't forget to give cell identifier as "MyCustomTableViewCellIdentieier"

After this, your static cell will dequeue only once and no chances od repeating UI elements

You can try this code in your table view cell class -

 override func prepareForReuse() {
    //set your lable and image view to nil
}

SideMenuTVCell is a my custom UITableViewCell class -

Hope like this you have your own class within this class you add prepareForReuse() method -

class SideMenuTVCell: UITableViewCell {

@IBOutlet weak var iconIView: UIImageView!
@IBOutlet weak var lblTitle: UILabel!

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

override func setSelected(_ selected: Bool, animated: Bool) {
    super.setSelected(selected, animated: animated)

}

override func setHighlighted(_ highlighted: Bool, animated: Bool) {
    super.setHighlighted(highlighted, animated: animated)  

}

override func prepareForReuse() {
    //set your lable and image view to nil

   self.iconIView.image = nil
   self.lblTitle.text = nil

 }

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM