Load. XIB in a class - c#

How do I load a. XIB by clicking on the row of table view?
This example below is part of a class, as I fario alert the loading of this class?
public override void RowSelected (UITableView tableView, NSIndexPath indexPath)
{
tableView.DeselectRow (indexPath, true);
//this.PresentModalViewController(home,true);
}

There's not many details in your question and comments...
If you override the RowSelected method then you're likely inheriting a new type from UITableViewSource right ? If so then you should make its constructor keep a reference to the UITableViewController and use this reference to call PresentModalViewController later.
E.g.
public class MyTableViewSource : UITableViewSource {
UITableViewController ctrl;
public MyTableViewSource (UITableViewController c)
{
ctrl = c;
}
public override void RowSelected (UITableView tableView, NSIndexPath indexPath)
{
tableView.DeselectRow (indexPath, true);
var home = new UIViewController ("YouNibName", null); // default bundle
ctrl.PresentModalViewController (home, true);
}
}
note: I used UIViewController but you likely already have a type defined for your NIB.

Related

Selector in UITableViewCell in Xamarin

I need the UIMenuItem to call a method inside the UITableViewCell. Something similar is possible in swift as explained here: https://stackoverflow.com/a/31358474/202179 .
At the moment I am only able to call the method inside the UIViewController from the UIMenuItem if I add an Export attribute to the method.
There is some trick in the export of this method in the cell, but I have failed to find it till now. I would expect this code to work, but it doesn't:
[Register("CustomCell")]
public class CustomCell : UITableViewCell
{
public CustomCell(IntPtr intPtr) : base(intPtr)
{
var menuItem = new UIMenuItem("Menu Item", new ObjCRuntime.Selector("CustomCell.SeekSelectedText"));
UIMenuController.SharedMenuController.MenuItems = new UIMenuItem[] { menuItem };
UIMenuController.SharedMenuController.Update();
}
[Export("SeekSelectedText")]
void SeekSelectedText()
{
//do something
}
}
As said just quoting a different selector inside the UIViewController works, so the problem is that this selector doesn't appear to be recognized whatever I try.
Here is my test and the custom UIMenuItem works fine.
First, define a custom cell class which inherits UITableViewCell
class MyCell: UITableViewCell
{
public MyCell(UITableViewCellStyle style, string reuseIdentifier):base(style, reuseIdentifier)
{
}
[Export("custom")]
void Custom()
{
//do something
Console.WriteLine("custom pop up");
}
public override bool CanPerform(Selector action, NSObject withSender)
{
if (action == new Selector("custom"))
return true;
else
return false;
}
}
Second, set the source of your UITableView
class MyTableViewSource : UITableViewSource
{
int i = 0;
// create custom cell
public override UITableViewCell GetCell(UITableView tableView, NSIndexPath indexPath)
{
string cellStr = "cell";
MyCell cell = (MyCell)tableView.DequeueReusableCell(cellStr);
if (cell == null)
{
cell = new MyCell(UITableViewCellStyle.Subtitle, cellStr);
cell.TextLabel.Text = $"User Name {i}";
cell.DetailTextLabel.Text = $"details here... {i++}";
}
return cell;
}
public override nint RowsInSection(UITableView tableview, nint section)
{
return 10;
}
}
Then override the realted methods(e.g., ShouldShowMenu, CanPerformAction).
class MyTableViewDelegate : UITableViewDelegate
{
public override bool ShouldShowMenu(UITableView tableView, NSIndexPath rowAtindexPath)
{
return true;
}
public override bool CanPerformAction(UITableView tableView, Selector action, NSIndexPath indexPath, NSObject sender)
{
return true;
}
public override void PerformAction(UITableView tableView, Selector action, NSIndexPath indexPath, NSObject sender)
{
}
public override bool ShouldHighlightRow(UITableView tableView, NSIndexPath rowIndexPath)
{
return true;
}
}
Last, don't foget to add custom MenuItem to your UITableViewCell.
UIMenuController.SharedMenuController.MenuItems = new UIMenuItem[] {
new UIMenuItem ("Custom", new Selector ("custom"))
};
Since UICollectionView has similar functions to UITableVIew, you can directly refer to the sample SimpleCollectionView.

UITableviewCell GetCell is not being called

I am using Visual Studio Community 2017 for Mac. I have a UITableview named SchoolSchedule. I am setting the Source from a button, but then when I click the button GetCell is not called, however, RowsInSection is called.
Here is my class:
public class ScheduleTableViewSource : UITableViewSource
{
private Schedule[] school;
public ScheduleTableViewSource(Schedule[] school)
{
this.school = school;
}
public override UITableViewCell GetCell(UITableView tableView, NSIndexPath indexPath)
{
var cell = (ScheduleCell) tableView.DequeueReusableCell("cell_id", indexPath);
var school1 = school[indexPath.Row];
cell.Updatecell(school1);
cell.TextLabel.Text = school[indexPath.Row].Site;
return cell;
}
public override nint RowsInSection(UITableView tableview, nint section)
{
return school.Length;
}
}
here is my button code :
partial void Get_button_TouchUpInside(UIButton sender)
{
bool check = NetworkInterface.GetIsNetworkAvailable();
if(check)
{
Service1 client = new Service1();
var school = client.CypressRidge("CypressRidge");
SchoolSchedule.Source = new ScheduleTableViewSource(school);
SchoolSchedule.ReloadData();
}
else{
Console.Write("Error no Internet connection");
return;
}
// int stop =0;
}
I have checked and there is data coming in from my service. so that is not the problem. can anyone help?
I had to deleted the table and make a new one.
there a bug in Xamarin.
I had the same problem and I managed to fix it setting up Identifier property of TableView first prototype row - in storyboard
In addition to implementing the IUITableViewDelegate and IUITableViewDataSource interfaces in the .cs file, calling RegisterNibForCellReuse, and assigning the table view's EstimatedRowHeight, it's easy to forget to set the data source and delegate properties of the table view in the XIB.

