Increasing space size between table cells - ios

I have made a UITableViewController, and in it, have programmatically added in cells. I have used snippet code directly off of Microsoft's website on how to create a table using Xamarin: https://learn.microsoft.com/en-us/xamarin/ios/user-interface/controls/tables/populating-a-table-with-data
The only problem is that the code used produces a table which, when you add cells to, creates a spacer between each subsequent cell, of increasing size:
And my storyboard of the UITableViewController looks normal, and it contains no constraints or spacing:
Does anyone have any idea as to why this error might be occurring?
To clarify the code used for the source file for the table is:
public class mailSource : UITableViewSource
{
string[] TableItems;
string CellIdentifier = "TableCell";
public mailSource(string[] items)
{
TableItems = items;
}
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 (cell == null)
{ cell = new UITableViewCell(UITableViewCellStyle.Default, CellIdentifier); }
cell.TextLabel.Text = item;
return cell;
}
}
And the code for the view controller is:
public partial class mailTableController : UITableViewController
{
public mailTableController (IntPtr handle) : base (handle)
{
}
public override void ViewDidLoad()
{
base.ViewDidLoad();
table = new UITableView(View.Bounds); // defaults to Plain style
string[] tableItems = new string[] { "Vegetables", "Fruits", "Flower Buds", "Legumes", "Bulbs", "Tubers", "a", "a", "a", "a", "a", "a", "a", "a", "a", "a", "a", "a" };
table.Source = new mailSource(tableItems);
Add(table);
}
}

Related

Update textfield after a row is selected in tableview Xamarin.IOS

Here is a scenario that I am encountering. I am having a login screen with UserName as one of the fields. The user can select a user using a dropdown list (implemented via a button - to simulate drop down arrow, a popover controller and a table view). I having a controller (UserNameController) which has the logic of fetching the usernames and binding the same to the table view within it. The UserNameController is called via ViewController.cs which has the textfield and the dropdown button using below code:
var content = this.Storyboard.InstantiateViewController("UserNameLookUp") as UserNameController;
UIPopoverController popover = new UIPopoverController(content);
//popover.SetPopoverContentSize(new SizeF(80, 80), true);
popover.PresentFromRect(new RectangleF(float.Parse((sender.Frame.X + 115).ToString()),
float.Parse((sender.Frame.Y + 180).ToString())
, 80, 80), View, UIPopoverArrowDirection.Up, true);
and in UserNameController:
public override void ViewDidLoad()
{
string[] userName = new string[10];
tblVwUserName.Source = new TableSource(userName);
}
}
and TabelSource.cs looks like this:
public class TableSource : UITableViewSource
{
string[] TableItems;
string CellIdentifier = "TableCell";
public TableSource(string[] items)
{
TableItems = items;
}
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)
{
// HOW TO SET BACK THE USERNAME TEXT FIELD IN VIEWCONTROLLER? AND CLOSE THE POPUP
tableView.DeselectRow(indexPath, true);
}
}
Now how do i need to display the selected username in the viewcontroller's textfield and close the popup?
Thanks!
Sid
I typically solve this requirement with properties on the TableViewSource and the TableViewController. Your source will change to:
public class TableSource : UITableViewSource
{
string[] TableItems;
string CellIdentifier = "TableCell";
public string SelectedItem {get; set;}
public TableSource(string[] items)
{
TableItems = items;
}
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)
{
SelectedItem = items[indexPath.Row];
tableView.DeselectRow(indexPath, true);
}
}
Now you need a way to get that value. There are two possible ways:
1) UserNameController is of type UITableViewController, than it would be (content.TableView.Source as TableSource).SelectedItem
2) UserNameController is not of type UITableViewController, than I would add a property too that returns the property of the TableViewSource.
Last thing to do is closing the Popover and query the selected item. For this task I would wrap the UserNameController in a UINavigationController and add a Cancel and a Done button into it like this:
var navigationController = new UINavigationController(content);
var popover = new UIPopoverController(navigationController);
content.NavigationItem.SetLeftBarButtonItem(new UIBarButtonItem(UIBarButtonSystemItem.Cancel, (s, e) =>
{
parentController.DismissViewController(true, null);
}), true);
content.NavigationItem.SetRightBarButtonItem(new UIBarButtonItem(UIBarButtonSystemItem.Done, (s, e) =>
{
parentController.DismissViewController(true, null);
var selectedItem = (content.Source as TableSource).SelectedItem;
}), true);

How to insert row at desired index in UITableView Xamarin.ios?

