英文:
UITableView doesn't display at all
问题
我看不出为什么我的TableView不显示,但它确实没有。UITableViewSource中的GetCell
方法从未被调用,所以显然这是问题的一部分,但是我无法弄清楚为什么。我认为可能是因为视图被设置在屏幕之外,但无论我如何调整框架,都无法让任何东西显示出来。我在构建表视图时遵循了这里的帖子,所以我相信我做对了,但显然我漏掉了什么,我在网上阅读的所有内容都没有解决问题。
我的代码:
public override void ViewDidLoad()
{
//...(你的其余代码)
}
public class MessageSource : UITableViewSource
{
//...(你的TableViewSource代码)
}
英文:
I see no reason why my TableView won't display, but it doesn't. The GetCell
method in the UITableViewSource never gets called, so obviously that is part of the problem, but for the life of me I cannot figure out why. I thought it may be because the view is set off the screen, but no matter what I do with the frame. nothing ever appears. I followed the post here when constructing the table view, so I believe I got it right, but obviously I'm missing something and nothing I've read online has shed any light on the issue. My code:
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()); // Returns a List<string> of previous messages, currently there are 9 message strings that are retreived, so there is data
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(); // just is case
}
The TableViewSource Code:
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 there are no cells to reuse, create a new one
if (cell == null)
{
cell = new UITableViewCell(UITableViewCellStyle.Default, CellIdentifier);
}
cell.TextLabel.Text = item;
return cell;
}
}
答案1
得分: 0
以下是您提供的代码的翻译部分:
似乎当使用 Cirrious.FluentLayouts
时,执行 View.SubviewsDoNotTranslateAutoresizingMaskIntoConstraints();
后,表视图的大小被设置为零,因此它不可见。我简化了我的代码如下:
public override async void ViewDidLoad()
{
base.ViewDidLoad();
ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12;
var height = UIScreen.MainScreen.Bounds.Height;
var width = UIScreen.MainScreen.Bounds.Width;
UIImageView banner = new UIImageView();
banner.Image = UIImage.FromResource(null, "MY_TESTAPP.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";
MyMessages = GetMessages();
tblMessages.LayoutIfNeeded();
source.Data = MyMessages;
tblMessages.Source = source;
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, 50),
banner.AtRightOf(View, 0),
banner.AtLeftOf(View, 0),
lblTitle.Below(banner, 5),
lblTitle.WithSameCenterX(banner),
tblMessages.Below(lblTitle, 10),
tblMessages.AtLeftOf(View),
tblMessages.WithSameWidth(View),
tblMessages.Height().EqualTo(height/2),
txtSendingTextMessage.Below(tblMessages, 10),
txtSendingTextMessage.WithSameCenterX(tblMessages),
btnSendText.Below(txtSendingTextMessage, 10),
btnSendText.WithSameCenterX(txtSendingTextMessage)
);
}
请注意,我已经翻译了您提供的代码,如果您有任何其他问题或需要进一步的帮助,请随时告诉我。
英文:
I seems that when one uses Cirrious.FluentLayouts
like I do and then executes View.SubviewsDoNotTranslateAutoresizingMaskIntoConstraints();
the tableview gets sized to zero, consequently its not visible. I simplified my code as such:
public override async void ViewDidLoad()
{
base.ViewDidLoad();
ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12;
var height = UIScreen.MainScreen.Bounds.Height;
var width = UIScreen.MainScreen.Bounds.Width;
UIImageView banner = new UIImageView();
banner.Image = UIImage.FromResource(null, "MY_TESTAPP.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";
MyMessages = GetMessages();
tblMessages.LayoutIfNeeded();
source.Data = MyMessages;
tblMessages.Source = source;
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, 50),
banner.AtRightOf(View, 0),
banner.AtLeftOf(View, 0),
lblTitle.Below(banner, 5),
lblTitle.WithSameCenterX(banner),
tblMessages.Below(lblTitle, 10),
tblMessages.AtLeftOf(View),
tblMessages.WithSameWidth(View),
tblMessages.Height().EqualTo(height/2),
txtSendingTextMessage.Below(tblMessages, 10),
txtSendingTextMessage.WithSameCenterX(tblMessages),
btnSendText.Below(txtSendingTextMessage, 10),
btnSendText.WithSameCenterX(txtSendingTextMessage)
);
}
The line of code that made all the difference is tblMessages.Height().EqualTo(height/2)
. And of course there's no documentation anywhere that lets one know, not even in the Cirrious.FluentLayouts
documentation. Sigh, so frustrating. In any event, the post HERE set me on the correct path.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论