Xamarin.iOS - Multiple XIBs in a single UITableView - xamarin

I am trying to load different nibs into a single UITableView depending on a variable within a model. I seem to have something that looks logical and doesn't crash, however, only 1 of the xibs is being loaded and displayed.
Controller Method:
private void populateTableData()
{
liveTipsTableView.RegisterNibForCellReuse(UINib.FromName("LiveTipCell_", null), "LiveTipCell_");
liveTipsTableView.RegisterNibForCellReuse(UINib.FromName("NewsCell_", null), "NewsCell_");
setListViewSource();
Refresh();
AddRefreshControl();
Add(liveTipsTableView);
liveTipsTableView.Add(RefreshControl);
}
TableSource Method
public override UITableViewCell GetCell(UITableView tableView, NSIndexPath indexPath)
{
var localTip = _tips[indexPath.Row];
if (localTip.NewsItem)
{
CellIdentifier = new NSString("NewsCell_");
NewsCell_ cell = new NewsCell_();
cell = tableView.DequeueReusableCell("NewsCell_") as NewsCell_;
var views = NSBundle.MainBundle.LoadNib("NewsCell_", cell, null);
cell = ObjCRuntime.Runtime.GetNSObject(views.ValueAt(0)) as NewsCell_;
cell.SelectionStyle = UITableViewCellSelectionStyle.None;
cell.BindDataToCell(localTip);
return cell;
}
else
{
CellIdentifier = new NSString("LiveTipCell_");
LiveTipCell_ cell = new LiveTipCell_();
cell = tableView.DequeueReusableCell("LiveTipCell_") as LiveTipCell_;
var views = NSBundle.MainBundle.LoadNib("LiveTipCell_", cell, null);
cell = ObjCRuntime.Runtime.GetNSObject(views.ValueAt(0)) as LiveTipCell_;
cell.SelectionStyle = UITableViewCellSelectionStyle.None;
cell.BindDataToCell(localTip);
return cell;
}
}
I have 2 separate Xib files and they have their own classes which work to populate the views. They all seem to getting used when debugging, it's just a case of the table view is only displaying 1 of these items.
Thanks in advance and please let me know if you need more information to see what's going on.

This is in objective C, but it shouldn't be too hard to convert.
SO Post on a different question

Related

How to insert new cell at first element in UICollectionView in Xamarin?

