2017-06-05 80 views
0

單擊表格單元格後,我想使用segue導航到新頁面。但是,無論何時單擊單元格都不會發生任何事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

嘗試檢查[他的答案](https://stackoverflow.com/questions/22759167/how-to-make-a-push-segue-when-a-uitableviewcell-is-selected) –

+0

Thx的建議。根據第二個答案,我的工作應該很好。顯然它並沒有那麼茫然。 。 。 。 – RyeGuy

+1

這是你完整的'DayTableViewController'嗎?你在哪裏設置數據或返回創建的視圖'GetView'方法? – apineda

回答

1

我想你錯過了將委託和數據源連接到uitableviewcontroller中的tableview。所以連接它,它會正常工作。

+0

我相信你可能是對的。不過,我的印象我已經這樣做了。你能提供一個代碼示例嗎?如果它有效,我一定會給你一個綠色檢查 – RyeGuy

+0

self.tableView.delegate = self self.tableView.dataSource = self – sumeet

相關問題