簡體   English   中英

Xamarin iOS TableView未顯示

[英]Xamarin iOS TableView not showing up

我正在使用Xamarin制作適用於iOS的應用程序,我在使用UITableViews時遇到了一些麻煩,並找到了本教程: https : //docs.microsoft.com/zh-cn/xamarin/ios/user-interface/controls/tables/populating-a-數據表

我創建了一個新項目,並按照他們的工作進行了操作,但對我而言不起作用。 我一定做錯了,但我不知道該怎么辦。

有人可以看看為什么不顯示TableView嗎?

BasicViewController:

public class BasicTableViewController : UIViewController
{
    private UIView view;

    public override void ViewDidLoad()
    {
        base.ViewDidLoad();

        var table = new UITableView(View.Bounds);
        table.Source = new BasicTableSource("aaaaaaaaa", "bbbbb", "cccccccc", "ddddddddddddd", "eeeee");
        Add(table);
    }

    public override void LoadView()
    {
        view = new UIView()
        {
            BackgroundColor = UIColor.White,
        };

        View = view;
    }
}

BasicTableViewSource:

public class BasicTableSource : UITableViewSource
{
    private readonly string[] items;

    public BasicTableSource(params string[] items)
    {
        this.items = items;
    }

    public override UITableViewCell GetCell(UITableView tableView, NSIndexPath indexPath)
    {
        var cell = tableView.DequeueReusableCell("cell");
        if (cell == null)
            cell = new UITableViewCell(UITableViewCellStyle.Default, "cell");

        cell.TextLabel.Text = items[indexPath.Row];

        return cell;
    }

    public override nint RowsInSection(UITableView tableview, nint section)
    {
        return items.Length;
    }
}

AppDelegate:

[Register("AppDelegate")]
public class AppDelegate : UIApplicationDelegate
{
    public override UIWindow Window { get; set; }

    public override bool FinishedLaunching(UIApplication application, NSDictionary launchOptions)
    {
        Window = new UIWindow(UIScreen.MainScreen.Bounds);
        Window.RootViewController = new BasicTableViewController();

        Window.MakeKeyAndVisible();

        return true;
    }
}

運行該項目時模擬器的外觀: 模擬器的外觀

原因:

當您覆蓋LoadView方法時,似乎忘記了設置view Frame ,因此view的大小將以高度為0和寬度為0。

解:

將視野設置為屏幕尺寸。

public override void LoadView()
{
    view = new UIView()
    {
        Frame = UIScreen.MainScreen.Bounds,
        BackgroundColor = UIColor.White,
    };

    View = view;
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM