UITableView display first 5 rows of an array

I have a UITableView with 1 cell and when the array loads I just want for the user to see the content of the first 5 rows and blur the rest. So, if there is an array with 20 items, the first 5 need to be visible and the remaining 15 with a blur. With the code below, I'm able to just add a blur to row 5 only, I can't figure this out. Any help is greatly appreciated.


Code Block
let visualEffectView = UIVisualEffectView(effect: UIBlurEffect(style: .light))
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return array.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath) as! CustomTableViewCell
cell.cellLbl?.text = array[indexPath.row]
if indexPath.row == 5 {
visualEffectView.frame = cell.bounds
visualEffectView.layer.masksToBounds = true
cell.addSubview(visualEffectView)
}
return cell
}


Replies

I was able to find a solution for this. Here is the updated code:

Code Block
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    return array.count
  }
   
  func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath) as! CustomTableViewCell
    cell.cellLbl?.text = array[indexPath.row]
     
    cell.blurView.isHidden = true
    if indexPath.row >= 5 {
       
      cell.blurView.isHidden = false
    }
    return cell
  }

Just to get (slightly) more compact code:
Code Block
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath) as! CustomTableViewCell
cell.cellLbl?.text = array[indexPath.row]
cell.blurView.isHidden = indexPath.row < 5
return cell
}