【问题标题】:Xamarin How to select uitableview cell programatically based on defaultsettingsXamarin 如何根据默认设置以编程方式选择 uitableview 单元格
【发布时间】:2017-01-19 16:03:18
【问题描述】:

我看过一些 uitableview 和加载数据的示例,但我找不到我需要的解决方案。

基本上我有一个数据源(myList),我将数据加载到 UIViewController 中

public class Lookup
{

    public string ID { get; set; }
    public string Name { get; set; }
}

public List<Lookup> myList = new List<Lookup>();

从 Web 服务加载数据后,我将数据绑定到 tableview,效果很好....

myTableView.Source = new tableSource(MyList, this);

这是来自源代码的示例......

   class tableSource : UITableViewSource
{
    List<HospitalLookup> TableItems;
    string CellIdentifier = "TableCell";
    UIVCPreferences Parent;

    public tableSitesSource(List<Lookup> items, UIVCPreferences parent) 
    {
        TableItems = items;
        Parent = parent;
    }
public override UITableViewCell GetCell(UITableView tableView, NSIndexPath indexPath)
    {
        UITableViewCell cell = tableView.DequeueReusableCell(CellIdentifier);
        Lookup item = TableItems[indexPath.Row];            
        if (cell == null)
        { cell = new UITableViewCell(UITableViewCellStyle.Default, CellIdentifier); }
        cell.TextLabel.Text = item.Name;...

我正在尝试找到一种方法来根据用户的偏好值选择表值,比如说 UserLookupValue = 5。

我见过scrolltorow,但我不知道如何根据数据源ID 或名称找到索引。我试过类似的东西:

       myTableView.SelectRow(List.Find(UserLookupValue),false,UITableViewScrollPosition.None);       

我猜我可能需要另一种方式来做到这一点,任何帮助将不胜感激。

【问题讨论】:

    标签: c# ios uitableview xamarin xamarin.ios


    【解决方案1】:

    起初:您的代码很难阅读。您应该通过坚持 c# 的一般命名约定来改进它。

    myTableView.SelectRow(...) 中的List 是什么?如果它是List&lt;LookUp&gt; 类型,它应该是myListSelectRow 需要一个 NSIndexPath 作为第一个参数,但 List.Find 将返回一个 Lookup 实例。通过替换尝试以下操作

    myTableView.SelectRow(List.Find(UserLookupValue),false,UITableViewScrollPosition.None);
    

    var userLookupValueIndex = myList.IndexOf(userLookupValue);    
    myTableView.SelectRow(NSIndexPath.FromRowSection(userLookupIndex, 0), false, UITableViewScrollPosition.None);
    

    这对你有用吗?

    【讨论】:

    • 您好,感谢您的帮助。对不起命名约定,我不得不破解我的一些代码来给出这个例子。以后我会多加注意的。
    猜你喜欢
    • 2012-06-26
    • 2017-01-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-06-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多