我有3个不同的数组,分别是cricketMatchArray、soccerMatchArray和tennisMatchArray。我在3个表视图中显示这3个数组数据,单击标题后,表视图将展开。面临的问题是表高度不会根据数据计数而变化。
This output is I'm getting and I want remove that red mark space
例如:* 如果具有数据的板球和网球阵列以及足球阵列为空,则足球桌高度不变 *
我想根据数组计数动态更改表的高度。
例如,如果具有数据的板球和网球阵列和足球阵列为空,则足球桌高度应为0。
下面是我的代码。
class AllLiveMatchesViewController: UIViewController {
@IBOutlet weak var cricketTableView: UITableView!
@IBOutlet weak var soccerTableView: UITableView!
@IBOutlet weak var tennisTableView: UITableView!
var selectedIndx = -1
var thereIsCellTapped = false
var cricketMatchArray = [LiveMatchesData]()
var soccerMatchArray = [LiveMatchesData]()
var tennisMatchArray = [LiveMatchesData]()
override func viewDidLoad() {
super.viewDidLoad()
cricketTableView.dataSource = self
cricketTableView.delegate = self
cricketTableView.separatorStyle = .none
cricketTableView.tableFooterView = UIView()
soccerTableView.dataSource = self
soccerTableView.delegate = self
soccerTableView.separatorStyle = .none
soccerTableView.tableFooterView = UIView()
tennisTableView.dataSource = self
tennisTableView.delegate = self
tennisTableView.separatorStyle = .none
tennisTableView.tableFooterView = UIView()
getCricketMatches()
getSoccerMatches()
getTennisMatches()
}
extension AllLiveMatchesViewController: UITableViewDelegate, UITableViewDataSource{
func numberOfSections(in tableView: UITableView) -> Int {
if tableView == cricketTableView
{
return cricketMatchArray.count
}
else if tableView == soccerTableView
{
return soccerMatchArray.count
}
else if tableView == tennisTableView
{
return tennisMatchArray.count
}
else
{
return 0
}
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
if tableView == cricketTableView
{
return cricketMatchArray[section].score.count
}
else if tableView == soccerTableView
{
return soccerMatchArray[section].score.count
}
else if tableView == tennisTableView
{
return tennisMatchArray[section].score.count
}
else
{
return 0
}
}
func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
if tableView == cricketTableView
{
let obj = cricketMatchArray[section]
if cricketMatchArray.count == 0
{
return 0
}
else
{
if obj.inplay == true && obj.status == "OPEN"
{
return 50
}
else if obj.inplay == false && obj.status == "OPEN"
{
return 0
}
else
{
return 0
}
}
}
else if tableView == soccerTableView
{
let obj = soccerMatchArray[section]
if obj.inplay == true && obj.status == "OPEN"
{
return 50
}
else if obj.inplay == false && obj.status == "OPEN"
{
return 0
}
else
{
return 0
}
}
else if tableView == tennisTableView
{
let obj = tennisMatchArray[section]
if obj.inplay == true && obj.status == "OPEN"
{
return 50
}
else if obj.inplay == false && obj.status == "OPEN"
{
return 0
}
else
{
return 0
}
}
else
{
return 0
}
}
func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
if tableView == cricketTableView
{
if indexPath.section == selectedIndx && thereIsCellTapped{
return 106
}
else{
return 0
}
}
else if tableView == soccerTableView
{
if indexPath.section == selectedIndx && thereIsCellTapped{
return 106
}
else{
return 0
}
}
else if tableView == tennisTableView
{
if indexPath.section == selectedIndx && thereIsCellTapped{
return 106
}
else{
return 0
}
}
else
{
return 0
}
}
func tableView(_ tableView: UITableView, heightForFooterInSection section: Int) -> CGFloat {
if tableView == cricketTableView
{
if (self.selectedIndx != section) && thereIsCellTapped{
return 0
}
else if (self.selectedIndx == section) && thereIsCellTapped{
return 20
}
else
{
return 0
}
}
else if tableView == soccerTableView
{
if (self.selectedIndx != section) && thereIsCellTapped{
return 0
}
else if (self.selectedIndx == section) && thereIsCellTapped{
return 20
}
else
{
return 0
}
}
else if tableView == tennisTableView
{
if (self.selectedIndx != section) && thereIsCellTapped{
return 0
}
else if (self.selectedIndx == section) && thereIsCellTapped{
return 20
}
else
{
return 0
}
}
else
{
return 0
}
}
func tableView(_ tableView: UITableView, viewForFooterInSection section: Int) -> UIView? {
let cell = tableView.dequeueReusableCell(withIdentifier: String(describing: ExpandExtTableViewCell.self)) as! ExpandExtTableViewCell
if section == selectedIndx && thereIsCellTapped{
cell.footerView.roundCorners(corners: [.bottomLeft,.bottomRight], radius: 10)
}
else
{
cell.footerView.roundCorners(corners: [.bottomLeft,.bottomRight], radius: 0)
}
return cell
}
func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
if tableView == cricketTableView
{
let cell = tableView.dequeueReusableCell(withIdentifier: String(describing: ExpandTableViewCell.self)) as! ExpandTableViewCell
let obj = cricketMatchArray[section]
if obj.inplay == false && obj.status == "CLOSE"
{
cell.liveView.isHidden = true
}
else if obj.inplay == true && obj.status == "OPEN"
{
cell.liveView.isHidden = false
}
cell.sportIcon.image = UIImage(named: "whiteball")
cell.teamNameLabel.text = obj.name ?? ""
cell.btnSelection.tag = section
cell.btnSelection.addTarget(self, action: #selector(AllLiveMatchesViewController.btnSectionClick(sender:)), for: .touchUpInside)
if section == selectedIndx && thereIsCellTapped{
cell.headerView.roundCorners(corners: [.topLeft,.topRight], radius: 10)
}
else
{
cell.headerView.roundCorners(corners: [.topLeft,.topRight,.bottomLeft,.bottomRight], radius: 10)
}
return cell
}
else if tableView == soccerTableView
{
let cell = tableView.dequeueReusableCell(withIdentifier: String(describing: SoccerExpandTableViewCell.self)) as! SoccerExpandTableViewCell
let obj = soccerMatchArray[section]
if obj.inplay == false && obj.status == "CLOSE"
{
cell.liveView.isHidden = true
}
else if obj.inplay == true && obj.status == "OPEN"
{
cell.liveView.isHidden = false
}
cell.sportIcon.image = UIImage(named: "soccerball")
cell.teamNameLabel.text = obj.name ?? ""
cell.btnSelection2.tag = section
cell.btnSelection2.addTarget(self, action: #selector(AllLiveMatchesViewController.btnSectionClick2(sender:)), for: .touchUpInside)
if section == selectedIndx && thereIsCellTapped{
cell.headerView.roundCorners(corners: [.topLeft,.topRight], radius: 10)
}
else
{
cell.headerView.roundCorners(corners: [.topLeft,.topRight,.bottomLeft,.bottomRight], radius: 10)
}
return cell
}
else if tableView == tennisTableView
{
let cell = tableView.dequeueReusableCell(withIdentifier: String(describing: TennisExpandTableViewCell.self)) as! TennisExpandTableViewCell
let obj = tennisMatchArray[section]
if obj.inplay == false && obj.status == "CLOSE"
{
cell.liveView.isHidden = true
}
else if obj.inplay == true && obj.status == "OPEN"
{
cell.liveView.isHidden = false
}
cell.sportIcon.image = UIImage(named: "tennisracket")
cell.teamNameLabel.text = obj.name ?? ""
cell.btnSelection3.tag = section
cell.btnSelection3.addTarget(self, action: #selector(AllLiveMatchesViewController.btnSectionClick3(sender:)), for: .touchUpInside)
if section == selectedIndx && thereIsCellTapped{
cell.headerView.roundCorners(corners: [.topLeft,.topRight], radius: 10)
}
else
{
cell.headerView.roundCorners(corners: [.topLeft,.topRight,.bottomLeft,.bottomRight], radius: 10)
}
return cell
}
else
{
let cell = tableView.dequeueReusableCell(withIdentifier: String(describing: ExpandTableViewCell.self)) as! ExpandTableViewCell
return cell
}
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: String(describing: ExpandInsideTableViewCell.self)) as! ExpandInsideTableViewCell
if tableView == cricketTableView
{
let ob = cricketMatchArray[indexPath.section]
let obj = cricketMatchArray[indexPath.section].score[indexPath.row]
if obj.spnnation1 == nil
{
let teamName = ob.name?.components(separatedBy: " v ")
let fTeamWords = teamName?[0].split { !$0.isLetter }
let sTeamWords = teamName?[1].split { !$0.isLetter }
if fTeamWords?.count == 1
{
let fTeam = teamName?[0].prefix(3)
cell.firstTeamName.text = fTeam?.description.uppercased()
}
else
{
let fTeam = teamName?[0].getAcronyms()
cell.firstTeamName.text = fTeam
}
if sTeamWords?.count == 1
{
let fTeam = teamName?[1].prefix(3)
cell.secondTeamName.text = fTeam?.description.uppercased()
}
else
{
let fTeam = teamName?[1].getAcronyms()
cell.secondTeamName.text = fTeam
}
cell.firstTeamScore.text = obj.score1
cell.secondTeamScore.text = obj.score2
cell.dateLabel.text = ob.openDate
cell.commonScore.isHidden = true
}
else
{
cell.firstTeamName.text = obj.spnnation1
cell.secondTeamName.text = obj.spnnation2
cell.firstTeamScore.text = obj.score1
cell.secondTeamScore.text = obj.score2
cell.dateLabel.text = ob.openDate
cell.commonScore.isHidden = true
}
}
else if tableView == soccerTableView
{
let ob = soccerMatchArray[indexPath.section]
let obj = soccerMatchArray[indexPath.section].score[indexPath.row]
if obj.spnnation1 == nil
{
let teamName = ob.name?.components(separatedBy: " v ")
let fTeamWords = teamName?[0].split { !$0.isLetter }
let sTeamWords = teamName?[1].split { !$0.isLetter }
if fTeamWords?.count == 1
{
let fTeam = teamName?[0].prefix(3)
cell.firstTeamScore.text = fTeam?.description.uppercased()
}
else
{
let fTeam = teamName?[0].getAcronyms()
cell.firstTeamScore.text = fTeam
}
if sTeamWords?.count == 1
{
let fTeam = teamName?[1].prefix(3)
cell.secondTeamScore.text = fTeam?.description.uppercased()
}
else
{
let fTeam = teamName?[1].getAcronyms()
cell.secondTeamScore.text = fTeam
}
cell.firstTeamName.isHidden = true
cell.secondTeamName.isHidden = true
cell.dateLabel.text = ob.openDate
cell.commonScore.isHidden = false
cell.commonScore.text = "\(obj.score1 ?? "")-\(obj.score2 ?? "")"
}
else
{
cell.firstTeamName.isHidden = true
cell.secondTeamName.isHidden = true
cell.firstTeamScore.text = obj.spnnation1?.getAcronyms()
cell.secondTeamScore.text = obj.spnnation2?.getAcronyms()
cell.dateLabel.text = ob.openDate
cell.commonScore.isHidden = false
cell.commonScore.text = "\(obj.score1 ?? "")-\(obj.score2 ?? "")"
}
}
else if tableView == tennisTableView
{
let ob = tennisMatchArray[indexPath.section]
let obj = tennisMatchArray[indexPath.section].score[indexPath.row]
if obj.spnnation1 == nil
{
let teamName = ob.name?.components(separatedBy: " v ")
let fTeamWords = teamName?[0].split { !$0.isLetter }
let sTeamWords = teamName?[1].split { !$0.isLetter }
if fTeamWords?.count == 1
{
let fTeam = teamName?[0].prefix(3)
cell.firstTeamScore.text = fTeam?.description.uppercased()
}
else
{
let fTeam = teamName?[0].getAcronyms()
cell.firstTeamScore.text = fTeam
}
if sTeamWords?.count == 1
{
let fTeam = teamName?[1].prefix(3)
cell.secondTeamScore.text = fTeam?.description.uppercased()
}
else
{
let fTeam = teamName?[1].getAcronyms()
cell.secondTeamScore.text = fTeam
}
cell.firstTeamName.isHidden = true
cell.secondTeamName.isHidden = true
cell.dateLabel.text = ob.openDate
cell.commonScore.isHidden = false
cell.commonScore.text = "\(obj.score1 ?? "")-\(obj.score2 ?? "")"
}
else
{
cell.firstTeamName.isHidden = true
cell.secondTeamName.isHidden = true
cell.firstTeamScore.text = obj.spnnation1?.getAcronyms()
cell.secondTeamScore.text = obj.spnnation2?.getAcronyms()
cell.dateLabel.text = ob.openDate
cell.commonScore.isHidden = false
cell.commonScore.text = "\(obj.score1 ?? "")-\(obj.score2 ?? "")"
}
}
return cell
}
有人能帮我一下吗。
3条答案
按热度按时间djp7away1#
问题是您复制了代码,但没有复制属性:
您在以下方面遇到此问题:
9gm1akwq2#
作为一个好奇的问题-为什么你有3个单独的表视图,而不是一个与部分?
在这种情况下,不需要在一个ViewController中具有3个不同的UITableView。
通过控制截面中的单元格数量,我们可以简化视图的逻辑。
点击标题应控制其他节的“隐藏”逻辑(=将给定节中的行数设置为零)。
camsedfj3#
您可以将Single TableView与包含StackView的单元格一起使用,方法是将其设置为Top、Leading、Trailing和Bottom。节中的行数应为3。这意味着现在有三个单元格填充每个单元格中的数组。
heightForRowAt
应为UITableView.automaticDimension