uitableview return 2 cellIdentifier - ios

I have two cells:
like taskcella, taskcellb
but it dynamically returns only 1 cell data
is there possible to add and return multiple cellidentifiers?
public override UITableViewCell GetCell (UITableView tableView, MonoTouch.Foundation.NSIndexPath indexPath)
{
// in a Storyboard, Dequeue will ALWAYS return a cell,
UITableViewCell cell = tableView.DequeueReusableCell (cellIdentifier);
cell.TextLabel.Text = "Görev : " + tableItemsX[indexPath.Row].Gorev;
UITableViewCell celld = tableView.DequeueReusableCell (cellIdentifierd);
celld.TextLabel.Text = "İşlem : " + tableItemsX[indexPath.Row].Gorev;
return celld; return cell;
}
this is code which return first, returns first

First of all make sure that in you TableViewSource you override method RowsInSection and it's returns 2
public override int RowsInSection(UITableView tableview, int section)
{
return 2;
}
Then rewrite you GetCell method in the next way
public override UITableViewCell GetCell (UITableView tableView, MonoTouch.Foundation.NSIndexPath indexPath)
{
if (indexPath.Row == 0)
{
UITableViewCell cell = tableView.DequeueReusableCell (cellIdentifier);
cell.TextLabel.Text = "Görev : " + tableItemsX[indexPath.Row].Gorev;
return cell;
}
else
{
UITableViewCell celld = tableView.DequeueReusableCell (cellIdentifierd);
celld.TextLabel.Text = "İşlem : " + tableItemsX[indexPath.Row].Gorev;
return celld;
}
}

Related

GetCell method ignores changed values

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;
}

Swipe to delete cell Height is decrease every time in xamarin ios

I am new to iOS. I am creating TableView with swipe to delete the cell. But When I swipe every time the cell height is decrease. I am using iOS 10. The below code I have used.
Code :
class AppointmentSourceClass : UITableViewSource
{
List<AppointmentItem> appointments;
public AppointmentSourceClass(List<AppointmentItem> appointments)
{
this.appointments = appointments;
}
public override UITableViewCell GetCell(UITableView tableView, NSIndexPath indexPath)
{
ApointListCell cell = tableView.DequeueReusableCell(ApointListCell.Key) as ApointListCell ?? ApointListCell.Create();
var item = appointments[indexPath.Row];
cell.BindData(item);
cell.Layer.MasksToBounds = false;
cell.Layer.CornerRadius = 5.0f;
cell.BackgroundColor = UIColor.White;
cell.SelectionStyle = UITableViewCellSelectionStyle.None;
return cell;
}
public override void CommitEditingStyle(UITableView tableView, UITableViewCellEditingStyle editingStyle, NSIndexPath indexPath)
{
switch (editingStyle)
{
case UITableViewCellEditingStyle.Delete:
appointments.RemoveAt(indexPath.Row);
tableView.DeleteRows(new NSIndexPath[] { indexPath},UITableViewRowAnimation.Fade);
break;
case UITableViewCellEditingStyle.None:
break;
}
}
public override bool CanEditRow(UITableView tableView, NSIndexPath indexPath)
{
return true;
}
public override string TitleForDeleteConfirmation(UITableView tableView, NSIndexPath indexPath)
{
return "Trash ( ";
}
public override nint RowsInSection(UITableView tableview, nint section)
{
return appointments.Count;
}
}
}
Output :
Before Swipe Image :
After Swipe Image :
Found the solution.
In the TableCell I have use below code to seperate the two tableCell and make space between them. If i remove the code then it working fine.
public override CoreGraphics.CGRect Frame
{
get
{
return base.Frame;
}
set
{
value.Y += 4;
value.Height -= 2 * 4;
base.Frame = value;
}
}

TextField Change event in a CustomCell

I have been working on CustomCell in Xamarin.iOS platform. The following code is working fine. I have tableItems which is List that stores Start and End values.
And I also have a class(SettingCustomCell) where I have programmatically created TextFields to display Start and End values.
I wonder how could I detect/capture when user changes either Start or End values in the TextField in my current implementation.
MainTableViewController
tableItems.Add (new TableItem() {Start=1000, End=4000});
tableItems.Add (new TableItem() {Start=4000, End=6000});
public override UITableViewCell GetCell (UITableView tableView, NSIndexPath indexPath)
{
SettingsCustomCell cell = tableView.DequeueReusableCell (cellIdentifier) as SettingsCustomCell;
if (cell == null) {
cell = new SettingsCustomCell (cellIdentifier);
}
cell.UpdateCell (tableItems [indexPath.Row].Start, tableItems [indexPath.Row].End);
return cell;
}
SettingsCustomCell
public CustomCell (NSString cellId) : base (UITableViewCellStyle.Default, cellId)
{
SelectionStyle = UITableViewCellSelectionStyle.None;
firstLabel = new UITextField ();
secondLabel = new UITextField ();
ContentView.Add (firstLabel);
ContentView.Add (secondLabel);
}
public void UpdateCell (int caption, int subtitle)
{
firstLabel.Text = caption.ToString();
secondLabel.Text = subtitle.ToString();
}
Create the eventhandler in the tablecell. And when the your UITextField text change call this eventhandler.
public EventHandler<bool> EditingChanged;
//some code here
firstTextField.ValueChanged += (s, e) => {
if(EditingChanged!=null)
EditingChanged(this,true);
}
Than in your DataSource you subscribe for EditingChanged event and create another eventhandler which will be called when the EditingChanged will be executed.
public EventHandler<bool> SourceEditingChanged;
public override UITableViewCell GetCell (UITableView tableView, NSIndexPath indexPath)
{
SettingsCustomCell cell = tableView.DequeueReusableCell (cellIdentifier) as SettingsCustomCell;
if (cell == null) {
cell = new SettingsCustomCell (cellIdentifier);
}
cell.UpdateCell (tableItems [indexPath.Row].Start, tableItems [indexPath.Row].End);
cell.EditingChanged += {
if(SourceEditingChanged!=null)
SourceEditingChanged(this,true);
};
return cell;
}
Subscribe for your SourceEditingChange Event from viewController.
MySource.SourceEditingChange += (s,e) =>{
//Your Code here
}

