class CallHistoryDataSource : UITableViewSource
{
CallHistoryController controller;
public CallHistoryDataSource (CallHistoryController controller)
{
this.controller = controller;
}
public override int RowSelected(UITableView tableView, int section)
{
return controller.PhoneNumbers.Count;
}
//
// Returns a table cell for the row indicated by row property of the NSIndexPath
// This method is called multiple times to populate each row of the table.
// The method automatically uses cells that have scrolled off the screen or creates new ones as necessary.
//
public override UITableViewCell GetCell (UITableView tableView, NSIndexPath indexPath)
{
var cell = tableView.DequeueReusableCell (CallHistoryController.callHistoryCellId);
int row = indexPath.Row;
cell.TextLabel.Text = controller.PhoneNumbers [row];
return cell;
}
}
`Helloworld_iOS.CallHistoryController.CallHistoryDataSource.RowSelected(UIKit.UITableView, int)' is marked as an override but no suitable method found to override (CS0115)
Hi I'm trying to make a helloworld application with xamarin monotouch and I'm taking this error.Can anybody help me about this?
Thank you
My guess is that you're using the Unified API.
In which case use this (nint instead of int):
public override nint RowSelected(UITableView tableView, nint section)
Related
I have a UITableView set up with GetCell, RowsInSection, and other methods required for my UITableView. Here is my code:
using UIKit;
...
namespace ...
{
public partial class (className) : UIViewController, IUITextFieldDelegate, IUITableViewDataSource, IUITableViewDelegate
bool test = false;
public override void ViewDidLoad()
{
base.ViewDidLoad();
table.ReloadData();
table.DataSource = this;
table.Delegate = this;
test = true;
}
...
public nint RowsInSection(UITableView tableview, nint section)
{
return 2;
}
public UITableViewCell GetCell(UITableView tableView, NSIndexPath indexPath)
{
var cell = tableView.DequeueReusableCell("cell") as Cell;
bool selected = test;
cell.Setup(selected);
return cell;
}
}
What I attempt to do is checking a changed value of a variable while setting up the cell in GetCell. However, it seems that the GetCell method ignores the changed value of the variable test, so my question is how do I access the variable after the change in ViewDidLoad.
Call table.ReloadData(); after modifying test , because reloadData is used to trigger all the methods in the delegate including GetCell .
table.DataSource = this;
table.Delegate = this;
test = true;
table.ReloadData();
Your code is not for cell reuse , pls refer to the correct way here .
//register
table.RegisterClassForCellReuse (typeof(Cell), "cell");
public override UITableViewCell GetCell (UITableView tableView, NSIndexPath indexPath)
{
//cell reuse
var cell = (Cell) tableView.DequeueReusableCell ("cell", indexPath);
if (cell == null) cell = new Cell("cell");
bool selected = test;
cell.Setup(selected);
return cell;
}
I have a collection view that needs to have the cells fitting size to content.
I use sizeForItemAtIndexPath: to set size for each cell.
It's fired and works great.
Here is the problem what I am facing.
The data list for collection view is changed dynamically.
collection view has only 1 item to show and shows it in correct cell size(let's say size1)
next time collection view has 1 item to show again but it's different data.
In this case, the collection view doesn't show it in correct cell size. it shows in size1
I want to show next item in its own size but collection view keep original size.
When item count is changed, sizeForItemAtIndexPath: is fired again.
When item count is same, it's not fired.
I want it to be fired everytime.
I share some of my code.
cllSelected.Source = new SelectedItemSource(this, _selectedItems);
Above code will be called whenever there is new _selectedItems
I implemented the UICollectionViewSource as following
public class SelectedItemSource : UICollectionViewSource, IUICollectionViewDelegateFlowLayout
{
CatalogVC _ownerVC;
SelectedCategoryList _selectedItems;
public SelectedItemSource(CatalogVC ownerVC, SelectedCategoryList selectedItems)
{
_ownerVC = ownerVC;
_selectedItems = selectedItems;
}
public override nint NumberOfSections(UICollectionView collectionView)
{
return 1;
}
public override nint GetItemsCount(UICollectionView collectionView, nint section)
{
return _selectedItems != null ? _selectedItems.Count : 0;
}
public override bool ShouldHighlightItem(UICollectionView collectionView, Foundation.NSIndexPath indexPath)
{
return true;
}
public override void ItemHighlighted(UICollectionView collectionView, Foundation.NSIndexPath indexPath)
{
}
public override void ItemUnhighlighted(UICollectionView collectionView, Foundation.NSIndexPath indexPath)
{
_ownerVC.SelectedFilter_ItemClick(indexPath.Row);
}
public override UICollectionViewCell GetCell(UICollectionView collectionView, Foundation.NSIndexPath indexPath)
{
SelectedFilterCollectionCell cell = collectionView.DequeueReusableCell("SelectedFilterCollectionCell", indexPath) as SelectedFilterCollectionCell;
cell.Layer.ShouldRasterize = true;
cell.Layer.RasterizationScale = UIScreen.MainScreen.Scale;
cell.BindDataToCell(_selectedItems[indexPath.Row]);
return cell;
}
[Export("collectionView:layout:sizeForItemAtIndexPath:")]
public CoreGraphics.CGSize GetSizeForItem(UICollectionView collectionView, UICollectionViewLayout layout, Foundation.NSIndexPath indexPath)
{
var text = new NSString(_selectedItems[indexPath.Row].Text);
CGSize cellSize = new CGSize(xxx, yyy); // cellSize will be determined according to text
return cellSize;
}
}
Thanks for any solution or suggestion!
Try to call CollectionView.ReloadData() after the data changes.
I think it will invoke the methods included in the delegate.
Good evening everybody.
Case Study:
Currently I'm working on an tvos application, that needs a Netflix like vertical scroll. I found the following article that explains the approach: https://www.thorntech.com/2015/08/want-your-swift-app-to-scroll-in-two-directions-like-netflix-heres-how/
Problem:
My constructor of the class ("DashboardCollectionView.cs") isn't called, so my cells aren't intialized. In result i receive a view without any collectionviewcells, like the photo below:
Information about code and the project:
I have attached the solution as a zip file. I hope someone can help me. I'm really new on .ios so maybe it can be something easy.
http://www35.zippyshare.com/v/cTJje8WL/file.html
EDIT:
Part of Code
public partial class DashboardCollectionView : UICollectionView
{
public DashboardCollectionView (IntPtr handle) : base (handle)
{
RegisterClassForCell(typeof(DashboardCollectionViewCell), "movieCell");
DataSource = new DashboardCollectionViewDataSource();
Delegate = new DashboardCollectionViewDelegateFlowLayout();
}
}
public partial class DashboardCollectionViewCell : UICollectionViewCell
{
public DashboardCollectionViewCell (IntPtr handle) : base (handle)
{
}
}
public class DashboardCollectionViewDataSource: UIKit.UICollectionViewDataSource
{
public DashboardCollectionViewDataSource()
{
}
public override UICollectionViewCell GetCell(UICollectionView collectionView, NSIndexPath indexPath)
{
var movieCell = (DashboardCollectionViewCell)collectionView.DequeueReusableCell("movieCell", indexPath);
return movieCell;
}
public override nint GetItemsCount(UICollectionView collectionView, nint section)
{
return 12;
}
public override nint NumberOfSections(UICollectionView collectionView)
{
return 1;
}
}
public class DashboardCollectionViewDelegateFlowLayout : UIKit.UICollectionViewDelegateFlowLayout
{
public DashboardCollectionViewDelegateFlowLayout()
{
}
public override CoreGraphics.CGSize GetSizeForItem(UIKit.UICollectionView collectionView, UIKit.UICollectionViewLayout layout, Foundation.NSIndexPath indexPath)
{
var itemsPerRow = 4;
var hardCodedPadding = 10;
var itemWidth = (collectionView.Bounds.Width / itemsPerRow) - hardCodedPadding;
var itemHeight = collectionView.Bounds.Height - (2 * hardCodedPadding);
return new CoreGraphics.CGSize(itemWidth, itemHeight);
}
}
public partial class DashboardTableViewController : UITableViewController
{
private String[] categories = new String[] { "Kürzlich hinzugefügt", "Beliebt" };
private String cardCellId = "cell";
public DashboardTableViewController(IntPtr handle) : base(handle)
{
}
public override nint NumberOfSections(UITableView tableView)
{
return categories.Length;
}
public override string TitleForHeader(UITableView tableView, nint section)
{
return categories[section];
}
public override nint RowsInSection(UITableView tableView, nint section)
{
return 1;
}
public override UITableViewCell GetCell(UITableView tableView, NSIndexPath indexPath)
{
var cell = tableView.DequeueReusableCell(cardCellId, indexPath);
return cell;
}
}
The problem is this part
3. Now for the tricky part — wiring the Collection View’s DataSource and Delegate to the cell. It’s easier if you drag from the Collection View’s Connections Inspector to the Table View Cell within the view hierarchy.
I am not sure how to do that using VS story board, you probably need to use XCode. You kind of try to do it programmatically but that doesn't work.
So if working with XCode is not an option you can trick this part and give to your DashboardCollectionView a name to force its creation. But you cannot do that without creating custom class of your "Table View Cell".
You also need to comment out
//RegisterClassForCell(typeof(DashboardCollectionViewCell), "movieCell");
After those 3 steps you should be able to see your cells. I ran it on iOS 9 simulator but couldn't get any scrolling vertical or horizontal but this is a different question.
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();
}
}
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;