Custom UITableViewCell

AdamLonsdale.3550AdamLonsdale.3550 GBUniversity ✭✭
edited November 2014 in Xamarin University

Hi All,

I've been playing around with the FieldService project to get my head around some of the UITableView stuff ahead of the certification exam, and I've stumbled across a problem I can't seem to resolve. I want to change one of the tables to implement my own custom style.

I've changed the storyboard to use a custom style and laid out the default cell as I plan on having it appear as you can see below. But whenever I run the project I always get a blank list of cells. Also when I step through the code, the actual controls in my cell are set to null which I assume is part of the problem. I've copied my code below. Any ideas on what could be causing this?

Custom Table Cell:

` partial class cellDocument : UITableViewCell
{

    public cellDocument(string identifier) : base(UITableViewCellStyle.Default, identifier)
    {

    }

    public UIImage DocumentIcon { 
        get { return imgIcon.Image; } 
        set {  if (imgIcon != null)
            imgIcon.Image = value;} 
    }

    public string DocumentName { 
        get { return lblDocumentName.Text; } 
        set {  if (lblDocumentName != null)
            lblDocumentName.Text = value;} 
    }

    public string DocumentDate { 
        get { return lblDocumentDate.Text; } 
        set {  if (lblDocumentDate != null)
            lblDocumentDate.Text = value;} 
    }

    public string DocumentUser { 
        get { return lblDocumentUser.Text; } 
        set {  if (lblDocumentUser != null)
            lblDocumentUser.Text = value;} 
    }
}`

UITableViewSource:
` private class DocumentsTableSource : UITableViewSource

    {
        //readonly DocumentViewModel documentViewModel;
        UIDocumentInteractionController documentController;
        const string Identifier = "DocumentCell";
        List<clsDocument> _lstDocs;

        public DocumentsTableSource (List<clsDocument> lstDocs, UITableView TableView)
        {
            _lstDocs = lstDocs;
        }

        public override int RowsInSection (UITableView tableview, int section)
        {
            return _lstDocs.Count;
        }

        public override void RowSelected (UITableView tableView, NSIndexPath indexPath)
        {
            tableView.DeselectRow (indexPath, true); // normal iOS behaviour is to remove the blue highlight
        }

        public override UITableViewCell GetCell(UITableView tableView, NSIndexPath indexPath)
        {  
            try{
                cellDocument cell = (cellDocument)tableView.DequeueReusableCell (Identifier);
                if (cell == null)
                    cell = new cellDocument(Identifier);

                cell.DocumentName = _lstDocs[indexPath.Row].FullDesc;
                cell.DocumentDate = _lstDocs[indexPath.Row].DateProduced.ToShortDateString();
                cell.DocumentUser = _lstDocs[indexPath.Row].FileName;
                cell.SetNeedsLayout();
                return cell;
            }
            catch (Exception e){
                System.Diagnostics.Debugger.Break ();
                return null;
            }
        } 
    }`

Loading the data for the table:

`
var list = new List{};

                    JsonSerializer serializer = new JsonSerializer();
                    list = (List<clsDocument>)serializer.Deserialize (new JTokenReader ((JToken)arResponse.data), typeof(List<clsDocument>));
                    list = (from chItem in list orderby chItem.FullDesc ascending select chItem).ToList();
                    InvokeOnMainThread (() => {

                        var tableData = new DocumentsTableSource(list, this.tableView);
                        this.tableView.RowHeight = 80;
                        this.tableView.Source = tableData;
                        tableView.ReloadData();
                    });`

Posts

  • AdamLonsdale.3550AdamLonsdale.3550 GBUniversity ✭✭

    So in my infinite wisdom I failed to notice that I needed to set the Reuse Identifier in my storyboard. After setting that my problem went away (after adding an intPtr overload to my UITableViewCell)

  • nick5454nick5454 USUniversity ✭✭✭

    It's even easier to do it completely without code :)

    new UITableView(this.View);

    you handle the events exactly the same, but you don't have all the complications of the UI getting in the way.

Sign In or Register to comment.