Ios 如果表视图中有数据但没有数据,如何调整表视图的大小
我有一个标题为“当前团队”的屏幕,下面是一个表视图,下面是一个带有+按钮的文本字段。所以,当用户在UITextField中输入任何数据时,如果用户按下+按钮,该数据将添加到UITableView上方 此吹扫图像是我的当前屏幕: 现在,我在上面的屏幕上设计并添加了一些约束条件。现在,当表视图中没有数据且至少有一个数据时,标题和文本字段之间会有更多的空间 但我需要的是: 我需要显示如下图所示: 我不需要在标题和文本字段之间显示空格。当表视图中至少有一个数据时,表视图的高度应该增加,文本字段也应该位于下面 我如何处理这个问题: 我喜欢这样:Ios 如果表视图中有数据但没有数据,如何调整表视图的大小,ios,swift,uitableview,Ios,Swift,Uitableview,我有一个标题为“当前团队”的屏幕,下面是一个表视图,下面是一个带有+按钮的文本字段。所以,当用户在UITextField中输入任何数据时,如果用户按下+按钮,该数据将添加到UITableView上方 此吹扫图像是我的当前屏幕: 现在,我在上面的屏幕上设计并添加了一些约束条件。现在,当表视图中没有数据且至少有一个数据时,标题和文本字段之间会有更多的空间 但我需要的是: 我需要显示如下图所示: 我不需要在标题和文本字段之间显示空格。当表视图中至少有一个数据时,表视图的高度应该增加,文本字段也应
if currentTeams.visibleCells.count == 0 {
tableViewHeight.constant = 5
}
else{
tableViewHeight.constant = 50
}
当前团队=表视图名称
但这不管用,请帮帮我。我该怎么做
代码剪报:
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
if tableView.isEqual(currentTeams) {
return teamNames.count
}
return pastTeamNames.count
}
func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
return 20
}
func numberOfSectionsInTableView(tableView: UITableView) -> Int {
return 1
}
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
if tableView.isEqual(currentTeams) {
return getCellForRow(indexPath)
}
return getCellForPastTeamsRow(indexPath)
}
您可以通过在表的和处选择一个修复UITableviewCell来实现同样的效果。如果最初没有数据,则在
行数部分方法返回1
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return (<Your Array>.count > 1) ? <Your Array>.count + 1 : 1;
}
您的设计将类似于
您也可以查看示例。您可以通过在表的和处使用一个修复UITableviewCell来实现相同的效果。如果最初没有数据,则在行数部分方法返回1
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return (<Your Array>.count > 1) ? <Your Array>.count + 1 : 1;
}
您的设计将类似于
您还可以查看示例。通过将tableView行高设置为UITableViewAutomaticDimension,可以获得动态单元格高度。此外,您还需要提供估计高度。因此,在viewdidload中添加以下代码:
tableView.rowHeight = UITableViewAutomaticDimension
tableView.estimatedRowHeight = 140 // or your estimated height
然后在tableview单元格中使用autolayout来调整项目和容器高度通过将tableview行高度设置为UITableViewAutomaticDimension,可以获得动态单元格高度。此外,您还需要提供估计高度。因此,在viewdidload中添加以下代码:
tableView.rowHeight = UITableViewAutomaticDimension
tableView.estimatedRowHeight = 140 // or your estimated height
然后在tableview单元格中使用autolayout来调整项目和容器高度-
- 首先,您应该为“当前团队”设置一个
UILabel
,并对superView设置前导、尾随和顶部约束
- 在其下方添加
UITableView
,并在UILabel
中添加一个前导、尾随和顶部,以及一个优先级为250的固定高度,让我们先将其设置为0
- 将带有前导、尾随、固定高度的
UITextField
添加到UITableView
中,并将底部间距添加到带有=0的superView中
这将允许UITextField
始终粘贴到UITableView
的底部
为UIViewController
中的UITableView
高度约束设置一个出口。
现在,如果您可以使用默认的UITableViewCell
height,那么您的工作就更容易了,否则您将不得不计算每个字符串在单元格内的高度并获得该值。在viewdiload
中,将UITableView
高度约束更新为-
//do this after you do the insertion into the tableview
tableViewHeightConstraint.constant = numberOfRows * rowHeight
view.layoutIfNeeded()
每次在UITableView
中添加或删除行时,都必须更新此高度。一旦您的单元格占据整个屏幕,高度限制将被打破,UITextField
将卡在底部,您的UITableView
将可滚动
编辑:一旦高度约束中断,您必须以编程方式重新添加高度约束,并且UITableView
停止占据整个屏幕。这样设置约束-
// your table hight constrain.
@IBOutlet var current_team_tablehight: NSLayoutConstraint!
@IBOutlet var playpast_role_table_hight: NSLayoutConstraint!
@IBOutlet var past_team_table_hight: NSLayoutConstraint!
@IBOutlet var certificate_table_hight: NSLayoutConstraint!
override func viewDidLoad() {
super.viewDidLoad()
// put it as 0 for default
current_team_tablehight.constant = 0
certificate_table_hight.constant = 0
past_team_table_hight.constant = 0
playpast_role_table_hight.constant = 0
}
override func viewDidAppear(animated: Bool) {
super.viewDidAppear(true)
if profileData.FirstName.length > 0 {
// data available for that table then .
currentTeams.reloadData()
current_team_tablehight.constant = currentTeams.contentSize.height + 5;
pastTeams.reloadData()
past_team_table_hight.constant = pastTeams.contentSize.height + 5;
playedTableView.reloadData()
playpast_role_table_hight.constant = playedTableView.contentSize.height + 5;
certificationtableview.reloadData()
certificate_table_hight.constant = certificationtableview.contentSize.height + 5;
}
}
func deleteTeamFromCurrentTeams(sender: UIButton) {
let but = sender
let view = but.superview!
let cell = view.superview as! CurrentTeamsTableViewCell
if let tblView = cell.superview?.superview as? UITableView {
if tblView.isEqual(self.currentTeams) {
let indexPath = currentTeams.indexPathForCell(cell)
teamNames.removeAtIndex((indexPath?.row)!)
currentTeams.reloadData()
if teamNames.count > 0 {
current_team_tablehight.constant = currentTeams.contentSize.height;
}else{
current_team_tablehight.constant = 0;
}
}
else if tblView.isEqual(self.pastTeams) {
let indexPath = pastTeams.indexPathForCell(cell)
pastTeamNames.removeAtIndex((indexPath?.row)!)
pastTeams.reloadData()
if pastTeamNames.count > 0 {
past_team_table_hight.constant = pastTeams.contentSize.height;
}else{
past_team_table_hight.constant = 0;
}
}
else if tblView.isEqual(self.playedTableView) {
let indexPath = playedTableView.indexPathForCell(cell)
playedTeamNames.removeAtIndex((indexPath?.row)!)
playedTableView.reloadData()
if playedTeamNames.count > 0 {
playpast_role_table_hight.constant = playedTableView.contentSize.height;
}else{
playpast_role_table_hight.constant = 0;
}
}
else
{
let indexPath = certificationtableview.indexPathForCell(cell)
ExpTeamNames.removeAtIndex((indexPath?.row)!)
certificationtableview.reloadData()
if ExpTeamNames.count > 0 {
certificate_table_hight.constant = certificationtableview.contentSize.height;
}else{
certificate_table_hight.constant = 0;
}
}
}
self.view .layoutIfNeeded()
self.view .setNeedsLayout()
}
@IBAction func addPastTeamsPressed(sender: AnyObject) {
if pastTeamName.text?.trimWhiteSpace != "" && pastTeamName.text?.trimWhiteSpace != "-" {
pastTeamNames.append(pastTeamName.textVal)
pastTeamName.text = ""
pastTeams.reloadData()
past_team_table_hight.constant = pastTeams.contentSize.height + 5;
}
}
@IBAction func addTeamsPressed(sender: AnyObject) {
if teamName.text?.trimWhiteSpace != "" && teamName.text?.trimWhiteSpace != "-" {
teamNames.append(teamName.textVal)
teamName.text = ""
currentTeams.reloadData()
current_team_tablehight.constant = currentTeams.contentSize.height + 5;
}
}
// for played role
@IBAction func addPlayedTeamsPressed(sender: AnyObject) {
if playedTeam.text?.trimWhiteSpace != "" && playedTeam.text?.trimWhiteSpace != "-" {
playedTeamNames.append(playedTeam.textVal)
playedTeam.text = ""
playedTableView.reloadData()
playpast_role_table_hight.constant = playedTableView.contentSize.height + 5;
}
}
@IBAction func addcertificatecoursePressed(sender: AnyObject) {
if Experience.text?.trimWhiteSpace != "" && Experience.text?.trimWhiteSpace != "-" {
ExpTeamNames.append(Experience.textVal)
Experience.text = ""
certificationtableview.reloadData()
certificate_table_hight.constant = certificationtableview.contentSize.height + 5;
}
}
- 首先,您应该为“当前团队”设置一个
UILabel
,并对superView设置前导、尾随和顶部约束
- 在其下方添加
UITableView
,并在UILabel
中添加一个前导、尾随和顶部,以及一个优先级为250的固定高度,让我们先将其设置为0
- 将带有前导、尾随、固定高度的
UITextField
添加到UITableView
中,并将底部间距添加到带有=0的superView中
这将允许UITextField
始终粘贴到UITableView
的底部
为UIViewController
中的UITableView
高度约束设置一个出口。
现在,如果您可以使用默认的UITableViewCell
height,那么您的工作就更容易了,否则您将不得不计算每个字符串在单元格内的高度并获得该值。在viewdiload
中,将UITableView
高度约束更新为-
//do this after you do the insertion into the tableview
tableViewHeightConstraint.constant = numberOfRows * rowHeight
view.layoutIfNeeded()
每次在UITableView
中添加或删除行时,都必须更新此高度。一旦您的单元格占据整个屏幕,高度限制将被打破,UITextField
将卡在底部,您的UITableView
将可滚动
编辑:一旦高度约束中断且UITableView
停止占据整个屏幕,您必须以编程方式重新添加高度约束
// your table hight constrain.
@IBOutlet var current_team_tablehight: NSLayoutConstraint!
@IBOutlet var playpast_role_table_hight: NSLayoutConstraint!
@IBOutlet var past_team_table_hight: NSLayoutConstraint!
@IBOutlet var certificate_table_hight: NSLayoutConstraint!
override func viewDidLoad() {
super.viewDidLoad()
// put it as 0 for default
current_team_tablehight.constant = 0
certificate_table_hight.constant = 0
past_team_table_hight.constant = 0
playpast_role_table_hight.constant = 0
}
override func viewDidAppear(animated: Bool) {
super.viewDidAppear(true)
if profileData.FirstName.length > 0 {
// data available for that table then .
currentTeams.reloadData()
current_team_tablehight.constant = currentTeams.contentSize.height + 5;
pastTeams.reloadData()
past_team_table_hight.constant = pastTeams.contentSize.height + 5;
playedTableView.reloadData()
playpast_role_table_hight.constant = playedTableView.contentSize.height + 5;
certificationtableview.reloadData()
certificate_table_hight.constant = certificationtableview.contentSize.height + 5;
}
}
func deleteTeamFromCurrentTeams(sender: UIButton) {
let but = sender
let view = but.superview!
let cell = view.superview as! CurrentTeamsTableViewCell
if let tblView = cell.superview?.superview as? UITableView {
if tblView.isEqual(self.currentTeams) {
let indexPath = currentTeams.indexPathForCell(cell)
teamNames.removeAtIndex((indexPath?.row)!)
currentTeams.reloadData()
if teamNames.count > 0 {
current_team_tablehight.constant = currentTeams.contentSize.height;
}else{
current_team_tablehight.constant = 0;
}
}
else if tblView.isEqual(self.pastTeams) {
let indexPath = pastTeams.indexPathForCell(cell)
pastTeamNames.removeAtIndex((indexPath?.row)!)
pastTeams.reloadData()
if pastTeamNames.count > 0 {
past_team_table_hight.constant = pastTeams.contentSize.height;
}else{
past_team_table_hight.constant = 0;
}
}
else if tblView.isEqual(self.playedTableView) {
let indexPath = playedTableView.indexPathForCell(cell)
playedTeamNames.removeAtIndex((indexPath?.row)!)
playedTableView.reloadData()
if playedTeamNames.count > 0 {
playpast_role_table_hight.constant = playedTableView.contentSize.height;
}else{
playpast_role_table_hight.constant = 0;
}
}
else
{
let indexPath = certificationtableview.indexPathForCell(cell)
ExpTeamNames.removeAtIndex((indexPath?.row)!)
certificationtableview.reloadData()
if ExpTeamNames.count > 0 {
certificate_table_hight.constant = certificationtableview.contentSize.height;
}else{
certificate_table_hight.constant = 0;
}
}
}
self.view .layoutIfNeeded()
self.view .setNeedsLayout()
}
@IBAction func addPastTeamsPressed(sender: AnyObject) {
if pastTeamName.text?.trimWhiteSpace != "" && pastTeamName.text?.trimWhiteSpace != "-" {
pastTeamNames.append(pastTeamName.textVal)
pastTeamName.text = ""
pastTeams.reloadData()
past_team_table_hight.constant = pastTeams.contentSize.height + 5;
}
}
@IBAction func addTeamsPressed(sender: AnyObject) {
if teamName.text?.trimWhiteSpace != "" && teamName.text?.trimWhiteSpace != "-" {
teamNames.append(teamName.textVal)
teamName.text = ""
currentTeams.reloadData()
current_team_tablehight.constant = currentTeams.contentSize.height + 5;
}
}
// for played role
@IBAction func addPlayedTeamsPressed(sender: AnyObject) {
if playedTeam.text?.trimWhiteSpace != "" && playedTeam.text?.trimWhiteSpace != "-" {
playedTeamNames.append(playedTeam.textVal)
playedTeam.text = ""
playedTableView.reloadData()
playpast_role_table_hight.constant = playedTableView.contentSize.height + 5;
}
}
@IBAction func addcertificatecoursePressed(sender: AnyObject) {
if Experience.text?.trimWhiteSpace != "" && Experience.text?.trimWhiteSpace != "-" {
ExpTeamNames.append(Experience.textVal)
Experience.text = ""
certificationtableview.reloadData()
certificate_table_hight.constant = certificationtableview.contentSize.height + 5;
}
}
输出:
输出:
你试过了吗,self.view.setNeedsLayout()我对处理ios还不熟悉,通过看一些教程,我设置了一些高度限制,我试过了。但是如果currentTeams.visibleCells.count==0{tableViewHeight.constant=5 self.view.setNeedsLayout()}或者{tableViewHeight.constant=50 self.view.setNeedsLayout()},我会尝试其他方法您可以将页脚视图
添加到表视图,并将文本字段视图
添加到页脚视图
。这样,您就不需要更改行高
或其他任何内容