programming_advisor programming_advisor - 4 years ago 136
Swift Question

Avoid Data repeatation 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 .

enter image description here

enter image description here

Answer Source

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

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download