2015-10-23 25 views
0

我已經子類UITableView與所需的方法重寫在子類中填充表視圖。但是,NumberOfSections被調用時,不會調用NumberOfRowsInSectionUITableView子類一些重寫的​​方法不被稱爲

下面的代碼來自Xamarin項目,但這與其等同的本地版本沒有什麼不同,方法名稱略有不同。

partial class ShootsTable : UITableView, IUISearchResultsUpdating, IUISearchBarDelegate 
{ 
    public bool History { get; set; } 
    public UIViewController Parent { get; set; } 

    private Bootleg.API.Event[] Events { get; set; } 
    private List<nint> ExpandedSections { get; } 

    public ShootsTable (IntPtr handle) : base (handle) 
    { 
     ExpandedSections = new List<nint>(); 
     SetEvents(); 
    } 

    private void SetEvents() 
    { 
     if (History) { 
      Events = AppDelegate.Api.GetShootHistory().ToArray(); 
     } else { 
      Events = AppDelegate.Api.MyEvents.ToArray(); 
     } 
    } 

    private void AddRefreshControl() 
    { 
     var refreshControl = new UIRefreshControl(); 
     refreshControl.AttributedTitle = new NSAttributedString ("Pull to refresh"); 
     refreshControl.AddTarget (async delegate(object sender, EventArgs e) { 
      await AppDelegate.Api.RefreshEvents(); 
      SetEvents(); 
      ReloadData(); 
      refreshControl.EndRefreshing(); 
     }, UIControlEvent.ValueChanged); 

     AddSubview (refreshControl); 
    } 

    private void AddSearchControl() 
    { 
     var searchControl = new UISearchController (Parent); 
     searchControl.SearchResultsUpdater = this; 
     searchControl.SearchBar.Delegate = this; 
     TableHeaderView = searchControl.SearchBar; 
    } 

    public void UpdateSearchResultsForSearchController (UISearchController searchController) 
    { 
     SetEvents(); 
     Events = Events.Where (e => e.name.Contains (searchController.SearchBar.Text)).ToArray(); 
     ReloadData(); 
    } 

    public override nint NumberOfSections() 
    { 
     if (Events != null && Events.Length > 0) 
     { 
      SeparatorStyle = UITableViewCellSeparatorStyle.SingleLine; 
      return Events.Length; 
     } 

     var label = new UILabel (new CGRect (Bounds.X, Bounds.Y, Bounds.Size.Width, Bounds.Size.Height)); 
     label.Text = History ? "You have not contributed to any shoots yet." : "No shoots available."; 
     label.Lines = 2; 
     label.TextAlignment = UITextAlignment.Center; 

     BackgroundView = label; 
     SeparatorStyle = UITableViewCellSeparatorStyle.None; 

     return 0; 
    } 

    public override nint NumberOfRowsInSection (nint section) 
    { 
     var e = Events[section]; 

     if (e.group == null) 
     { 
      return 1; 
     } 

     return ExpandedSections.Contains (section) ? e.events.Count : 0; 
    } 

    [Export ("tableView:heightForHeaderInSection:")] 
    public System.nfloat GetHeightForHeader (UIKit.UITableView tableView, System.nint section) 
    { 
     return Events [section].group == null ? 0 : tableView.SectionHeaderHeight; 
    } 

    [Export ("tableView:viewForHeaderInSection:")] 
    public UIKit.UIView GetViewForHeader (UIKit.UITableView tableView, System.nint section) 
    { 
     var e = Events [section]; 

     if (e.group == null) 
     { 
      return null; 
     } 

     var cell = (ShootGroupCell) tableView.DequeueReusableCell (ShootGroupCell.Key); 

     cell.Event = e; 

     cell.AddGestureRecognizer (new UITapGestureRecognizer(() => { 
      if (ExpandedSections.Contains(section)) 
      { 
       ExpandedSections.Remove(section); 
       tableView.ReloadSections(new NSIndexSet((nuint) section), UITableViewRowAnimation.Automatic); 
      } else { 
       ExpandedSections.Add(section); 
       tableView.ReloadSections(new NSIndexSet((nuint) section), UITableViewRowAnimation.Automatic); 
      } 
     })); 

     return cell.ContentView; 
    } 

    public override UITableViewCell CellAt (NSIndexPath ns) 
    { 
     var e = Events[ns.Section]; 

     e = e.group == null ? e : e.events[ns.Row]; 

     if (History) { 
      var cell = (ShootCell)DequeueReusableCell (ShootCell.Key); 
      cell.Event = e; 
      cell.Parent = Parent; 
      return cell; 
     } else { 
      var cell = (ShootJoinCell) DequeueReusableCell (ShootJoinCell.Key); 
      cell.Event = e; 
      return cell; 
     } 
    } 

    [Export ("tableView:heightForRowAtIndexPath:")] 
    public System.nfloat GetHeightForRow (UIKit.UITableView tableView, Foundation.NSIndexPath indexPath) 
    { 
     return Events [indexPath.Section].group == null || ExpandedSections.Contains (indexPath.Section) ? tableView.RowHeight : 0; 
    } 

    [Export ("tableView:didSelectRowAtIndexPath:")] 
    public async void RowSelected (UIKit.UITableView tableView, Foundation.NSIndexPath indexPath) 
    { 
     if (!History) 
     { 
      var e = Events [indexPath.Section]; 

      if (e.group != null) { 
       e = e.events [indexPath.Row]; 
      } 

      MBHUDView.HudWithBody ("Connecting Event...", MBAlertViewHUDType.ActivityIndicator, 0, true); 
      await AppDelegate.Api.ConnectToEvent (e, false); 
      MBHUDView.DismissCurrentHUD(); 

      if (AppDelegate.Api.CurrentEvent.id != e.id) 
      { 
       var alert = UIAlertController.Create ("Confirm", "This event requires you to confirm you wish to join.", UIAlertControllerStyle.Alert); 
       alert.AddAction(UIAlertAction.Create("Join", UIAlertActionStyle.Default, async delegate { 
        MBHUDView.HudWithBody ("Connecting Event...", MBAlertViewHUDType.ActivityIndicator, 0, true); 
        await AppDelegate.Api.ConnectToEvent (e, true); 
        MBHUDView.DismissCurrentHUD(); 

        e = AppDelegate.Api.CurrentEvent; 

        DialogHelper.PermissionsDialog (e, delegate { 
         Parent.PerformSegue("phasesSegue", tableView); 
        }, null, Parent); 
       })); 
       alert.AddAction(UIAlertAction.Create("Cancel", UIAlertActionStyle.Cancel, null)); 

       Parent.PresentViewController (alert, true, null); 
      } 
      else 
      { 
       e = AppDelegate.Api.CurrentEvent; 

       DialogHelper.PermissionsDialog (e, delegate { 
        Parent.PerformSegue("phasesSegue", tableView); 
       }, null, Parent); 
      } 
     } 
    } 

現在,我一直在尋找一些可能的解決方案但它似乎相當罕見的子類的UITableView與廣大的解決方案根本是沒有正確設置委託或數據源。我也看到了這個問題的SO解決方案,僅僅是因爲TableView不在視圖中,因此不需要調用委託方法,但事實並非如此。

我不需要設置委託方法,因爲我繼承了UITableView本身,只需重寫內置方法就足夠了。在故事板中,我將課程設置爲我的自定義課程ShootsTable

有沒有人有任何想法?

在此先感謝。

回答

0

這似乎是一個錯誤。不知道它是什麼級別(Xamarin或iOS),但只要它改回到它的工作的IUITableViewDataSource實現。

我會向Xamarin彙報這件事,看看會發生什麼。

相關問題