In xamarin iOS - get [indexPath.Row] in UITableViewCell, Using button click event

In Xamarin iOS - I have a class named TableCell ( public class TableCell : UITableViewCell { } ) - in this class I have declared buttons.
In button click event , i'm trying to access [indexPath.Row] in UITableViewCell. I can select a row to find [indexPath.Row] in UITableViewSource.
public class TableSource : UITableViewSource {
// - - - -
public override void RowSelected (UITableView tableView, NSIndexPath indexPath)
{
new UIAlertView("Row Selected", tableItems[indexPath.Row], null, "OK", null).Show();
tableView.DeselectRow (indexPath, true); // iOS convention is to remove the highlight
}
// - - - -
}
How can I get [indexPath.Row] in UITableViewCell, Using button click event ..?
It's not the best approach but it will definitely work:
I would extend TableCell class with a new property:
public NSIndexPath IndexPath { get; set; }
Then I would add this line to the UITableViewSource's GetCell method if you are using the cell reusing pattern:
public override UITableViewCell GetCell(UITableView tableView, NSIndexPath indexPath)
{
TableCell cell = tableView.DequeueReusableCell("YourIdentifier") ?? new TableCell();
cell.IndexPath = indexPath;
return cell;
}
And after that you could access the cell's current IndexPath anywhere. i.e.:
public TableCell()
{
UIButton btn = new UIButton(UIButtonType.RoundRect);
btn.TouchUpInside += (sender, args)
{
new UIAlertView("Button pressed!", indexPath.Row, null, "OK", null).Show();
}
}
Use VisibleCells property.
As soon as user selected row, the UITableViewCell will be visible.
Alternative solution (if you want to get similar, but not the same cell):
You can get it via UITableViewCell.GetCell using passed UITableView tableView, then cast it into your type of UITableViewCell, then call it's method using passed NSIndexPath indexPath.
Something like this:
public class TableSource : UITableViewSource {
// - - - -
public override void RowSelected (UITableView tableView, NSIndexPath indexPath)
{
var cell = tableView.GetCell(tableView, indexPath);
var myCell class = cell as MyCellClass;
if (myCell != null) {
// TODO: do something with cell.
}
}
// - - - -
}
// - - - -
}

iOS Row Selected not being called

Ive added a print statement in my row selected method and it is never being called:
public override void RowSelected (UITableView tableView, NSIndexPath indexPath)
{
Console.WriteLine ("Row selected");
}
My cells have a UIImageView that covers the whole cell acting as a background image. Could this be causing the row to not recognize the touch?
Had to call:
MyTableView.AllowsSelection = true;
It must have been set to false by default.
Adding latest: Visual Studio/Xamarin iOS/C#
1) Add IUITableViewDelegate to the ViewController.cs
2) On ViewDidLoad:
public override void ViewDidLoad()
{
base.ViewDidLoad();
this.yourTableview.Delegate = this;
this.yourTableview.AllowsSelection = true;
}
3) Implement IUITableViewDelegate the method.
[Export("tableView:didSelectRowAtIndexPath:")]
public virtual void RowSelected(UITableView tableView, NSIndexPath indexPath)
{
Console.WriteLine("Row Selected");
}
Please note that RowSelected will not be called if [Export("tableView:didSelectRowAtIndexPath:")] is not added.

Unable to load ViewController from tableview RowSelected in xamarin

On selected tableview rowselected loading a DetailViewcontroller class.
public override void RowSelected (UITableView tableView, NSIndexPath indexPath)
{
if (OnRowSelected != null) {
OnRowSelected (this, new RowSelectedEventArgs (tableView, indexPath));
}
var detailController = new DetailViewController ();
UINavigationController controller = new UINavigationController();
controller.PushViewController(detailController, true);
}
Unable to load the DetailViewController.
public override void RowSelected (UITableView tableView, NSIndexPath indexPath)
method is in UITableViewSource
Is there any way to load DetailViewController on rowSelected.
When you create your table, you need to place it within the context of a Navigation controller.
UINavigationController nav = new UINavigationController(myTableViewController);
Then, when you want to display your DetailViewController, you can push it onto the already existing Navigation controller. However, by default your TableSource does not have access to NavigationController - you will need to pass a reference to your TableView controller when you create the TableSource so that the TableSource can access the NavigationController:
// in your controller, when you assign the source
this.TableView.Source = new MyTableViewSource(this);
// in your source, keep a class level ref to the parent
MyTableViewController _parent;
// your Source's constructor
public MyTableViewSource(MyTableViewController parent) {
_parent = parent;
}
// finally, in your RowSelected use the _parent reference to access the Nav controller
var detailController = new DetailViewController ();
_parent.NavigationController.PushViewController(detailController, true);

Categories

Resources