Return different cell for last row of a UITableView

My app uses a UITableview to display a feed of images. The API only gives so many images at once so I want the final cell in the tableview to be small cell that has a Load More Button. To do this I created a custom cell called LoadMoreCell with an identifier LoadCell
Using the GetCell method I do:
public override UITableViewCell GetCell (UITableView tableView, MonoTouch.Foundation.NSIndexPath indexPath)
{
// request a recycled cell to save memory
FeedCell cell = tableView.DequeueReusableCell (cellIdentifier) as FeedCell;
if(indexPath.Row >= tableItems.Count)
{
LoadMoreCell loadCell = tableView.DequeueReusableCell ("LoadCell") as LoadMoreCell;
return loadCell;
}
//Set Date and title
cell.SetInfo (ids[indexPath.Row], userID, tableItems [indexPath.Row]);
cell.SetImage (images[indexPath.Row]);
cell.parent = this.parent;
return cell;
}
This gives me a cell at the bottom with the Load More Button, but the cell is the same dimensions as a FeedCell not the smaller dimensions of a LoadMoreCell
Anyone know what my problem might be with this?
You may need to override GetHeightForRow in your UITableViewSource
public override float GetHeightForRow (UITableView tableView, MonoTouch.Foundation.NSIndexPath indexPath)
{
if (indexPath.Row >= tableItems.Count) {
return loadMoreHeight;
} else {
return regularRowHeight;
}
}
UITableViewDelegate has a method tableView:heightForRowAtIndexPath: You can return the height for the cell there.

Editable UITableViewCell - EditingStyleForRow() not invoked

I am trying to implement deletable UITableViewCells. My UITableView has 3 sections. The 1st section contains one cell for every element of a List (called items for simplicity). Various elements (UILabels and a UIButton) are added to each cell.
Like most articles on the web, my code implements the delegate methods that are in charge of making cells editable:
public override bool CanEditRow(UITableView tableView, NSIndexPath indexPath) {
if (indexPath.Section == 0 && items.Count > 0) {
return true;
}
return false;
}
public override UITableViewCellEditingStyle EditingStyleForRow(UITableView tableView, NSIndexPath indexPath) {
if (indexPath.Section == 0 && items.Count > 0) {
return UITableViewCellEditingStyle.Delete;
}
return UITableViewCellEditingStyle.None;
}
public override void CommitEditingStyle(UITableView tableView, UITableViewCellEditingStyle editingStyle, NSIndexPath indexPath) {
if (editingStyle == UITableViewCellEditingStyle.Delete) {
items.RemoveAt(indexPath.Row);
tableView.DeleteRows(new NSIndexPath[] { indexPath }, UITableViewRowAnimation.None);
}
}
Whether or not a cell is editable/deletable is decided in GetCell():
public override UITableViewCell GetCell(UITableView tableView, NSIndexPath indexPath) {
string cellIdentifier;
UITableViewCell cell;
if (indexPath.Section == 0) {
if (items.Count == 0) {
// Generate a "No data here..." cell
...
} else {
cellIdentifier = "ItemCell";
cell = tableView.DequeueReusableCell(cellIdentifier);
if (cell == null) {
// Create a new cell
...
}
cell.Editing = true; // Make the cell deletable
cell.ShouldIndentWhileEditing = true;
cell.ShowingDeleteConfirmation = true;
// Assign item values to the cell
...
}
} else if (...) { // Other sections go here
}
return cell;
}
Placing Console.Writes and breakpoints all over the place made me believe that the EditingStyleForRow() delegate method is not even invoked. None of it's Console.Writes showed up, no breakpoint in there was ever reached.
All the other delegates work fine though. Table contents show up as expected, just not editable/deletable.
Any hints on what I am doing wrong? Thanks in advance.
EDIT:
Apple docs say that UITableViewCellEditingStyle.Delete will be set for editable cells if the delegate method is not implemented. But it doesn't work without the method either.
EDIT 2:
Thanks to Ratinhos hint, EditingStyleForRow() is now invoked and correctly returns UITableViewCellEditingStyle.Delete, but the delete controls still don't appear.
did you invoke:
[tableView setEditing:animated:]
?
It works now. I completely removed those:
cell.Editing = true; // Make the cell deletable
cell.ShouldIndentWhileEditing = true;
cell.ShowingDeleteConfirmation = true;
Also, CanEditRow() doesn't really seem to be required. All that is left is tableView.Editing = true and the implementations of EditingStyleForRow() and CommitEditingStyle().

Resources