【问题标题】:MonoTouch How to implement RowSelected for a UITableViewController with Static CellsMonoTouch 如何为带有静态单元格的 UITableViewController 实现 RowSelected
【发布时间】:2012-04-23 07:51:21
【问题描述】:

我正在将本机 Objective-C 应用程序移植到 Monotouch 解决方案。

我的情节提要中有一个表格视图,其中包含一些静态表格单元格。

使用下面的objective-c代码a从表中接收到一个选择

- (void) tableView:(UITableView *) aTableView didSelectRowAtIndexPath:(NSIndexPath *) indexPath
{        // remove the row highlight
    [aTableView deselectRowAtIndexPath:indexPath animated:YES];

    switch(indexPath.row) {
        case 0: [self callWebsite]; break;
        case 1: [self sendEmail]; break;
        case 2: [self makePhoneCall]; break;
    }

    return;
}

现在我想在 MonoTouch 中做同样的事情,但无法找到我在网上找到的所有示例以及如何使用 DataSource 和 UITableViewDelegate。

但是当使用这种方法时,我的“静态”单元格被删除、替换了。

这就是我正在尝试的

public partial class ContactViewController : UITableViewController
{
    public ContactViewController (IntPtr handle) : base (handle)
    {
        this.TableView.Delegate = new CustomTableViewDelegate();
    }
}

public class CustomTableViewDelegate : UITableViewDelegate
{
    public override void RowSelected (UITableView tableView, NSIndexPath indexPath)
    {
        // Remove the row highlight
        RowDeselected(tableView, indexPath);

        /*
             // Is this section our "button" section?
            switch(indexPath.row) {
                case 0: [self callWebsite]; break;
                case 1: [self sendEmail]; break;
                case 2: [self makePhoneCall]; break;
            }
        */

        Console.WriteLine("Do something meaningfull.");
    }
}

有人有什么建议吗?

【问题讨论】:

  • 我不太明白,你的细胞是如何被删除的?你粘贴的代码是选择单元格时的动作,你有一些注释代码,为什么要注释掉?
  • 注释掉的代码是我的 Objective-c 代码的复制粘贴,因为我正在将其翻译成 C#。对这个问题没有任何意义。我不确定为什么要删除单元格。我认为这是因为我没有实现从代码创建表时使用的方法“numberOfRowsInSection”和“cellForRowAtIndexPath”。不是静态的……“我认为……”

标签: uitableview static xamarin.ios cells


【解决方案1】:

我认为您应该尝试使用WeakDelegate 来完成这项工作。

在你的控制器中连接这样的方法:

[Export("tableView:didSelectRowAtIndexPath:")]
public void RowSelected (UITableView tableView, NSIndexPath indexPath)
{
     //Your code here
}

然后设置TableView.WeakDelegate = this;

试一试,我的参数可能不完全正确,等等。

【讨论】:

  • 我对此进行了测试,发现设置 WeakDelegate 会导致静态单元格被清除。但是,如果您将带有 Export 属性的 RowSelected 方法添加到拥有的 UITableViewController 中,那么您会得到预期的结果。
  • 因此您设置的静态单元格必须将控制器设置为UITableView 的委托。您所做的可能是使其与 MonoTouch 一起使用的唯一方法。这是我想亲自尝试的一件事,我从来没有使用过故事板,因为它们需要 iOS 5。我们已经到了需要 iOS 5 可能不再是什么大不了的地步了。
  • 确实实现了这一点,没有弱委托分配工作。谢谢各位!我会投票,但我没有足够的声誉,whoehoeee =P
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2015-08-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-04-01
  • 2012-01-28
  • 1970-01-01
相关资源
最近更新 更多