iOS导航到表格单元格上的新视图单击使用Segue

时间:2017-06-05 22:38:19

标签: ios objective-c iphone uitableview xamarin.ios

单击表格单元格后,我想使用segue导航到新页面。但是每当我点击单元格时都没有任何反应

当我在PrepareForSegue方法上放置一个断点时,当我点击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];
        }

    }
}

我是否需要添加DayCell的TouchUpInside事件?关于如何使其发挥作用的任何建议都会很棒。

1 个答案:

答案 0 :(得分:1)

我认为您错过了将委托和数据源连接到uitableviewcontroller中的tableview。所以连接它就能正常工作。