I want to make horizontal collection and add new cell at the first position.
I make custom UICollectionViewFlowLayout and override
LayoutAttributesForElementsInRect like this (to see reverse order of items):
public override UICollectionViewLayoutAttributes[] LayoutAttributesForElementsInRect(CGRect rect)
{
var attributes = new UICollectionViewLayoutAttributes[cellCount];
NSIndexPath indexPath;
// Setting first position
int shift = 25, reverseXCenter;
for (int i = 0; i < cellCount; i++)
{
indexPath = NSIndexPath.FromItemSection(i, 0);
attributes[i] = LayoutAttributesForItem(indexPath);
// We have to make opposite order
reverseXCenter = (cellCount -1 - i) * 50;
attributes[i].Center = new CGPoint(reverseXCenter + shift, attributes[i].Center.Y);
}
return attributes;
}
I think is bad solution, because when I have more than 5 cells, the displayed collection is filled and I can't scroll.
I had debugger in GetOrCreateCellFor in CollectionViewSource
and indexPath.Item's had value for example (10,9,8,7,6...), where 10 is the last I added.
Maybe is another way to do this, or just I miss something?
Ok I am gonna help you out with a flow .
Suppose you are in a ViewController called "A" which will present a controller ViewController called "B"
Inside A use this function to push B
Public void PushNewHorizontalCollectionController(){
using (var vc =
new B(this.PageViewControllerLayout(), indexPath))
{
this.controller.PushViewController(pageViewController, true);
}
}
public UICollectionViewFlowLayout PageViewControllerLayout()
{
var flowLayout = new UICollectionViewFlowLayout();
var itemSize = this.controller.NavigationBarHidden ? new CGSize(Constants.ScreenWidth, Constants.ScreenHeight + 20) : new CGSize(Constants.ScreenWidth, Constants.ScreenHeight - Constants.NavigationHeaderAndStatusbarHeight);
flowLayout.ItemSize = itemSize;
flowLayout.MinimumLineSpacing = 0;
flowLayout.MinimumInteritemSpacing = 0;
flowLayout.ScrollDirection = UICollectionViewScrollDirection.Horizontal;
return flowLayout;
}
Here itemSize is CGSize which u can assign what size you can ignore my constants which are self explainable .
Inside B your constructor should look like this .
public HorizontalViewController(UICollectionViewLayout layout, NSIndexPath indexpath) : base(layout)
{
Do your initialisation like registering UICollectionViewCell class nibs for use with this Controller
}
Handling real requirement from comment .
You wanna add something always as 1st element in collectionView ,Go to this method which should look like below
public override UICollectionViewCell GetCell(UICollectionView collectionView, NSIndexPath indexPath)
{
Console.WriteLine("getcellHori");
var isQuoteType = Photo.PhotoCollection[(int)indexPath.Item].Type;
if (isQuoteType == 3 || isQuoteType == 4)
{
var item = Photo.PhotoCollection[(int)indexPath.Item];
var cell = (HorizontalviewForQuoteType)collectionView.DequeueReusableCell("HorizontalviewForQuoteType", indexPath);
cell.PopulateCell(item.Caption,item.TextLabel,item.Likes,item.TimeLabel,item.Id);
HorizontalviewForQuoteType.pullAction = (offset) =>
{
this.pullOffset = offset;
this.NavigationController.PopViewController(true);
};
cell.SetNeedsLayout();
return cell;
}
Here you this line
var item = Photo.PhotoCollection[(int)indexPath.Item];
this is the data source , so whenever you adding something at index first and everything will be solved .

How to drag and drop rows in NSTableView in Mac

I have struck with one point i.e now I have got a list of data using NSTableView but what my requirement is, able to drag and drop that rows from one row position to another row position. please give any suggestion for get out this problem. Thanks in advance.
My sample code
Within your NSTableViewDataSource subclass implement WriteRows, ValidateDrop and AcceptDrop and register which Drag/Drop targets your NSTableView accepts. In this case you are only accepting Drops from within your own NSTableView.
Assign a name that will be used for valid drag operations on this NSTableView:
// Any name can be registered, I find using the class name
// of the items in the datasource is cleaner than a const string
string DragDropType = typeof(Product).FullName;
Register the drag types for your NSTableView:
ProductTable.RegisterForDraggedTypes(new string[] { DragDropType });
Implement drag/drop methods on your NSTableViewDataSource:
public override bool WriteRows(NSTableView tableView, NSIndexSet rowIndexes, NSPasteboard pboard)
{
var data = NSKeyedArchiver.ArchivedDataWithRootObject(rowIndexes);
pboard.DeclareTypes(new string[] { DragDropType }, this);
pboard.SetDataForType(data, DragDropType);
return true;
}
public override NSDragOperation ValidateDrop(NSTableView tableView, NSDraggingInfo info, nint row, NSTableViewDropOperation dropOperation)
{
tableView.SetDropRowDropOperation(row, dropOperation);
return NSDragOperation.Move;
}
public override bool AcceptDrop(NSTableView tableView, NSDraggingInfo info, nint row, NSTableViewDropOperation dropOperation)
{
var rowData = info.DraggingPasteboard.GetDataForType(DragDropType);
if (rowData == null)
return false;
var dataArray = NSKeyedUnarchiver.UnarchiveObject(rowData) as NSIndexSet;
Console.WriteLine($"{dataArray}");
// Move hack for this example... you need to handle the complete NSIndexSet
tableView.BeginUpdates();
var tmpProduct = Products[(int)dataArray.FirstIndex];
Products.RemoveAt((int)dataArray.FirstIndex);
if (Products.Count == row - 1)
Products.Insert((int)row - 1 , tmpProduct);
else
Products.Insert((int)row, tmpProduct);
tableView.ReloadData();
tableView.EndUpdates();
return true;
}

Disabled button in UITableViewCell Xamarin.iOs

I have UITableView and each cell has button and text. Actual cell shouldn't be clickable, but button should. When I run in simulator I can hit button, but when deploy to device it is disabled. (edited)
Tried to follow many different steps to fix it from this post Button in UITableViewCell not responding under ios 7, such as ContentView.UserInteractionEnabled = False; for cell.
All of those for native iOS, maybe for Xamarin there are something else?
Any ideas what am I missing?
UITableViewCell code
this.DelayBind(() =>
{
var set = this.CreateBindingSet<CalendarCell, ActivityModel>();
set.Bind(CustomerNameLabel).To(a => a.Subject);
set.Bind(MarkAsMeetingButton).For(a => a.Hidden).WithConversion("ActivityTypeToVisibility");
set.Bind(MarkAsMeetingButton).To(a => a.SendMessageToViewModelCommand).CommandParameter(BindingContext);
set.Apply();
});
ContentView.UserInteractionEnabled = false;
UItableView code
public override void ViewDidLoad()
{
base.ViewDidLoad();
var source = new TableSource(CalendarList);
var set = this.CreateBindingSet<CalendarView, CalendarViewModel>();
set.Bind(source).To(vm => vm.CalendarList);
set.Apply();
CalendarList.Source = source;
CalendarList.ReloadData();
}
public class TableSource : MvxSimpleTableViewSource
{
private List<IGrouping<DateTime, ActivityModel>> GroupedCalendarList = new List<IGrouping<DateTime, ActivityModel>>();
public TableSource(UITableView calendarView) : base(calendarView, "CalendarCell", "CalendarCell")
{}
public override UITableViewCell GetCell(UITableView tableView, NSIndexPath indexPath)
{
var currentSection = GroupedCalendarList[indexPath.Section].ToList();
var item = currentSection[indexPath.Row];
var cell = GetOrCreateCellFor(tableView, indexPath, item);
var bindable = cell as IMvxDataConsumer;
if (bindable != null)
bindable.DataContext = item;
return cell;
}
public override nint RowsInSection(UITableView tableview, nint section)
{
return (nint)GroupedCalendarList[(int)section].Count();
}
public override UIView GetViewForHeader(UITableView tableView, nint section)
{
var label = new UILabel();
var currentDate = DateTime.Now;
var titleText = GroupedCalendarList[(int)section].FirstOrDefault().ScheduledStart.Value.Date;
return label;
}
public override nint NumberOfSections(UITableView tableView)
{
return (nint)GroupedCalendarList.Count;
}
public override void ReloadTableData()
{
if (ItemsSource == null) return;
var groupedCalendarList = (ItemsSource as List<ActivityModel>).GroupBy(cl => cl.ScheduledStart.Value.Date).ToList();
GroupedCalendarList = new List<IGrouping<DateTime, ActivityModel>>(groupedCalendarList);
base.ReloadTableData();
}
}
This code works absolutely fine for simulator, but doesn't work on device, UIButton in each cell is disabled.
As #Luke said in the comments of your question said, do not use ContentView.UserInteractionEnabled = false;, since this disables any touch events on the whole view of your cell.
To achieve what you need, implement the UITableViewDelegate method ShouldHighlightRow and return false:
public override bool ShouldHighlightRow(UITableView tableView, NSIndexPath rowIndexPath) {
return false;
}
Then, the cells will not get highlighted on tap, and the RowSelected method will not get called, but your button will be clickable!

Xamarin.ios CollectionView Delete and Insert Items

I am currently developing an iphone app with xamarin in visual studio. I'm a beginner..
I use a CollectionView. This has a list and includes 8 items with different ID's in the CollectionViewSource.cs:
ProductCategoryList.Add (new ProductCategorys () {Categoryid = 1, ProductCategoryName = "testname", ProductCategoryImage = UIImage.FromFile ( "productcat / maincat / testpic.jpg")});
Now I would like, if an item is pressed, delete All Items and refill it.
In the ProductCategoryCollectionDelegate.cs i have:
Public override Boolean ShouldSelectItem (UICollectionView collectionView, NSIndexPath indexPath)
For the delete and insert I have found these 2 methods
Public void DeleteItems (NSIndexPath [] indexPaths)
Public void InsertItems (NSIndexPath [] indexPaths)
(The xamarin Documentation is very scarce)
How can I use this 2 metodes to completely empty the list and add new items?
thanks for help
I've taken a TableView instead of the CollectionView.
With this function it is possible to directly delete items in the Tablesource class:
public override void CommitEditingStyle(UITableView tableView,
UITableViewCellEditingStyle editingStyle,
Foundation.NSIndexPath indexPath)
{
switch (editingStyle)
{
case UITableViewCellEditingStyle.Delete:
AppDelegate.DAUtil.setBasketItemStatusByBasketID(
basketitems[indexPath.Row].ZBASKETID, "2");
// remove the item from the underlying data source
basketitems.RemoveAt(indexPath.Row);
// delete the row from the table
tableView.DeleteRows(new NSIndexPath[] { indexPath },
UITableViewRowAnimation.Fade);
break;
case UITableViewCellEditingStyle.None:
Console.WriteLine("CommitEditingStyle:None called");
break;
}
}
public override bool CanEditRow(UITableView tableView,
NSIndexPath indexPath)
{
return true;
// return false if you wish to disable editing
// for a specific indexPath or for all rows
}
The Source of the Table is a List. Alternatively, you can reload the TableSource with:
tablename.ReloadData();

Multiple Custom Cell's in a ListView (Cross Platform)

Currently with ListView's I've only found that you can create a template for cells, which makes each cell look exactly the same. You can't have multiple custom cells in the listview. There are work-arounds like hiding the content in the cell depending on the content, but this seems pretty hacky.
The reason I want to use a listview over a tableview is because we plan on doing inserts, deletions, dynamically showing certain cells, and listview's can be binded to a data source.
Create your own ViewCell which overrides binding context change method. When the binding changes set the ViewCell's view to one that matches the type of view model and also set the height of the cell. Below is a quick sample that should give you an idea how to accomplish it.
public class DataTemplateCell1 : ViewCell
{
protected override void OnBindingContextChanged()
{
var vm1 = this.BindingContext as ViewModel1;
if (vm1 != null)
{
this.View = new View1() { HeightRequest = 40 };
this.Height = this.View.HeightRequest;
return;
}
var vm2 = this.BindingContext as ViewModel2;
if (vm2 != null)
{
this.View = new View2() { HeightRequest = 80 };
this.Height = this.View.HeightRequest;
return;
}
base.OnBindingContextChanged();
}
}

Resources