Как поместить таблицу в фоновом режиме через UIViewController? - PullRequest
0 голосов
/ 13 октября 2019

У меня есть таблица в UIView. Весь этот контент создается в UIViewController. Например, у меня есть кнопки с метками и т. Д. В этом UIView, и проблема в том, что, когда я выбираю строку, я хотел, чтобы этот UIView находился в фоновом режиме. Проблема в том, что когда я использую override void RowSelected(UITableView tableView, NSIndexPath indexPath), только таблица остается в фоновом режиме, и это имеет смысл, потому что переопределение касается всей таблицы. Как я могу сделать это возможным? Я не могу придумать другое решение. Я использую xamarin native language and I don't use storyboard neither forms

1 Ответ

0 голосов
/ 14 октября 2019

Я пишу простой пример для вас. Я добавляю два labels и один tableview к backView enter code here. Затем представьте новый вид в методе RowSelected.

Как я уже упоминал в комментарии, вы должны add the presentView to the backView instead of tablview. При создании tableVieSoure вы можете передать BackView и затем использовать его в RowSelected: middleTable.Source = new TableSource(tableItems,backView);

public partial class ViewController : UIViewController
{
    public ViewController (IntPtr handle) : base (handle)
    {
    }

    public override void ViewDidLoad ()
    {
        base.ViewDidLoad ();
        // Perform any additional setup after loading the view, typically from a nib.


        UIView backView = new UIView();
        backView.BackgroundColor = UIColor.Orange;
        backView.Frame = new CoreGraphics.CGRect(0,0,300,500);
        View.Add(backView);

        UILabel topLabel = new UILabel(new CoreGraphics.CGRect(0,10,200,50));
        topLabel.BackgroundColor = UIColor.Green;
        topLabel.Text = "I am top label";
        backView.Add(topLabel);

        UITableView middleTable = new UITableView(new CoreGraphics.CGRect(0, 60, 200, 300)); 
        string[] tableItems = new string[] { "Vegetables", "Fruits", "Flower Buds", "Legumes", "Bulbs", "Tubers" };
        //you can pass the backView when create the tableView
        middleTable.Source = new TableSource(tableItems,backView);
        backView.Add(middleTable);

        UILabel bottomLabel = new UILabel(new CoreGraphics.CGRect(0, 360, 200, 50));
        bottomLabel.BackgroundColor = UIColor.SystemPinkColor;
        bottomLabel.Text = "I am bottom Label";
        backView.Add(bottomLabel);
    }

}

public class TableSource : UITableViewSource
{

    UIView mybackView;

    string[] TableItems;
    string CellIdentifier = "TableCell";

    public TableSource(string[] items, UIView backView)
    {
        TableItems = items;
        mybackView = backView;
    }

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

    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;
    }

    public override void RowSelected(UITableView tableView, NSIndexPath indexPath)
    {

        UIView presentView = new UIView();

        presentView.BackgroundColor = UIColor.Blue;

        presentView.Frame = new CoreGraphics.CGRect(40,80,250,200);

        //add the presentView to the backView instead of tablview
        mybackView.Add(presentView);
    }
}

Вот снимок экрана:

image

Не стесняйтесь спрашивать меня, если у вас есть какие-либо вопросы.

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...