UITableView only display 10 cells/rows

匿名 (未验证) 提交于 2019-12-03 00:44:02

问题:

I have two scopes/tabs on my TableViewController, "All" & "Top 10". I only want my "Top 10" tab to display only 10 cells/rows instead of displaying everything. Do look at my images for better references.

P.S: My UITableView is used to display real-time results of over 30 booths for my event using firebase database. Also, case 0 is "All", case 1 is "Top 10".

TableViewController

回答1:

Declare a variable in your view controller pointing to which segment is selected

var selectedTab = 0 

In selectedScopeButtonIndexDidChange, assign value to above variable based on selection change:

func searchBar(_ searchBar: UISearchBar, selectedScopeButtonIndexDidChange selectedScope: Int) {             switch selectedScope {             case 0:                 selectedTab = 0                 currentpostDataArray.removeAll()                 postData2.removeAll()                 for data in tableDataArray {                     currentpostDataArray.append(data.boothName)                      let value = String(describing: data.boothRating)                     postData2.append(value)                 }                 self.tableView.reloadData()              case 1:                 selectedTab = 1                 currentpostDataArray.removeAll()                 postData2.removeAll()                 let sortedTableData = tableDataArray.sorted(by: { $0.boothRating > $1.b  oothRating })             for data in sortedTableData {                 currentpostDataArray.append(data.boothName)                  let value = String(describing: data.boothRating)                 postData2.append(value)             }             self.tableView.reloadData()          default:             break          }         tableView.reloadData()     } 

In numberOfRowsInSection, check for selected segment and set row count:

override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {             if selectedTab = 0 {               return currentpostDataArray.count             } else {               if currentpostDataArray.count >= 10 {                 return 10               } else {                 return currentpostDataArray.count               }             }  } 


回答2:

After sorting your tableDataArray, you should create an array with the first 10 elements from tableDataArray



回答3:

You need to implement your logic as you change the index of your segmentControl.Set a tag variable as the index of segment changes and reload your table view once again. In your codes you have returned all the cells in your table view whatever the Segmentindex becomes.

You can try this and if you feel confused feel free to comment below:

import Foundation import UIKit class SegmentViewController : UIViewController,UITableViewDelegate,UITableViewDataSource {     @IBOutlet weak var uiSegmentControl: UISegmentedControl!     @IBOutlet weak var tableView: UITableView!     var tag : Int = 1     override func viewDidLoad() {         super.viewDidLoad()         self.tableView.delegate = self         self.tableView.dataSource = self     }       func numberOfSections(in tableView: UITableView) -> Int {         return 1     }      func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {         if tag == 1         {  //return total count             return 100         }         else if tag == 2         {             return 10         }         return 0         }       func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {         let cell = self.tableView.dequeueReusableCell(withIdentifier: "Cell") as! UITableViewCell         cell.textLabel?.text = "CEll"         return cell     }       @IBAction func didchange(_ sender: Any) {         if uiSegmentControl.selectedSegmentIndex == 1         {           print("Segment 1 is selected")             self.tag = 2             self.tableView.reloadData()         }         else         {             print("Segment 2 is selected")              self.tag = 1             self.tableView.reloadData()          }     } } 


回答4:

You need to customize when data appending in loadDara2 function.

func loadDara2() {     let sortedTableData = tableDataArray.sorted(by: { $0.boothRating > $1.boothRating })     for data in sortedTableData {         currentpostDataArray.append(data.boothName)          let value = String(describing: data.boothRating)           if (postData2.count != 10){                postData2.append(value)             }else {               break          }     } } 

This will help.



回答5:

Create a outlet of segment control and connect it.

@IBOutlet weak var segment: UISegmentedControl! 

Now replace numberOfRowsInSection method with below code.

func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {     return segment.selectedSegmentIndex == 0 ? currentpostDataArray.count : min(currentpostDataArray.count, 10) }  


标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!