Working on a way to display HTML and RTF content in a custom Element in MonoTouch.Dialog. To get started, I created a new class based on the UIViewElement implementation, with the intention of displaying a UIWebView in a cell (see UIViewElement code here).
The custom element works, but the contents of the UIWebView is never displayed. Once the text of the UIWebView is loaded, I try to resize it using UIWebView.SizeThatFits. This function always return 0s, even though the width given to it (from the actual UITableViewCell it is displayed in) is non-zero.
Is it sufficient to add the UIWebView to the UITableViewCell using this code:
cell.ContentView.AddSubview (webView);
The UIWebView never returns a non-zero height, so the actual cell is never displayed. Even if I override the height calculation and return a static height (such as 100f), the UIWebView is still not displayed.
The element's complete code:
public class RBHTMLRow : Element, IElementSizing
{
public enum CellFlags {Transparent = 1,DisableSelection = 2}
public CellFlags Flags;
private UIWebView webView = null;
private UITableViewCell current_cell;
NSString key;
private void ResizeWebView ()
{
if (current_cell != null)
{
RectangleF frame = webView.Frame;
SizeF fittingSize = webView.SizeThatFits(new SizeF(current_cell.Frame.Width, 1f));
frame.Size = fittingSize;
webView.Frame = frame;
}
}
private void InitWebView(bool isRTF, string content )
{
webView = new UIWebView();
webView.LoadHtmlString ( content, new NSUrl(""));
webView.LoadFinished += delegate {
ResizeWebView();
} ;
}
public RBHTMLRow (bool isRTF, String content, String caption) : base (caption)
{
key = new NSString("rbhtml_row");
InitWebView(isRTF, content);
}
protected override NSString CellKey {
get {
return key;
}
}
public override UITableViewCell GetCell (UITableView tv)
{
var cell = tv.DequeueReusableCell (CellKey);
if (cell == null){
cell = new UITableViewCell (UITableViewCellStyle.Default, CellKey);
if ((Flags & CellFlags.Transparent) != 0){
cell.BackgroundColor = UIColor.Clear;
cell.BackgroundView = new UIView (RectangleF.Empty) {
BackgroundColor = UIColor.Clear
} ;
}
cell.SelectionStyle = UITableViewCellSelectionStyle.None;
cell.ContentView.AddSubview (webView);
}
current_cell = cell;
ResizeWebView();
return cell;
}
public float GetHeight (UITableView tableView, NSIndexPath indexPath){
return webView.Bounds.Height;
}
Related
I'm wondering how can I get the relative position of a cell in a collection view in regards to the app window? I saw some examples for swift and forms, but i can't find any for Xamarin.iOS
Translate the solution in this thread:
public class collectionViewDelegate : UICollectionViewDelegate {
UIView view;
public collectionViewDelegate(UIView v)
{
//get the parent v when create collectionViewDelegate instance
this.view = v;
}
public override void ItemSelected(UICollectionView collectionView, NSIndexPath indexPath)
{
UICollectionViewCell cell = (UICollectionViewCell)collectionView.DequeueReusableCell("CollectionViewCell",indexPath);
CGRect frame = new CGRect(0,0,cell.Frame.Size.Width,cell.Frame.Size.Height);
frame.Location = collectionView.ConvertPointToView(cell.Frame.Location, this.view);
}
}
In my app i simply trying to add a checkmark to the selected row. I have around 20 items in my row and want to show a checkmark for the selected row. When i scroll to the bottom of the page and select a row it is throwing NullReferenceException. I have checkmark code reference from here
using System;
using System.Drawing;
using MonoTouch.Foundation;
using MonoTouch.UIKit;
using MonoTouch.ObjCRuntime;
using System.Collections.Generic;
namespace SampleApp
{
public partial class FirstViewController : UITableViewController
{
DataSource dataSource;
public FirstViewController () : base ("FirstViewController", null)
{
Title = NSBundle.MainBundle.LocalizedString ("First", "First");
TabBarItem.Image = UIImage.FromBundle ("first");
}
public override void DidReceiveMemoryWarning ()
{
// Releases the view if it doesn't have a superview.
base.DidReceiveMemoryWarning ();
// Release any cached data, images, etc that aren't in use.
}
public override void ViewDidLoad ()
{
base.ViewDidLoad ();
// Perform any additional setup after loading the view, typically from a nib.
// Add Table
TableView.Source = dataSource = new DataSource (this);
}
public override bool ShouldAutorotateToInterfaceOrientation (UIInterfaceOrientation toInterfaceOrientation)
{
// Return true for supported orientations
return (toInterfaceOrientation != UIInterfaceOrientation.PortraitUpsideDown);
}
class DataSource : UITableViewSource
{
static readonly NSString CellIdentifier = new NSString ("DataSourceCell");
FirstViewController controller;
private List<String> _listData = new List<String> ();
private NSIndexPath _previousRow;
public DataSource (FirstViewController controller)
{
this.controller = controller;
// POPULATE DISPOSITION LIST
PopulateDatabase();
_previousRow = NSIndexPath.FromRowSection(Settings.SelectedIndex,0);
}
void PopulateDatabase()
{
_listData.Add("val1");
_listData.Add("val2");
_listData.Add("val3");
_listData.Add("val4");
_listData.Add("val5");
_listData.Add("val6");
_listData.Add("val7");
_listData.Add("val8");
_listData.Add("val9");
_listData.Add("val10");
_listData.Add("val11");
_listData.Add("val12");
_listData.Add("val13");
_listData.Add("val14");
_listData.Add("val15");
}
// Customize the number of sections in the table view.
public override int NumberOfSections (UITableView tableView)
{
return 1;
}
public override int RowsInSection (UITableView tableview, int section)
{
return _listData.Count;
}
// Customize the appearance of table view cells.
public override UITableViewCell GetCell (UITableView tableView, MonoTouch.Foundation.NSIndexPath indexPath)
{
var cell = tableView.DequeueReusableCell (CellIdentifier);
if (cell == null) {
cell = new UITableViewCell (UITableViewCellStyle.Default, CellIdentifier);
//cell.Accessory = UITableViewCellAccessory.DisclosureIndicator;
}
cell.TextLabel.Text = _listData[indexPath.Row];
cell.TextLabel.Font = UIFont.SystemFontOfSize(18);
return cell;
}
// Row Select
public override void RowSelected (UITableView tableView, NSIndexPath indexPath)
{
// Uncheck the previous row
if (_previousRow != null)
tableView.CellAt(_previousRow).Accessory = UITableViewCellAccessory.None;
// Do something with the row
var row = indexPath.Row;
Settings.SelectedIndex = row;
tableView.CellAt(indexPath).Accessory = UITableViewCellAccessory.Checkmark;
//Console.WriteLine("{0} selected",_controller.Items[row]);
_previousRow = indexPath;
// This is what the Settings does under Settings>Mail>Show on an iPhone
tableView.DeselectRow(indexPath,false);
}
// Set row height
public override float GetHeightForRow(UITableView tableView, NSIndexPath indexPath)
{
return 50f;
}
}
public class Settings
{
public static int SelectedIndex = 0;
}
}
}
I get the exception at line
tableView.CellAt(_previousRow).Accessory =UITableViewCellAccessory.None;
When i debug i found that Accessory
tableView.CellAt(_previousRow).Accessory Unknown member: Accessory
Any idea what is going wrong???
Got the anser
// Row Select
public override void RowSelected (UITableView tableView, NSIndexPath indexPath)
{
// Uncheck the previous row
if (_previousRow != null)
{
NSIndexPath [] temp = tableView.IndexPathsForVisibleRows;
if (temp.Contains(preIndex))
{
tableView.CellAt(_previousRow).Accessory = UITableViewCellAccessory.None;
}
}
// Do something with the row
var row = indexPath.Row;
Settings.SelectedIndex = row;
tableView.CellAt(indexPath).Accessory = UITableViewCellAccessory.Checkmark;
//Console.WriteLine("{0} selected",_controller.Items[row]);
_previousRow = indexPath;
// This is what the Settings does under Settings>Mail>Show on an iPhone
tableView.DeselectRow(indexPath,false);
}
I have a UITableView and a subclassed UITableViewSource class:
table = new UITableView(window.Bounds);
table.Source = new CellSource();
public class CellSource : UITableViewSource
{
// etc etc
I'm trying to get the selected row, so implemented the RowSelected method in my source class:
public override void RowSelected(UITableView tableview, NSIndexPath indexpath) {
Console.WriteLine("User tapped");
}
However, tapping on the cells gives no response at all, not even when simply trying to write to the console.
I can post the complete classes if that will help?
Thanks.
EDIT
So, after I create my UITableView:
table = new UITableView(window.Bounds);
table.AutoresizingMask = UIViewAutoresizing.All;
table.SeparatorStyle = UITableViewCellSeparatorStyle.None;
table.BackgroundColor = UIColor.Clear;
table.Source = new CellSource();
I use my source class to parse an XML file, and populate a list:
List<Treasure> treasures = new List<Treasure>();
protected class Treasure {
public string cellTitle { get; set; }
public string cellTag { get; set; }
public string cellImage { get; set; }
public string audioFile { get; set; }
public string mainTitle { get; set; }
public string mainTag { get; set; }
public string mainBody { get; set; }
public string mainImage { get; set; }
public string mainCaption { get; set; }
}
public CellSource (/*string[] items*/)
{
Console.WriteLine("CellSource called");
string fileName = "treasuresiPhone.xml";
XDocument doc = XDocument.Load(fileName);
treasures = doc.Descendants("treasures").FirstOrDefault().Descendants("treasure").Select(p=> new Treasure() {
cellTitle = p.Element("celltitle").Value,
cellTag = p.Element("celltagline").Value,
cellImage = p.Element("cellimage").Value
}).ToList();
numCells = treasures.Count();
}
I then create a CGBitmapContext in another class, return the image, and set this as the cells image:
public UIImage DrawCell (string cellImage) {
string cellI = cellImage;
//create a new graphics context
int width = 320;
int height = 110;
CGBitmapContext ctx = new CGBitmapContext(IntPtr.Zero, width, height, 8, 4*width, CGColorSpace.CreateDeviceRGB(), CGImageAlphaInfo.PremultipliedFirst);
//load an image
var imagePath = (#cellI);
var image = UIImage.FromFile(imagePath).CGImage;
ctx.DrawImage(new RectangleF(0, 0, width, height), image);
UIImage returnedImage = new UIImage();
returnedImage = FromImage(ctx.ToImage());
return returnedImage;
}
I add some other test stuff like a label, and override the RowSelected method:
public override UITableViewCell GetCell (UITableView tableView, MonoTouch.Foundation.NSIndexPath indexPath) {
UITableViewCell cell = tableView.DequeueReusableCell (cellIdentifier);
cell = new UITableViewCell (UITableViewCellStyle.Default, cellIdentifier);
cell.UserInteractionEnabled = false;
UILabel secondViewLabel = new UILabel();
//if there are no cells create a new one
if (cell == null) {
Console.WriteLine("cell == null");
} else {
//create a new cellobject - this grabs the image and returns a CGBitmapContext
CellObject _cellObject = new CellObject();
cell.ImageView.Image = _cellObject.DrawCell(treasures[indexPath.Row].cellImage);
//add text
secondViewLabel.Tag = 1;
secondViewLabel.Text = treasures[indexPath.Row].cellTitle;
secondViewLabel.TextColor = UIColor.White;
secondViewLabel.TextAlignment = UITextAlignment.Center;
secondViewLabel.Lines = 0;
secondViewLabel.LineBreakMode = UILineBreakMode.WordWrap;
secondViewLabel.Font = UIFont.FromName("Helvetica", 16);
secondViewLabel.BackgroundColor = UIColor.FromRGB(205, 54, 51);
//get the width of the text
SizeF labelSize = secondViewLabel.StringSize(secondViewLabel.Text, secondViewLabel.Font);
secondViewLabel.Frame = new RectangleF(0, 110 - (labelSize.Height + 10), labelSize.Width + 20, labelSize.Height + 10);
//add a second view
UIView secondView = new UIView();
secondView.AddSubview(secondViewLabel);
cell.ContentView.AddSubview(secondView);
}
return cell;
}
public override void RowSelected(UITableView tableview, NSIndexPath indexpath) {
Console.WriteLine("User tapped");
}
In Monotouch UITableViewSource is the combined UITableViewDataSource and UITableViewDelegate for a UITableView. So, when you extend such class you can take advantage of methods within both data source and delegate.
Said this, I would modify a little bit your code (see comments).
public override UITableViewCell GetCell (UITableView tableView, MonoTouch.Foundation.NSIndexPath indexPath)
{
UITableViewCell cell = tableView.DequeueReusableCell (cellIdentifier);
// if there are no cells create a new one
if (cell == null) {
cell = new UITableViewCell (UITableViewCellStyle.Default, cellIdentifier);
// why do you set the interaction to false? Setting it to false disable interaction for your cell
cell.UserInteractionEnabled = true;
// create label and view here. You customize the cell with additional elements once
}
// update image and label here (you need to grab a reference for your label for example through the tag)
return cell;
}
Take also a look to Working with Tables and Cells in Xamarin documentation.
Hope it helps.
I am in need of some help from you guys. I have a Monotouch UITableView which contains some web images. I have implemented a Task to fetch them asynchronously so the UI is responsive, an even added some animations to fade them in when they are fetched from the web.
My problems start when the user scrolls down very fast down the UITableView, so since the cells are resusable, several background tasks are queued for images. When he is at the bottom of the list, he might see the thumbnail displaying an image for another cell, then another, then another, then another, as the tasks are completed and each image replaces the other one. I am in need of some sort of checking whether the currently displayed cell corresponds to the correct image url, but not sure how to do that.
Here is the code for my TableSource class.
using System;
using MonoTouch.UIKit;
using MonoTouch.Foundation;
using System.Collections.Generic;
using System.Threading;
using System.Threading.Tasks;
using System.Diagnostics;
{
public class ListDelegate:UITableViewDelegate
{
private UINavigationController nav;
public override float GetHeightForRow (UITableView tableView, NSIndexPath indexPath)
{
return 128;
}
public override void RowSelected (UITableView tableView, NSIndexPath indexPath)
{
DealViewController c = new DealViewController(((ListDataSource)tableView.DataSource).deals[indexPath.Row].Id,nav);
nav.PushViewController(c,true);
tableView.DeselectRow(indexPath,true);
}
public ListDelegate(UINavigationController nav)
{
this.nav = nav;
}
}
public class ListDataSource:UITableViewDataSource
{
bool toggle=true;
Dictionary<string,UIImage> images = new Dictionary<string, UIImage>();
public List<MyDeal> deals = new List<MyDeal>();
Dictionary<int,ListCellViewController> controllers = new Dictionary<int, ListCellViewController>();
public ListDataSource(List<MyDeal> deals)
{
this.deals = deals;
}
public override int RowsInSection (UITableView tableview, int section)
{
return deals.Count;
}
public override UITableViewCell GetCell (UITableView tableView, MonoTouch.Foundation.NSIndexPath indexPath)
{
UITableViewCell cell = tableView.DequeueReusableCell("cell");
ListCellViewController cellController = null;
if (cell == null || !controllers.ContainsKey(cell.Tag))
{
cellController = new ListCellViewController();
NSBundle.MainBundle.LoadNib("ListCellViewController", cellController, null);
cell = cellController.Cell;
cell.Tag = Environment.TickCount;
controllers.Add(cell.Tag, cellController);
}
else
{
cellController = controllers[cell.Tag];
}
if (toggle)
{
cell.BackgroundView = new UIImageView(UIImage.FromFile("images/bg1.jpg"));
}
else
{
cell.BackgroundView = new UIImageView(UIImage.FromFile("images/bg2.jpg"));
}
toggle = !toggle;
MyDeal d = deals[indexPath.Row];
cellController.SetValues(d.Title,d.Price,d.Value,d.DiscountPercent);
GetImage(cellController.Thumbnail,d.Thumbnail);
return cell;
}
private void GetImage(UIImageView img, string url)
{
img.Alpha = 0;
if (url != string.Empty)
{
if (images.ContainsKey(url))
{
img.Image = images[url];
img.Alpha = 1;
}
else
{
var context = TaskScheduler.FromCurrentSynchronizationContext ();
Task.Factory.StartNew (() => {
NSData imageData = NSData.FromUrl(new NSUrl(url));
var uimg = UIImage.LoadFromData(imageData);
images.Add(url,uimg);
return uimg;
}).ContinueWith (t => {
InvokeOnMainThread(()=>{
img.Image = t.Result;
RefreshImage(img);
});
}, context);
}
}
}
private void RefreshImage(UIImageView img)
{
UIView.BeginAnimations("imageThumbnailTransitionIn");
UIView.SetAnimationDuration(0.5f);
img.Alpha = 1.0f;
UIView.CommitAnimations();
}
}
}
Here is the ListCellViewController, that contains a custom cell
using System;
using System.Collections.Generic;
using System.Linq;
using MonoTouch.Foundation;
using MonoTouch.UIKit;
{
public partial class ListCellViewController : UIViewController
{
#region Constructors
// The IntPtr and initWithCoder constructors are required for items that need
// to be able to be created from a xib rather than from managed code
public ListCellViewController (IntPtr handle) : base(handle)
{
Initialize ();
}
[Export("initWithCoder:")]
public ListCellViewController (NSCoder coder) : base(coder)
{
Initialize ();
}
public ListCellViewController () : base("ListCellViewController", null)
{
Initialize ();
}
void Initialize ()
{
}
public UIImageView Thumbnail
{
get{return thumbnailView;}
}
public UITableViewCell Cell
{
get {return cell;}
}
public void SetValues(string title,decimal price,decimal valuex,decimal discount,int purchases)
{
}
#endregion
}
}
All help is greatly appreciated
I have found a simple (maybe not optimal) solution. The trick I used is a Queue object to store the last n valid image urls, where n the number of words displayable in the screen. Once a cell is reused, the last image url is dequeued and when it is about to be displayed I check if it's in the queue, and if it's not I reject it.
I am using Monotouch to develop iPad app.
Here is my scenario:
I Created Tabbed base application.
e.g. Home, Admin, Reports....etc
Home tab is UIViewController.
I want three section inside Home Tab:
e.g. Category(Table view with navigation control (reason to use navigation because we have subcategories inside Category) beside Category Table, Items of selected category(Other Table view) and right hand side is detail view of Selected ITEM.
Here is what i did....
Dynamically create two tableview controller and added to main view controller.
HomeViewController.cs:
public override void ViewDidLoad ()
{
base.ViewDidLoad ();
RootViewController rvc = new RootViewController("",UITableViewStyle.Grouped);
// navigation controller will manage the views displayed and provide navigation buttons
navigationController = new UINavigationController();
navigationController.PushViewController(rvc, false);
navigationController.TopViewController.Title ="Category";
navigationController.View.Frame = new RectangleF (0, 50, (50), (600));
// Main window to which we add the navigation controller to
this.View.AddSubview(navigationController.View);
itemtable.Delegate = new TableViewDelegate (list);
itemtable.DataSource = new TableViewDataSource (list);
// Perform any additional setup after loading the view, typically from a nib.
}
=====================================================
RootViewController:
using System;
using System.Collections.Generic;
using System.Linq;
using MonoTouch.Foundation;
using MonoTouch.UIKit;
using System.Drawing;
namespace DVPNTN_MobileApp
{
[MonoTouch.Foundation.Register("RootViewController")]
public partial class RootViewController : UITableViewController
{
public List<string> RootData = new List<string> { "Group1", "Group2" };
MonoTouch.UIKit.UINavigationController navigationControllerItem;
string SelectedGroup;
// Allow us to set the style of the TableView
public RootViewController(string selectedGroup, UITableViewStyle style) : base(style)
{
this.SelectedGroup = selectedGroup;
}
class DataSource : UITableViewDataSource
{
static NSString kCellIdentifier = new NSString ("MyIdentifier");
RootViewController tvc;
public DataSource (RootViewController tvc)
{
this.tvc = tvc;
}
public override int RowsInSection (UITableView tableView, int section)
{
return tvc.RootData.Count;
}
public override UITableViewCell GetCell (UITableView tableView, NSIndexPath indexPath)
{
var cell = tableView.DequeueReusableCell (kCellIdentifier);
if (cell == null)
{
cell = new UITableViewCell (UITableViewCellStyle.Default, kCellIdentifier);
}
cell.TextLabel.Text = tvc.RootData.ElementAt(indexPath.Row);
cell.Accessory = UITableViewCellAccessory.DetailDisclosureButton;
return cell;
}
}
class TableDelegate : UITableViewDelegate
{
RootViewController tvc;
SubGroupViewController sgvc;
public TableDelegate (RootViewController tvc)
{
this.tvc = tvc;
}
public override void RowSelected (UITableView tableView, NSIndexPath indexPath)
{
string selectedGroup = tvc.RootData.ElementAt(indexPath.Row);
sgvc = new SubGroupViewController(selectedGroup, UITableViewStyle.Grouped);
tvc.NavigationController.PushViewController(sgvc,true);
//tvc.View.RemoveFromSuperview();
//tvc.DidReceiveMemoryWarning();
GC.Collect();
}
}
public override void DidReceiveMemoryWarning ()
{
// Releases the view if it doesn't have a superview.
base.DidReceiveMemoryWarning ();
// Release any cached data, images, etc that aren't in use.
}
public override void ViewDidLoad ()
{
base.ViewDidLoad ();
TableView.Delegate = new TableDelegate (this);
TableView.DataSource = new DataSource (this);
RootVIewItemController rvc1 = new RootVIewItemController(SelectedGroup,UITableViewStyle.Grouped);
// navigation controller will manage the views displayed and provide navigation buttons
navigationControllerItem = new UINavigationController();
navigationControllerItem.PushViewController(rvc1, false);
navigationControllerItem.TopViewController.Title = SelectedGroup + " " + "Item List";
navigationControllerItem.View.Frame = new RectangleF (0, 300, (50),(700));
//this.View.AddSubview(navigationControllerItem.View);
//rvc1.View.EnableInputClicksWhenVisible = true;
//this.ParentViewController.AddChildViewController(navigationControllerItem);
**> Problem is here --- subview is successfully added to parent view but it's not accessible, mean items are there but we can't touch cell or row???????**
ParentViewController.View.AddSubview(navigationControllerItem.View);
GC.Collect();
}
public override bool ShouldAutorotateToInterfaceOrientation (UIInterfaceOrientation toInterfaceOrientation)
{
// Return true for supported orientations
return true;
}
}
}
********** ItemViewController **
using System;
using System.Collections.Generic;
using System.Linq;
using MonoTouch.Foundation;
using MonoTouch.UIKit;
namespace DVPNTN_MobileApp
{
[MonoTouch.Foundation.Register("RootVIewItemController")]
public partial class RootVIewItemController : UITableViewController
{
public List<string> RootData = new List<string> { "Item 1", "Item 2", "Item 3", "Item 4" };
string SelectedGroup;
public RootVIewItemController (string selectedGroup, UITableViewStyle style) : base (style)
{
this.SelectedGroup = selectedGroup;
}
class DataSource : UITableViewDataSource
{
static NSString kCellIdentifier = new NSString ("MyIdentifier");
RootVIewItemController tvc;
public DataSource (RootVIewItemController tvc)
{
this.tvc = tvc;
}
public override int RowsInSection (UITableView tableView, int section)
{
return tvc.RootData.Count;
}
public override UITableViewCell GetCell (UITableView tableView, NSIndexPath indexPath)
{
var cell = tableView.DequeueReusableCell (kCellIdentifier);
if (cell == null)
{
cell = new UITableViewCell (UITableViewCellStyle.Default, kCellIdentifier);
}
cell.TextLabel.Text = tvc.RootData.ElementAt(indexPath.Row);
//cell.Accessory = UITableViewCellAccessory.DetailDisclosureButton;
return cell;
}
}
class TableDelegate : UITableViewDelegate
{
RootVIewItemController tvc;
SubGroupViewController sgvc;
public TableDelegate (RootVIewItemController tvc)
{
this.tvc = tvc;
}
public override void RowSelected (UITableView tableView, NSIndexPath indexPath)
{
string selectedGroup = tvc.RootData.ElementAt(indexPath.Row);
Console.WriteLine(
"TableViewDelegate.RowSelected: Label={0}",selectedGroup);
/*
if(sgvc == null)
sgvc = new SubGroupViewController(selectedGroup, UITableViewStyle.Grouped);
tvc.NavigationController.PushViewController(sgvc,true);*/
}
}
public override void ViewDidLoad ()
{
base.ViewDidLoad ();
TableView.Delegate = new TableDelegate (this);
TableView.DataSource = new DataSource (this);
}
public override void DidReceiveMemoryWarning ()
{
// Releases the view if it doesn't have a superview.
base.DidReceiveMemoryWarning ();
// Release any cached data, images, etc that aren't in use.
}
}
}
Question:
Is it right way to doing this scenario?
It's work fine but when we add Item view to parent view controller it is not accessible, mean item list are there but cells are not accessible, we can't touch cell and raise even or do scrolling.
Please anybody can help me?
Thanks
I am not completely sure what you wanted to do. I took my best guess and created a sample with two solutions. One solution per tab. The first uses the built-in split view controller. I have read several places the UISplitViewController must be the root of the application. I have broke that rule by adding it as a child of the tab control.
The second creates a custom version of a split control.
using System;
using System.Collections.Generic;
using MonoTouch.Foundation;
using MonoTouch.UIKit;
using System.Drawing;
namespace delete04223
{
[Register ("AppDelegate")]
public partial class AppDelegate : UIApplicationDelegate
{
// class-level declarations
UIWindow window;
UITabBarController tabBarController;
public override bool FinishedLaunching (UIApplication app, NSDictionary options)
{
// create a new window instance based on the screen size
window = new UIWindow (UIScreen.MainScreen.Bounds);
var viewController1 = new MyUISplitViewController ();
var viewController2 = new MyUISplitViewController2 ();
tabBarController = new UITabBarController ();
tabBarController.ViewControllers = new UIViewController [] {
viewController1,
viewController2,
};
window.RootViewController = tabBarController;
// make the window visible
window.MakeKeyAndVisible ();
return true;
}
}
public class MyUISplitViewController : UISplitViewController
{
public MyUISplitViewController ()
{
this.Title = "Native Split View";
}
public override void ViewDidLoad ()
{
base.ViewDidLoad ();
var viewController1 = new LeftViewController ();
var viewController2 = new DummyViewController ("Pane 1", "Pane 1");
this.ViewControllers = new UIViewController [] {viewController1, viewController2};
this.WeakDelegate = viewController2;
}
public override bool ShouldAutorotateToInterfaceOrientation (UIInterfaceOrientation toInterfaceOrientation)
{
return true;
}
}
public class MyUISplitViewController2 : UIViewController
{
public MyUISplitViewController2 ()
{
this.Title = "Custom Split View";
}
public override void ViewDidLoad ()
{
base.ViewDidLoad ();
var viewController1 = new LeftViewController ();
var viewController2 = new DummyViewController ("Pane 1", "Pane 1");
this.AddChildViewController (viewController1);
this.AddChildViewController (viewController2);
this.View.AddSubview (viewController1.View);
this.View.AddSubview (viewController2.View);
}
public override void ViewDidLayoutSubviews ()
{
base.ViewDidLayoutSubviews ();
RectangleF lRect = this.View.Frame;
RectangleF rRect = lRect;
lRect.Width = .3f * lRect.Width;
rRect.X = lRect.Width + 1;
rRect.Width = (.7f * rRect.Width)-1;
this.View.Subviews[0].Frame = lRect;
this.View.Subviews[1].Frame = rRect;
}
public override bool ShouldAutorotateToInterfaceOrientation (UIInterfaceOrientation toInterfaceOrientation)
{
return true;
}
}
public class LeftViewController : UINavigationController
{
public LeftViewController ()
{
}
public override void ViewDidLoad ()
{
base.ViewDidLoad ();
MyUITableViewController table = new MyUITableViewController (this);
this.PushViewController (table, false);
}
public override bool ShouldAutorotateToInterfaceOrientation (UIInterfaceOrientation toInterfaceOrientation)
{
return true;
}
public override UIViewController PopViewControllerAnimated (bool animated)
{
return base.PopViewControllerAnimated (true);
}
}
public class DummyViewController : UIViewController
{
string _myLabelText = "";
UIToolbar _toolbar;
public DummyViewController (string viewName, string labelText)
{
this.Title = viewName;
this._myLabelText = labelText;
this.View.BackgroundColor = UIColor.White;
}
public override bool ShouldAutorotateToInterfaceOrientation (UIInterfaceOrientation toInterfaceOrientation)
{
return true;
}
public override void ViewDidLoad ()
{
base.ViewDidLoad ();
float center = this.View.Frame.Width / 2f;
UILabel label = new UILabel (new RectangleF (center - 50, 100, 100, 40));
label.Text = this._myLabelText;
label.TextAlignment = UITextAlignment.Center;
label.AutoresizingMask = UIViewAutoresizing.FlexibleMargins;
RectangleF rect = this.View.Frame;
rect.Y = 0;
rect.Height = 44;
_toolbar = new UIToolbar (rect);
_toolbar.AutoresizingMask = UIViewAutoresizing.FlexibleWidth;
this.View.AddSubview (label);
this.View.AddSubview (_toolbar);
}
[Export("splitViewController:willHideViewController:withBarButtonItem:forPopoverController:")]
public void WillHideViewController (UISplitViewController svc, UIViewController vc,
UIBarButtonItem barButtonItem, UIPopoverController pc)
{
barButtonItem.Title = "Menu";
var items = new List<UIBarButtonItem> ();
items.Add (barButtonItem);
if (_toolbar.Items != null)
items.AddRange (_toolbar.Items);
_toolbar.SetItems (items.ToArray (), true);
//popoverController = pc;
}
[Export("splitViewController:willShowViewController:invalidatingBarButtonItem:")]
public void WillShowViewController (UISplitViewController svc, UIViewController vc,
UIBarButtonItem button)
{
// Called when the view is shown again in the split view, invalidating the button and popover controller.
var items = new List<UIBarButtonItem> (_toolbar.Items);
items.RemoveAt (0);
_toolbar.SetItems (items.ToArray (), true);
//popoverController = null;
}
}
internal class MyUITableViewController : UITableViewController
{
static NSString kCellIdentifier = new NSString ("MyIdentifier");
LeftViewController _parent;
public MyUITableViewController (LeftViewController parent) : base (UITableViewStyle.Plain)
{
this.TableView.WeakDelegate = this;
this.TableView.WeakDataSource = this;
this._parent = parent;
}
[Export ("tableView:numberOfRowsInSection:")]
public int RowsInSection (UITableView tableView, int section)
{
if (section == 0)
return 2;
return 3;
}
[Export ("tableView:cellForRowAtIndexPath:")]
public UITableViewCell GetCell (UITableView tableView, NSIndexPath indexPath)
{
var cell = tableView.DequeueReusableCell (kCellIdentifier);
if (cell == null)
{
cell = new UITableViewCell (UITableViewCellStyle.Default, kCellIdentifier);
}
cell.TextLabel.Text = GetRowName (indexPath);
return cell;
}
private string GetRowName (NSIndexPath indexPath)
{
string ret;
if (indexPath.Section == 0)
{
ret =
indexPath.Row == 0 ? "Row A" : "Row B";
}
else
{
ret =
indexPath.Row == 0 ? "Row D" :
indexPath.Row == 1 ? "Row E" : "Row F";
}
return ret;
}
[Export ("numberOfSectionsInTableView:")]
public int NumberOfSections (UITableView tableView)
{
return 2;
}
[Export ("tableView:titleForHeaderInSection:")]
public string TitleForHeader (UITableView tableView, int section)
{
if (section == 0)
return "One";
return "Two";
}
[Export ("tableView:didSelectRowAtIndexPath:")]
public virtual void RowSelected (UITableView tableView, NSIndexPath indexPath)
{
Console.WriteLine("Item Selected: Section={0}, Row={1}",indexPath.Section, indexPath.Row);
this._parent.PushViewController (new MySubUITableViewController (), true);
}
public override bool ShouldAutorotateToInterfaceOrientation (UIInterfaceOrientation toInterfaceOrientation)
{
return true;
}
}
internal class MySubUITableViewController : UITableViewController
{
static NSString kCellIdentifier = new NSString ("MyIdentifier");
static string [] _names = new string [] {"One", "Two", "Three", "Four", "Five"};
public MySubUITableViewController () : base (UITableViewStyle.Plain)
{
this.TableView.WeakDelegate = this;
this.TableView.WeakDataSource = this;
}
[Export ("tableView:numberOfRowsInSection:")]
public int RowsInSection (UITableView tableView, int section)
{
return 5;
}
[Export ("tableView:didSelectRowAtIndexPath:")]
public virtual void RowSelected (UITableView tableView, NSIndexPath indexPath)
{
Console.WriteLine("Item Selected: Section={0}, Row={1}",indexPath.Section, indexPath.Row);
}
[Export ("tableView:cellForRowAtIndexPath:")]
public UITableViewCell GetCell (UITableView tableView, NSIndexPath indexPath)
{
var cell = tableView.DequeueReusableCell (kCellIdentifier);
if (cell == null)
{
cell = new UITableViewCell (UITableViewCellStyle.Default, kCellIdentifier);
}
cell.TextLabel.Text = _names[indexPath.Row];
return cell;
}
public override bool ShouldAutorotateToInterfaceOrientation (UIInterfaceOrientation toInterfaceOrientation)
{
return true;
}
}
}