0

Upon clicking a table cell I would like to navigate to a new page using a segue. However whenever I click the cell nothing happens.

When I place a breakpoint on the PrepareForSegue method it is never hit when I click a cell on the DayTableView.

Main.storyboard enter image description here

DayTableViewController

namespace HHGoals2
{
    public partial class DayTableViewController : UITableViewController
    {
        HHGoals2DataService dataService = new HHGoals2DataService();

        public DayTableViewController(IntPtr handle) : base (handle)
        {
        }

        public override void ViewDidLoad()
        {
            var allDays = dataService.GetAllDays();
            var dataSource = new GoalsDataSource(allDays, this);

            TableView.Source = dataSource;

            this.NavigationItem.Title = "Completed Goals";
        }

        public override void PrepareForSegue (UIStoryboardSegue segue, NSObject sender)
        {
            base.PrepareForSegue(segue, sender);

            if(segue.Identifier == "GoalDetailSegue")
            {
                var goalDetailViewController = segue.DestinationViewController
                                                    as GoalDetailViewController;
                if(goalDetailViewController != null)
                {
                    var source = TableView.Source as GoalsDataSource;
                    var rowPath = TableView.IndexPathForSelectedRow;
                    var item = source.GetItem(rowPath.Row);
                    goalDetailViewController.SelectedDay = item;
                }

            }
        }
    }
}

GoalsDataSource

using System;
using Foundation;
using UIKit;
using System.Collections.Generic;
using HHGoals2.Core.Model;
using HHGoals2.Cells;

namespace HHGoals2.DataSources
{
    public class GoalsDataSource: UITableViewSource
    {
        private List<Day> allDays;
        NSString cellIdentifier = new NSString("DayCell");
        DayTableViewController callingController;

        public GoalsDataSource(List<Day> allDays, DayTableViewController callingController)
        {
            this.allDays = allDays;
            this.callingController = callingController;
        }

        public override UITableViewCell GetCell(UITableView tableView, NSIndexPath indexPath)
        {
            DayListCell cell = tableView.DequeueReusableCell(cellIdentifier) as DayListCell;

            if (cell == null)
            {
                cell = new DayListCell(cellIdentifier);
            }


            cell.UpdateCell(allDays[indexPath.Row].Number.ToString(),
                            allDays[indexPath.Row].SuccessRate.ToString(),
                            allDays[indexPath.Row].FailRate.ToString());

            return cell;
        }

        public override nint RowsInSection(UITableView tableview, nint section)
        {
            return allDays.Count;
        }

        public Day GetItem(int id)
        {
            return allDays[id];
        }

    }
}

Do I need to add a TouchUpInside event of the DayCell? Any suggestions on how to get this to work would be great.

RyeGuy
  • 4,213
  • 10
  • 33
  • 57
  • 1
    try checking [his answer](https://stackoverflow.com/questions/22759167/how-to-make-a-push-segue-when-a-uitableviewcell-is-selected) – carlos salzar Jun 05 '17 at 23:02
  • Thx for the suggestion. According to the second answer mine should work fine. Obviously it does not so kind of at a loss . . . . – RyeGuy Jun 06 '17 at 00:23
  • 1
    This is your complete `DayTableViewController` ? where's are you setting the data or returning the created view `GetView` methods? – pinedax Jun 06 '17 at 00:52
  • The data is in the dataService (allDays variable) and the RowsInSection and GetCell methods are overridden in the GoalsDataSource (dataSource variable) – RyeGuy Jun 06 '17 at 01:15
  • I don't think it should matter but I added the code for the GoalsDataSource above – RyeGuy Jun 06 '17 at 01:18
  • 1
    The question was because when using UITableViewController you don't need to use DataSource. Generally you implement those methods in the same UITableViewController file. – pinedax Jun 06 '17 at 01:50
  • 1
    Make sure that when you Ctrl+Drag to create the Segue you did it from the Prototype Cell and not from the ViewController and that you chose one of the "Selection Segue" section. – pinedax Jun 06 '17 at 01:55
  • Yes I did make sure of that. It still is not acting appropriately – RyeGuy Jun 06 '17 at 01:57
  • 1
    Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/145928/discussion-between-apineda-and-ryeguy). – pinedax Jun 06 '17 at 01:58

1 Answers1

1

I think you just missed out to connect the delegate and datasource to tableview in uitableviewcontroller. So connect it and it will work fine.

sumeet
  • 157
  • 1
  • 5
  • I believe you may be correct. However I am under the impression I already did that. Could you provide a code example? If it works I'll be sure to give you a green check – RyeGuy Jun 06 '17 at 19:26
  • self.tableView.delegate = self self.tableView.dataSource = self – sumeet Jun 07 '17 at 05:51