ios - 如何更改 UITableView 中所选项目的 SeparatorColor

标签 ios uitableview xamarin

我有以下 UITableView

_navigationList = new UITableView
    {
        Source = UINavigationTableViewSource,
        BackgroundColor = UIColor.FromRGB(43, 43, 43), // #2b2b2b - same as Android
        SeparatorColor = UIColor.FromRGB(27,27,27), // #1b1b1b - same as Android
        SeparatorInset = UIEdgeInsets.Zero,
        ScrollEnabled = true
    };

UINavigationTableViewSource 使用自定义 UITableViewCell

public sealed class UINavigationTableViewCell : UITableViewCell
{
    public new readonly UILabel TextLabel;

    public UINavigationTableViewCell(string cellId)
        : base(UITableViewCellStyle.Default, cellId)
    {
        TextLabel = new UILabel(new RectangleF(10, 0, Bounds.Width, 40)) {TextColor = UIColor.White}; // properly position the label.

        SeparatorInset = UIEdgeInsets.Zero; // make the boarder go all the way across the list item

        ContentView.BackgroundColor = UIColor.FromRGB(43, 43, 43); // #2b2b2b - same as Android

        var activeOrb = new UIImageView(new RectangleF(0,0,10,Bounds.Height))
            {
                Image = UIImage.FromBundle("Images/active_orb.png"), 
                ContentMode = UIViewContentMode.ScaleAspectFit
            };
        SelectedBackgroundView = new UIView { BackgroundColor = UIColor.FromRGB(43, 43, 43) };
        SelectedBackgroundView.Add(activeOrb);

        ContentView.Add(TextLabel);
    }
}

除了选定行上的丑陋白线外,一切看起来都符合我们的要求。

如何在选中一行时更改 SeparatorColor?

enter image description here

最佳答案

我从来没有用 xamarin 编程过,但我在 ios(5-7) sdk 上工作过。
我建议您不要使用任何分隔符。而是将分隔符行作为 view(height-1pixel;width-cellWidth) 添加到您的单元格中。


由 ChaseFlorell 编辑 - 工作示例

_navigationList = new UITableView
    {
        Source = UINavigationTableViewSource,
        BackgroundColor = UIColor.FromRGB(43, 43, 43), // #2b2b2b - same as Android
        SeparatorStyle = UITableViewCellSeparatorStyle.None,
        ScrollEnabled = true
    };
Add(_navigationList);
public sealed class UINavigationTableViewCell : UITableViewCell
{
    public new readonly UILabel TextLabel;

    public UINavigationTableViewCell(string cellId) : base(UITableViewCellStyle.Default, cellId)
    {
        // The default state
        TextLabel = new UILabel(new RectangleF(10, 0, Bounds.Width, 40)) { TextColor = UIColor.White }; // properly position the label.

        ContentView.BackgroundColor = UIColor.FromRGB(43, 43, 43); // #2b2b2b - same as Android
        ContentView.Add(Separator(Bounds));
        ContentView.Add(TextLabel);

        // todo: move colors to a config file.

        // The selected state
        var activeOrb = new UIImageView(new RectangleF(0, 0, 10, Bounds.Height))
            {
                Image = UIImage.FromBundle("Images/active_orb.png"),
                ContentMode = UIViewContentMode.ScaleAspectFit
            };
        SelectedBackgroundView = new UIView { BackgroundColor = UIColor.FromRGB(43, 43, 43) };
        SelectedBackgroundView.Add(Separator(Bounds));
        SelectedBackgroundView.Add(activeOrb);
    }

    private static UIView Separator(RectangleF bounds)
    {
        return new UIView(new RectangleF(0, 0, bounds.Width, 1))
        {
            BackgroundColor = UIColor.FromRGB(27, 27, 27)
        };
    }
}

关于ios - 如何更改 UITableView 中所选项目的 SeparatorColor,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21464562/

相关文章:

ios - 使用 UIImageWriteToSavedPhotosAlbum() 捕获并存储图片

ios - 使用 AKPickerView 保存选择

c# - 使用 PageViewer 和 LayoutPagerAdapter 时的内存管理

c# - 如何分配 UIPopoverPresentationControllerDelegate

ios - 实时更新自定义表格部分标题 View 内容

uitableview - IOS 6 子类不更新 UITableViewCell 中的 UITextField

c# - 如何在不阻止 Xamarin Android 中用户输入的情况下更新 UI?

java - 什么是 android logcat 中的致命信号 6

ios - 从 Swift 函数中的异步调用返回数据

ios - 使用 UIStepper 更新 UITableViewCell 内容