我的TableViewController上有两个范围/标签,“All”& “前10名”。我只希望我的“前10名”标签只显示10个单元格/行而不是显示所有内容。请查看我的图片以获得更好的参考。
P.S:我的UITableView用于使用firebase数据库显示我的活动超过30个展位的实时结果。此外,案例0为“全部”,案例1为“前10名”。
TableViewController
import UIKit
import FirebaseDatabase
var ref: DatabaseReference?
var databaseHandle: DatabaseHandle?
var postData = [String]()
var postData2 = [String]()
var currentpostDataArray = [String]()
var tableDataArray = [tableData]()
var tableDataArray2 = [tableData]()
var searchArray = [tableData]()
var selectedTab = 0
class TableViewController: UITableViewController, UISearchBarDelegate {
@IBOutlet var searchBar: UISearchBar!
override func viewDidLoad() {
super.viewDidLoad()
setUpSearchBar()
ref = Database.database().reference()//set the firebase reference
databaseHandle = ref?.child("Posts2").observe(.value, with: { (snapshot) in
// Code to execute when a child is added under "Posts"
postData.removeAll()
postData2.removeAll()
tableDataArray.removeAll()
for child in snapshot.children {
let snap = child as! DataSnapshot
let key = snap.key
let value = String(describing: snap.value!)
let rating = (value as NSString).integerValue
postData.append(key)
postData2.append(value)
tableDataArray.append(tableData(boothName: key, boothRating: rating))
currentpostDataArray = postData
}
self.tableView.reloadData()
})
databaseHandle = ref?.child("Posts2").queryOrderedByValue().queryLimited(toLast: 10).observe(.value, with: { (snapshot) in
// Code to execute when a child is added under "Posts"
postData.removeAll()
postData2.removeAll()
tableDataArray2.removeAll()
for child in snapshot.children {
let snap = child as! DataSnapshot
let key = snap.key
let value = String(describing: snap.value!)
let rating = (value as NSString).integerValue
postData.append(key)
postData2.append(value)
tableDataArray2.append(tableData(boothName: key, boothRating: rating))
currentpostDataArray = postData
}
self.tableView.reloadData()
})
}
private func setUpSearchBar() {
searchBar.delegate = self
}
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return currentpostDataArray.count
}
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath)
cell.textLabel?.text = currentpostDataArray[indexPath.row]
cell.detailTextLabel?.text = postData2[indexPath.row] + " ♥"
cell.detailTextLabel?.textColor = UIColor.red;
return cell
}
func searchBar(_ searchBar: UISearchBar, textDidChange searchText: String) {
switch searchBar.selectedScopeButtonIndex {
case 0:
selectedTab = 0
searchArray.removeAll()
searchArray = tableDataArray
currentpostDataArray.removeAll()
postData2.removeAll()
if !searchText.isEmpty {
for data in searchArray {
let item = data.boothName
if (item.lowercased().range(of: searchText.lowercased()) != nil)
{
currentpostDataArray.append(data.boothName)
let value = String(describing: data.boothRating)
postData2.append(value)
}
}
}
else {
loadDara()
}
self.tableView.reloadData()
case 1:
selectedTab = 1
searchArray.removeAll()
searchArray = tableDataArray2
currentpostDataArray.removeAll()
postData2.removeAll()
if !searchText.isEmpty {
for data in searchArray {
let item = data.boothName
if (item.lowercased().range(of: searchText.lowercased()) != nil)
{
currentpostDataArray.append(data.boothName)
let value = String(describing: data.boothRating)
postData2.append(value)
}
}
}
else {
loadDara2()
}
self.tableView.reloadData()
default:
break
}
}
func searchBar(_ searchBar: UISearchBar, selectedScopeButtonIndexDidChange selectedScope: Int) {
switch selectedScope {
case 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:
currentpostDataArray.removeAll()
postData2.removeAll()
let sortedTableData = tableDataArray2.sorted(by: { $0.boothRating > $1.boothRating })
for data in sortedTableData {
currentpostDataArray.append(data.boothName)
let value = String(describing: data.boothRating)
postData2.append(value)
}
self.tableView.reloadData()
default:
break
}
tableView.reloadData()
}
func loadDara() {
for data in tableDataArray {
currentpostDataArray.append(data.boothName)
let value = String(describing: data.boothRating)
postData2.append(value)
}
}
func loadDara2() {
let sortedTableData = tableDataArray2.sorted(by: { $0.boothRating > $1.boothRating })
for data in sortedTableData {
currentpostDataArray.append(data.boothName)
let value = String(describing: data.boothRating)
postData2.append(value)
}
}
}
class tableData {
var boothName: String
var boothRating: Int
init(boothName: String, boothRating: Int) {
self.boothName = boothName
self.boothRating = boothRating
}
}
答案 0 :(得分:2)
在视图控制器中声明一个指向选择了哪个段的变量
var selectedTab = 0
在selectedScopeButtonIndexDidChange
中,根据选择更改为上述变量指定值:
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()
}
在numberOfRowsInSection
中,检查所选的细分并设置行数:
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
}
}
}
答案 1 :(得分:0)
在对 tableDataArray 进行排序后,您应该使用 tableDataArray
中的前10个元素创建一个数组答案 2 :(得分:0)
您需要在更改 segmentControl 的索引时实现逻辑。将标记变量设置为段的索引更改并再次重新加载表视图。在您的代码中,无论Segmentindex变为什么,您都会返回表格视图中的所有单元格。
你可以尝试这个,如果你感到困惑,请随时评论:
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()
}
}
}
答案 3 :(得分:0)
您需要自定义何时附加loadDara2
功能的数据。
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
}
}
}
这会有所帮助。
答案 4 :(得分:0)
创建分段控件的出口并连接它。
@IBOutlet weak var segment: UISegmentedControl!
现在用以下代码替换 numberOfRowsInSection 方法。
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return segment.selectedSegmentIndex == 0 ? currentpostDataArray.count : min(currentpostDataArray.count, 10)
}