【问题标题】:How to make `TextCell` `Detail` appear on the right of `Text` next to `Disclosure` arrow in Xamarin Forms?如何使`TextCell``Detail`出现在Xamarin Forms中`Disclosure`箭头旁边的`Text`右侧?
【发布时间】:2017-05-04 11:14:07
【问题描述】:

所以这是我第一次使用 Xamarin Forms TextCell。我希望单元格的 Detail 文本出现在右侧的 Disclosure 图标旁边,如下图所示。

PS:图片不是我的。

我已尝试搜索此内容,但找不到任何参考。有谁知道我该怎么做?

【问题讨论】:

    标签: c# xamarin.ios xamarin.forms


    【解决方案1】:

    原生 iOS 单元格有一个 UITableViewCellStyle (documentation),您可以通过实现 custom renderer 来设置它。

    这应该看起来像这样:

    [assembly: ExportRenderer (typeof (TextCell), typeof (RightDetailSample.iOS.TextCellRenderer))]
    namespace RightDetailSample.iOS
    {
        public class TextCellRenderer : Xamarin.Forms.Platform.iOS.TextCellRenderer
        {
    
            public override UITableViewCell GetCell (Cell item, UITableViewCell reusableCell, UITableView tv)
            {
                var textCell = (TextCell)item;
    
                var fullName = item.GetType ().FullName;
                var cell = tv.DequeueReusableCell (fullName) as CellTableViewCell;
                if (cell == null) {
                    cell = new CellTableViewCell (UITableViewCellStyle.Value1, fullName);
                } else {
                    cell.Cell.PropertyChanged -= cell.HandlePropertyChanged;
                }
    
                cell.Cell = textCell;
                textCell.PropertyChanged += cell.HandlePropertyChanged;
                cell.PropertyChanged = this.HandlePropertyChanged;
    
                cell.TextLabel.Text = textCell.Text;
    
                cell.DetailTextLabel.Text = textCell.Detail;
    
    
                UpdateBackground (cell, item);
    
                return cell;
            }
        }
    }
    

    注意您如何在新单元格的构造函数中设置UITableViewCellStyle.Value1。之后无法设置。实现后如下所示:

    可以在here找到一个示例项目。

    【讨论】:

    • 非常感谢!太完美了!
    • 这个例子适用于静态数据,但它没有解决如果底层值发生变化会发生什么。如果用户将英语更改为“法语”,则底层视图模型会更改,但 UItableview 永远不会刷新。到目前为止,我让它工作的唯一方法是添加... textCell.PropertyChanged += (sender, args) => tv.ReloadData();顺便说一句,这不是批评,只是向那些期望它反映视图模型变化的人指出。
    【解决方案2】:

    TextCell 在其详细信息下具有静态格式标题,如果您想进行自定义单元格设计,您应该使用Custom Cell

    【讨论】:

      猜你喜欢
      • 2018-01-17
      • 1970-01-01
      • 1970-01-01
      • 2022-11-18
      • 1970-01-01
      • 1970-01-01
      • 2019-05-23
      • 1970-01-01
      • 2017-10-03
      相关资源
      最近更新 更多