Xamarin.ios 是否未调用UITableView行选择?
我有一个Xamarin.ios 是否未调用UITableView行选择?,xamarin.ios,xamarin,uitableview,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(UITa
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");
}
然而,点击单元格根本不会给出任何响应,即使只是尝试写入控制台
如果有帮助的话,我可以发布完整的课程
谢谢
编辑
因此,在创建我的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();
}
我添加了一些其他测试内容,如标签,并覆盖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中,
UITableViewSource
是用于UITableView
的组合的UITableViewDataSource
和UITableViewDelegate
。所以,在扩展此类时,可以利用数据源和委托中的方法
说到这里,我会稍微修改一下您的代码(参见注释)
还可以查看Xamarin文档中的
希望有帮助。您是否也为视图分配了委托或数据源?我猜不是,我只有CellSource类,它用XML中的数据填充表。我找不到任何关于分配数据源的细节,我已经在谷歌上搜索过了,但是所有的例子看起来都和我正在做的非常相似。感谢您的回复。在创建表和分配属性时,您是否在做其他事情,或者您发布了所有内容?我将向top.cell.UserInteractionEnabled=true添加更多详细信息;
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");
}
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;
}