i want to add a couple of rows on the index next to the selected row index.
For example if i have clicked row at index 4 i want some rows to added at index 5,6,7. and the actual rows of these indexes shift forward on 8,9,10 etc.
In your table view source MyTableViewSource you have to override RowSelected. In this method, you check your row number and add items to you table view source's Items after adding them, you have to call ReloadData().
class MyTableViewSource : UITableViewSource
{
private readonly UITableView _table;
public List<string> Items { get; }
public MyTableViewSource(UITableView table)
{
_table = table;
Items = new List<string> { "Hello", "World", "Bla", "Foo" };
}
public override UITableViewCell GetCell(UITableView tableView, NSIndexPath indexPath)
{
var item = Items[indexPath.Row];
var cell = // create cellfor item
return cell;
}
public override nint RowsInSection(UITableView tableview, nint section)
{
return Items.Count;
}
public override void RowSelected(UITableView tableView, NSIndexPath indexPath)
{
if (indexPath.Row == 2)
{
Items.Insert(3, "Horst");
Items.Insert(4, "Klaus");
Items.Insert(5, "Peter");
_table.ReloadData();
}
}
}
Creating them:
var table = new UITableView();
table.Source = new MyTableViewSource(table);
If you want more control over the animation, you can use this version:
public override void RowSelected(UITableView tableView, NSIndexPath indexPath)
{
if (indexPath.Row == 2)
{
_table.BeginUpdates();
Items.Insert(3, "Horst");
Items.Insert(4, "Klaus");
Items.Insert(5, "Peter");
_table.InsertRows(new[] { NSIndexPath.Create(0, 3), NSIndexPath.Create(0, 4), NSIndexPath.Create(0, 5) }, UITableViewRowAnimation.Left);
_table.EndUpdates();
}
}

Xamarin Table View Crash

I want to use UITableView on XamarinApp.
I tried UITableView example Populating a Table with Data ,but it doesn't work.
When i used this.Add(table); cause crash. When I remove this.Add(table) it's shows empty table.
Please help me...
Here is my code
using Foundation;
using System;
using System.CodeDom.Compiler;
using UIKit;
namespace KUkyuko
{
partial class MyTableViewSource : UITableView
{
public MyTableViewSource(IntPtr handle) : base (handle)
{
var table = this;
string[] tableItems = new string[] {"Vegetables","Fruits","Flower Buds","Legumes","Bulbs","Tubers"};
table.Source = new TableSource(tableItems);
this.Add(table); //this code cause crash
}
}
public class TableSource : UITableViewSource {
string[] TableItems;
string CellIdentifier = "TableCell";
public TableSource (string[] items)
{
TableItems = items;
}
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;
}
}
}
Im abit unsure as to what you trying to achieve but
I would change the tableview to a tableViewController like so::
partial class TableViewController : UITableViewController
{
public TableViewController (IntPtr handle) : base (handle)
{
// Register the TableView's data source
string[] tableItems = new string[] {"Vegetables","Fruits","Flower Buds","Legumes","Bulbs","Tubers"};
this.TableView.Source = new TableSource(tableItems);
}
}
public class TableSource : UITableViewSource {
string[] TableItems;
string CellIdentifier = "TableCell";
public TableSource (string[] items)
{
TableItems = items;
}
public override nint RowsInSection (UITableView tableview, nint section)
{
return TableItems.Length;
}
public override nint NumberOfSections (UITableView tableView)
{
// TODO: return the actual number of sections
return 1;
}
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;
}
}
If you are using the table inside a viewController then you need to do this part in your viewController and set up a outlet for the table.
The reason this code crashes as you are adding this to this:
var table = this;
string[] tableItems = new string[] {"Vegetables","Fruits","Flower Buds","Legumes","Bulbs","Tubers"};
table.Source = new TableSource(tableItems);
this.Add(table); //this code cause crash as it is the same as this.Add(this)
Hope this helps!

Monotouch custom UITableViewCell GetCell not initialising new custom view cells

