UITableView完全不显示且GetCell方法未调用问题求助
问题:UITableView完全不显示,GetCell方法从未被调用
我的UITableView完全不显示,UITableViewSource里的GetCell方法从来没被触发。我试过调整frame,怀疑视图被移出屏幕,但不管怎么改都没用。我是按官方文档的步骤构建的表格,自认实现没问题,但肯定漏了什么,查资料也没找到线索。
相关代码如下:
ViewDidLoad代码
public override void ViewDidLoad() { base.ViewDidLoad(); ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12; var height = this.NavigationController.NavigationBar.Bounds.Height; var width = this.NavigationController.NavigationBar.Bounds.Width; UIImageView banner = new UIImageView(); banner.Image = UIImage.FromResource(null, "MYTEST_APP.Resources.drawable.Banner2.jpg"); banner.Frame = new RectangleF(0, 0, (float)width, 73); banner.ContentMode = UIViewContentMode.ScaleAspectFit; UILabel lblTitle = new UILabel(); lblTitle.Text = theSelectedTeam + " " + "Chat Room"; UITableView tblMessages = new UITableView(); tblMessages.Frame = new RectangleF(0f, 0f, (float)width, 400f); tblMessages.Source = new MessageSource(GetMessages()); // 返回包含9条消息的List<string>,确认有数据 UITextField txtSendingTextMessage = new UITextField(new CGRect(0, width, width, 100)); txtSendingTextMessage.Placeholder = "Enter Message To Send Here"; UIButton btnSendText = new UIButton(); CGRect ScreenBounds = UIScreen.MainScreen.Bounds; float buttonWidth = (float)ScreenBounds.Width / 4; btnSendText.Frame = new RectangleF(0f, 0f, buttonWidth, 50f); btnSendText.SetTitleColor(UIColor.Black, UIControlState.Normal); btnSendText.SetTitle("Send Text", UIControlState.Normal); btnSendText.BackgroundColor = UIKit.UIColor.FromRGB(255, 215, 0); View.Add(banner); View.Add(lblTitle); View.Add(tblMessages); View.Add(txtSendingTextMessage); View.Add(btnSendText); View.SubviewsDoNotTranslateAutoresizingMaskIntoConstraints(); View.AddConstraints( banner.AtTopOf(View, 100), banner.AtRightOf(View, 0), banner.AtLeftOf(View, 0), lblTitle.Below(banner, 5), lblTitle.WithSameCenterX(banner), tblMessages.Below(lblTitle, 10), tblMessages.WithSameCenterX(lblTitle), txtSendingTextMessage.Below(tblMessages, 10), txtSendingTextMessage.WithSameCenterX(tblMessages), btnSendText.Below(txtSendingTextMessage, 10), btnSendText.WithSameCenterX(txtSendingTextMessage) ); tblMessages.ReloadData(); // 尝试重新加载数据 }
MessageSource代码
public class MessageSource : UITableViewSource { List<string> TableItems; string CellIdentifier = "TableCell"; public MessageSource(List<string> items) { TableItems = items; } public override nint RowsInSection(UITableView tableview, nint section) { return TableItems.Count; } public override UITableViewCell GetCell(UITableView tableView, NSIndexPath indexPath) { UITableViewCell cell = tableView.DequeueReusableCell(CellIdentifier); string item = TableItems[indexPath.Row]; // 没有可复用的单元格时创建新的 if (cell == null) { cell = new UITableViewCell(UITableViewCellStyle.Default, CellIdentifier); } cell.TextLabel.Text = item; return cell; } }
解决方法
问题出在自动布局约束不完整:你给UITableView只设置了垂直位置(在lblTitle下方)和水平居中,但没有定义它的宽度和高度。因为启用了Autolayout(调用了View.SubviewsDoNotTranslateAutoresizingMaskIntoConstraints()),手动设置的Frame会被约束覆盖,最终UITableView的尺寸为0,自然不会显示,也不会触发GetCell方法。
需要给tblMessages补充以下约束:
- 宽度约束:让tableview与父视图左右对齐,或设置固定宽度
- 高度约束:设置固定高度,或与父视图底部建立约束实现自适应
修改后的约束代码如下:
View.AddConstraints( banner.AtTopOf(View, 100), banner.AtRightOf(View, 0), banner.AtLeftOf(View, 0), lblTitle.Below(banner, 5), lblTitle.WithSameCenterX(banner), tblMessages.Below(lblTitle, 10), tblMessages.WithSameCenterX(lblTitle), tblMessages.AtLeftOf(View, 0), tblMessages.AtRightOf(View, 0), tblMessages.Height().EqualTo(400), txtSendingTextMessage.Below(tblMessages, 10), txtSendingTextMessage.AtLeftOf(View, 0), txtSendingTextMessage.AtRightOf(View, 0), txtSendingTextMessage.Height().EqualTo(100), btnSendText.Below(txtSendingTextMessage, 10), btnSendText.WithSameCenterX(txtSendingTextMessage), btnSendText.Width().EqualTo(buttonWidth), btnSendText.Height().EqualTo(50) );
另外注意:txtSendingTextMessage的初始化Frame有问题(new CGRect(0, width, width, 100)将Y轴设为导航栏宽度,导致位置异常),不过启用Autolayout后该Frame会被约束覆盖,上面的修改已给它添加了正确的约束。
最后确认GetMessages()确实返回非空列表(你提到有9条数据,这部分无问题),调整约束后tableview就能正常显示,GetCell方法也会被触发。
内容的提问来源于stack exchange,提问作者Prescott Chartier
相关产品推荐
相关产品推荐

