xamarin.ios - UITableView RowSelected 没有被调用?

标签 xamarin.ios xamarin uitableview

我有一个 UITableView 和一个子类 UITableViewSource 类:

table = new UITableView(window.Bounds);
table.Source = new CellSource();

public class CellSource : UITableViewSource
{
    // etc etc

我正在尝试获取选定的行,因此在我的源类中实现了 RowSelected 方法:

public override void RowSelected(UITableView tableview, NSIndexPath indexpath) {
    Console.WriteLine("User tapped");
}

但是,点击单元格根本没有任何响应,即使只是尝试写入控制台也是如此。

如果有帮助的话,我可以发布完整的类(class)吗?

谢谢。

编辑

所以,在我创建 UITableView 后:

table = new UITableView(window.Bounds);
table.AutoresizingMask = UIViewAutoresizing.All;
table.SeparatorStyle = UITableViewCellSeparatorStyle.None;
table.BackgroundColor = UIColor.Clear;
table.Source = new CellSource();

我使用源类来解析 XML 文件,并填充列表:

List<Treasure> treasures = new List<Treasure>();

protected class Treasure {
    public string cellTitle { get; set; }
    public string cellTag { get; set; }
    public string cellImage { get; set; }
    public string audioFile { get; set; }
    public string mainTitle { get; set; }
    public string mainTag { get; set; }
    public string mainBody { get; set; }
    public string mainImage { get; set; }
    public string mainCaption { get; set; }
}

public CellSource (/*string[] items*/)
{
    Console.WriteLine("CellSource called");
    string fileName = "treasuresiPhone.xml";
    XDocument doc = XDocument.Load(fileName);
    treasures = doc.Descendants("treasures").FirstOrDefault().Descendants("treasure").Select(p=> new Treasure() {
        cellTitle = p.Element("celltitle").Value,
        cellTag = p.Element("celltagline").Value,
        cellImage = p.Element("cellimage").Value
    }).ToList();

    numCells = treasures.Count();       
}

然后,我在另一个类中创建一个 CGBitmapContext,返回图像,并将其设置为单元格图像:

public UIImage DrawCell (string cellImage) {
    string cellI = cellImage;
    //create a new graphics context
    int width = 320;
    int height = 110;
    CGBitmapContext ctx = new CGBitmapContext(IntPtr.Zero, width, height, 8, 4*width, CGColorSpace.CreateDeviceRGB(), CGImageAlphaInfo.PremultipliedFirst);

    //load an image
    var imagePath = (@cellI);
    var image = UIImage.FromFile(imagePath).CGImage;
    ctx.DrawImage(new RectangleF(0, 0, width, height), image);

    UIImage returnedImage = new UIImage();
    returnedImage = FromImage(ctx.ToImage());

    return returnedImage;
}

我添加了一些其他测试内容,例如标签,并覆盖 RowSelected 方法:

public override UITableViewCell GetCell (UITableView tableView, MonoTouch.Foundation.NSIndexPath indexPath) {
    UITableViewCell cell = tableView.DequeueReusableCell (cellIdentifier);

    cell = new UITableViewCell (UITableViewCellStyle.Default, cellIdentifier);
    cell.UserInteractionEnabled = false;
    UILabel secondViewLabel = new UILabel();

    //if there are no cells create a new one
    if (cell == null) {
        Console.WriteLine("cell == null");
    } else {

        //create a new cellobject - this grabs the image and returns a CGBitmapContext
        CellObject _cellObject = new CellObject();
        cell.ImageView.Image = _cellObject.DrawCell(treasures[indexPath.Row].cellImage);

        //add text
        secondViewLabel.Tag = 1;
        secondViewLabel.Text = treasures[indexPath.Row].cellTitle;
        secondViewLabel.TextColor = UIColor.White;
        secondViewLabel.TextAlignment = UITextAlignment.Center;
        secondViewLabel.Lines = 0;
        secondViewLabel.LineBreakMode = UILineBreakMode.WordWrap;
        secondViewLabel.Font = UIFont.FromName("Helvetica", 16);
        secondViewLabel.BackgroundColor = UIColor.FromRGB(205, 54, 51);

        //get the width of the text
        SizeF labelSize = secondViewLabel.StringSize(secondViewLabel.Text, secondViewLabel.Font);

        secondViewLabel.Frame = new RectangleF(0, 110 - (labelSize.Height + 10), labelSize.Width + 20, labelSize.Height + 10);

        //add a second view
        UIView secondView = new UIView();
        secondView.AddSubview(secondViewLabel);
        cell.ContentView.AddSubview(secondView);
    }
    return cell;
}

public override void RowSelected(UITableView tableview, NSIndexPath indexpath) {
    Console.WriteLine("User tapped");
}

最佳答案

在 Monotouch 中,UITableViewSourceUITableViewUITableViewDataSourceUITableViewDelegate 的组合。因此,当您扩展此类时,您可以利用数据源和委托(delegate)中的方法。

说到这里,我会稍微修改一下你的代码(见评论)。

public override UITableViewCell GetCell (UITableView tableView, MonoTouch.Foundation.NSIndexPath indexPath)
{
    UITableViewCell cell = tableView.DequeueReusableCell (cellIdentifier);

    // if there are no cells create a new one
    if (cell == null) {
        cell = new UITableViewCell (UITableViewCellStyle.Default, cellIdentifier);      

        // why do you set the interaction to false? Setting it to false disable interaction for your cell
        cell.UserInteractionEnabled = true;

        // create label and view here. You customize the cell with additional elements once

    }

    // update image and label here (you need to grab a reference for your label for example through the tag)

    return cell;
}

另请查看 Working with Tables and Cells在 Xamarin 文档中。

希望有帮助。

关于xamarin.ios - UITableView RowSelected 没有被调用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15951889/

相关文章:

ios - 如何删除 cell.imageView?

swift - xib 文件中的单元测试自定义 tableviewcell

c# - 具有 NULL 对象的 NSNotificationCenter.PostNotificationName() 不会触发 : bug or as designed?

.net - 在 MonoTouch 中找不到 System.Runtime.Serialization.Json

c# - 如何在 iOS 10.2 上使用 xamarin forms + zxing 扫描驾照 (PDF417)

c# - 从 iPhone native 应用程序使用 Xamarin 库

ios - 为什么我的 UITableView 的顶部有额外的填充,在 iOS7 中具有 UITableViewStyleGrouped 样式

ios - 我们可以从 swift 框架生成 Objective-C 静态库吗?

javascript - 使用 c# - xamarin ios 最简单的用于 monotouch uiwebview 的 javascript 桥

android - Google API 客户端 OAuth 始终返回已取消