I've been foloowing a number of tutorials to put together a custom table view cell using storyboard for a prototype table view.
I'm new to monotouch and managed to get a working solution for standard cell types. Running into issues with custom view cells as I'm unable to initialise a fresh cell in the correct manner. Some old tutorials appear to load a cell nib file but I'm using storyboard with the below code.
Where am I going wrong?
(I would use monotouch dialog but not couldn't figure out a way to add lovely uipickerviews on accessory, etc in a simple manner).
http://docs.xamarin.com/guides/ios/user_interface/tables/part_5_-_using_xcode%2C_interface_builder%2C_and_storyboards
public override UITableViewCell GetCell (UITableView tableView, MonoTouch.Foundation.NSIndexPath indexPath)
{
// in a Storyboard, Dequeue will ALWAYS return a cell
//*** above comment doesnt seem to hold for custom uitableview cells
UITableViewCell cell = tableView.DequeueReusableCell (cellIdentifier);
// now set the properties as normal
cell.TextLabel.Text = tableItems[indexPath.Row].Name;
if (tableItems[indexPath.Row].Done)
cell.Accessory = UITableViewCellAccessory.Checkmark;
else
cell.Accessory = UITableViewCellAccessory.None;
return cell;
}
// here's my implementation of GetCell but problem is that I can't seem to generate a new cell
public override UITableViewCell GetCell (UITableView tableView, NSIndexPath indexPath)
{
_cellIdentifier = "SingleTimeViewCell";
CustomViewCell cell = tableView.DequeueReusableCell (_cellIdentifier) as
// if (cell == null)
// {
// cell = new SingleTimeViewCell();
// }
cell.myCustomProperty = "hello";
return cell;
}
// here's the auto generated CustomViewCell class from Xcode storyboard
public partial class CustomViewCell : UITableViewCell
{
public CustomViewCell () : base() // I added this ctor but it didnt seem to help matters
{
}
public CustomViewCell (IntPtr handle) : base (handle)
{
}
}
It can be done using only storyboards with prototype cells, see this sample from Xamarin forums.
Here is the relevant code (credit to Stephane Cordonnier):
public partial class CustomCellStoryBoardViewController : UIViewController
{
public CustomCellStoryBoardViewController(IntPtr handle) : base (handle)
{
}
public override void DidReceiveMemoryWarning()
{
base.DidReceiveMemoryWarning();
}
public override void ViewDidLoad()
{
base.ViewDidLoad();
this.MyTableView.DataSource = new MyDataSource();
}
private class MyDataSource : UITableViewDataSource
{
private String[] items = {"Item 1", "Item 2", "Item 3", "Item 4", "Item 5" };
public MyDataSource()
{
}
public override int RowsInSection(UITableView tableView, int section)
{
return this.items.Length;
}
public override UITableViewCell GetCell(UITableView tableView, NSIndexPath indexPath)
{
MyCustomCell cell = (MyCustomCell)tableView.DequeueReusableCell(MyCustomCell.Key);
cell.SetTitle(this.items[indexPath.Row]);
return cell;
}
}
}
The custom cell:
[Register ("MyCustomCell")]
public class MyCustomCell : UITableViewCell
{
[Outlet]
MonoTouch.UIKit.UILabel TitleLabel { get; set; }
public static readonly NSString Key = new NSString("MyCustomCell");
public MyCustomCell(IntPtr handle) : base(handle)
{
}
public void SetTitle(String title)
{
this.TitleLabel.Text = title;
}
}
Make sure you have set the custom cell class and identifier to the name of your cell class in the storyboard.
In your .xib file, for that cell, are you using the same cell identifier 'SingleTimeViewCell'. I'm not sure exactly how you've created your custom cell but I know I had what sounds like the same issue.
You cant'd do it with storyboard. Use main storyboard and separate xib files for the cells.
I read so mush non sense about tableview cell and reuse and Xib,
Almost every sample code a found jsute don't reuse properly...
on iOS6 and further the simplest way to do it :
create a new UITABLEVIEWCELL from Xamarin. (let's say MyCustomCell)
in InterfaceBuilder set the Identifier of the cell to the value of MyCustomCell.Key (found the value in MyCsutomCell.cs created by Xamarin Studio)
In your ViewController Register the nib this way :
tableView.RegisterNibForCellReuse (MyCsutomCell.Nib, MyCsutomCell.Key);
then on the GetCell méthode in your dataSource symple dequeue a cell (it will create one if none exists):
MyCsutomCell cell = tableView.DequeueReusableCell (MyCsutomCell.Key) as MyCsutomCell;

Multiple tabs in rootview

I am very new to XCode and Monotouch development. I am trying to add multiple tabs in root view (Master-Detail application). I am using Mono for development and xCode 4 for UI design. For example, one tab shows list of employees and another tab shows list of departments.
I removed the default table view in the rootviewController.xib file and replaced with tabbedview but I cant get new table view for each tab working.
I would appreciate if anyone can show me the correct way of doing this.
UPDATED: I use the code below to add 2 tabs and 2 table view. One is to hold "Securities" and the other one to hold the "Indicators"
using System;
using System.Drawing;
using MonoTouch.Foundation;
using MonoTouch.UIKit;
using System.Collections.Generic;
namespace ChartQ
{
public partial class RootViewController : UITabBarController
{
public DetailViewController DetailVC { get; set; }
private List<SecurityInfo> listSecInfo = new List<SecurityInfo>();
private List<Indicator> listIndicator = new List<Indicator>();
public RootViewController () : base ("RootViewController", null)
{
this.Title = NSBundle.MainBundle.LocalizedString ("Securities", "Securities");
//this.ClearsSelectionOnViewWillAppear = false;
this.ContentSizeForViewInPopover = new SizeF (100f, 200f);
//this.ContentSizeForViewInPopover = new SizeF (320f, 600f);
// Custom initialization
}
public override void ViewDidLoad ()
{
base.ViewDidLoad ();
//Read data from Database..
UITableViewController secController = new UITableViewController();
UITableViewController IndiController = new UITableViewController();
this.AddChildViewController(secController);
this.AddChildViewController(IndiController);
SQLLiteDatabase db = new SQLLiteDatabase();
listSecInfo = db.ReadSecurities();
listIndicator = db.ReadIndicator();
//TableView.Source = new SecuritiesDataSource (this);
secController.TableView.Source = new SecuritiesDataSource (this);
secController.TableView.SelectRow (NSIndexPath.FromRowSection (0, 0), false, UITableViewScrollPosition.Middle);
IndiController.TableView.Source = new IndicatorDataSource (this);
IndiController.TableView.SelectRow (NSIndexPath.FromRowSection (0, 0), false, UITableViewScrollPosition.Middle);
}
public override bool ShouldAutorotateToInterfaceOrientation (UIInterfaceOrientation toInterfaceOrientation)
{
// Return true for supported orientations
return true;
}
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 ViewDidUnload ()
{
base.ViewDidUnload ();
// Clear any references to subviews of the main view in order to
// allow the Garbage Collector to collect them sooner.
//
// e.g. myOutlet.Dispose (); myOutlet = null;
ReleaseDesignerOutlets ();
}
class SecuritiesDataSource : UITableViewSource
{
static NSString cellIdentifier = new NSString ("CellId");
RootViewController controller;
public SecuritiesDataSource (RootViewController controller)
{
this.controller = controller;
}
// 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 controller.listSecInfo.Count;
}
public override string TitleForHeader (UITableView tableView, int section)
{
return "Securities";
}
// Customize the appearance of table view cells.
public override UITableViewCell GetCell (UITableView tableView, MonoTouch.Foundation.NSIndexPath indexPath)
{
string cellIdentifier = "Cell";
var cell = tableView.DequeueReusableCell (cellIdentifier);
if (cell == null) {
cell = new UITableViewCell (UITableViewCellStyle.Subtitle, cellIdentifier);
//Add in a detail disclosure icon to each cell
cell.Accessory = UITableViewCellAccessory.DetailDisclosureButton;
}
// Configure the cell.
var sInfo = controller.listSecInfo [indexPath.Row];
cell.TextLabel.Text = String.Format ("{0}",sInfo.SecCode);
cell.DetailTextLabel.Text = sInfo.SecName;
return cell;
}
private void InfoAlert (string msg)
{
using (UIAlertView av = new UIAlertView("Info", msg, null, "OK", null)) {
av.Show ();
}
}
public override void RowSelected (UITableView tableView, NSIndexPath indexPath)
{
var sInfo = controller.listSecInfo [indexPath.Row];
controller.DetailVC.DrawChart(sInfo.SecID, sInfo.SecCode);
}
}
class IndicatorDataSource : UITableViewSource
{
static NSString cellIdentifier = new NSString ("CellId");
RootViewController controller;
public IndicatorDataSource (RootViewController controller)
{
this.controller = controller;
}
// 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 controller.listIndicator.Count;
}
public override string TitleForHeader (UITableView tableView, int section)
{
return "Indicators";
}
// Customize the appearance of table view cells.
public override UITableViewCell GetCell (UITableView tableView, MonoTouch.Foundation.NSIndexPath indexPath)
{
string cellIdentifier = "Cell";
var cell = tableView.DequeueReusableCell (cellIdentifier);
if (cell == null) {
cell = new UITableViewCell (UITableViewCellStyle.Subtitle, cellIdentifier);
//Add in a detail disclosure icon to each cell
cell.Accessory = UITableViewCellAccessory.DetailDisclosureButton;
}
// Configure the cell.
var sInfo = controller.listIndicator [indexPath.Row];
cell.TextLabel.Text = String.Format ("{0}",sInfo.DescriptiveName);
cell.DetailTextLabel.Text = sInfo.ShortName;
return cell;
}
private void InfoAlert (string msg)
{
using (UIAlertView av = new UIAlertView("Info", msg, null, "OK", null)) {
av.Show ();
}
}
public override void RowSelected (UITableView tableView, NSIndexPath indexPath)
{
var sInfo = controller.listIndicator [indexPath.Row];
//controller.DetailVC.DrawChart(sInfo.SecID, sInfo.SecCode);
}
}
}
However, it crashes when I click the second tab. I am getting the error as below
"Got a SIGSEGV while executing native code"
The first list populated ok.
I think the UITableViewController objects are getting collected and causing your abort. Make secController and secController member variables.
public partial class RootViewController : UITabBarController
{
UITableViewController secController;
UITableViewController IndiController;
public DetailViewController DetailVC { get; set; }
...
public override void ViewDidLoad ()
{
base.ViewDidLoad ();
//Read data from Database..
secController = new UITableViewController ();
IndiController = new UITableViewController ();

Resources