【问题标题】:MonoTouch: how to use UITableViewController as data source for UITableView?MonoTouch:如何使用 UITableViewController 作为 UITableView 的数据源?
【发布时间】:2012-05-22 14:44:29
【问题描述】:

我正在使用带有静态内容的故事板和表格视图。在内部,UITableViewController 似乎隐含地成为了UITableView 的来源。

如果我现在想影响静态内容,我将不得不覆盖表源的方法。在 ObjectiveC 中我可以放置

-(NSString*)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
    if (section == 0)
        return @"HELLO!";
    else {
        return [super tableView:tableView titleForHeaderInSection:section];
    }
}

在我的控制器中,该方法将被覆盖。但在 MonoTouch 中,这不起作用。 请注意,我不想创建委托或数据源的新实例。对于静态单元,控制器源/代表。在 ObjectiveC 中,这是通过让控制器实现相应的协议来完成的。

这是我提出的与该主题相关的问题,但现在我无法将解决方案转换为 MonoTouch:

How to override tableView:titleForHeaderInSection: to adjust section headers of static UITableViews?

【问题讨论】:

    标签: c# xamarin.ios uikit


    【解决方案1】:

    在 Xamarin 网站上的精彩教程中找到了解决方案:http://docs.xamarin.com/ios/tutorials/Events%2c_Protocols_and_Delegates

    Export”属性可以解决问题!

    public partial class TestController : UITableViewController
        {
            public TestController (IntPtr handle) : base (handle)
            {
    
            }
    
            public override void ViewDidLoad ()
            {
                base.ViewDidLoad ();
            }
    
            [Export("tableView:titleForHeaderInSection:")]
            public string TitleForHeaderInSection(UITableView oTableView, int iSection)
            {
                return "TEST";
            }
        }
    

    【讨论】:

    • 我用[Export("tableView:didSelectRowAtIndexPath:")] 尝试过,但它从未被调用过。你还有什么要补充的吗?
    • @testing 你必须将控制器设置为WeakDelegateWeakSource - 不记得确切的属性。
    • WeakDelegate 对我说了些什么。下次需要的时候我会试试的。
    【解决方案2】:

    在 Mono Touch 中,您必须创建一个 DataSource Delegate。

    喜欢这篇博文中的演示:http://sabonrai.wordpress.com/2009/08/28/monotouch-sample-code-uitableview/

            public override string TitleForHeader (UITableView tableView, int section)
            {
            //do your stuff 
            }   
    

    如果您想自定义标题部分的标题,只需覆盖 TableViewDelegate 中的 titleForHeaderInSection。

    【讨论】:

    • 我知道。但在特定情况下,UITableViewController 委托。所以我不想再创造一个。我只是想以某种方式告诉我的控制器它实现了协议。
    • 在 .NET 中不可能有一个由多个类继承的类。只有多个接口,但 UITableViewDataSource 不是一个接口。所以你必须为 UITableView 创建一个单独的委托。在 Objective-C 中,可以有一个实现多个协议的类。另请参阅此文档docs.xamarin.com/ios/tutorials/Events,_Protocols_and_Delegates“协议深潜”
    猜你喜欢
    • 1970-01-01
    • 2012-02-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-08-01
    • 1970-01-01
    相关资源
    最近更新 更多