ios - 如何将这些自定义单元格添加到 TableView?

标签 ios listview xamarin xamarin.forms

使用 Xamarin.Forms,如何实现这样的布局?

enter image description here

我说的是灰色背景上的文字提示:

Turn on Personal Hotspot to share ...

所有我可以添加到 ListViewTableView 的都是标准的 ViewCells,它们都使用分隔符和白色背景呈现。

如果使用 TableViewListView 无法实现此布局,我该如何呈现 ViewCell(如 Wi-Fi 密码示例图像中的一个)在标准 StackLayout 中?

最佳答案

即使 TableView 没有开箱即用的 ViewCell 可以做到这一点,您当然可以推出自己的实现。显然有一个 TextCell,但它不允许您指定字体大小和其他内容。

你可以这样做:

<?xml version="1.0" encoding="UTF-8"?>
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms" xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml" x:Class="DemoTableView.TablePage" Title="TableView">
    <ContentPage.Content>
        <TableView Intent="Settings">
            <TableRoot>
                <TableSection>
                    <SwitchCell Text="Personal Hotspot" />
                    <ViewCell> <!-- Simple text -->
                        <Label Text="Turn on personal hotspot..." FontSize="Small" ForegroundColor="Gray" />
                    </ViewCell>
                    <ViewCell> <!-- More complex cell -->
                        <StackLayout Orientation="Horizontal>
                            <Image Source="someicon" />
                            <StackLayout>
                                <Label Text="TO CONNECT USING WIFI" FontSize="Large"/>
                                <Label Text="Something more" FontSize="Small" />
                            </StackLayout>
                        </StackLayout>
                    </ViewCell>
                </TableSection>
            </TableRoot>
        </TableView>
    </ContentPage.Content>
</ContentPage>

这只是一个匆忙写的简单例子,但你明白了。您还可以通过从 ViewCell 类继承并在 TableView 中显示它们,以编程方式定义自己的单元格。

Here's more关于官方文档中的自定义单元格。


编辑:根据您的评论,我们需要深入研究平台特定代码以禁用对纯文本单元格的选择。您应该使用以下适用于 iOS 的自定义渲染器创建自定义 ViewCell 实现:

[assembly: ExportCell(typeof(YourCell), typeof(YourCellRenderer))]
namespace YourProject.iOS
{
    public class YourCellRenderer : ViewCellRenderer
    {
        public override UITableViewCell GetCell (Cell item, UITableView tv)
        {
            var cell = base.GetCell (item, tv);
            cell.SelectionStyle = UITableViewCellSelectionStyle.None;
            return cell;
        }
    }
}

关于ios - 如何将这些自定义单元格添加到 TableView?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46000569/

相关文章:

iphone - Facebook SDK 3.0 授权后启动 View Controller

iphone - 从我的应用程序向 iPhone 的默认日历添加事件

objective-c - iPad 没有调用 didRegisterForRemoteNotificationsWithDeviceToken

java - ListView 中的图像混合在一起

android - 无法从 apk/res-auto 命名空间访问项目资源

ios - iOS Apple App Store 下载链接中的转义参数

带有 ListView 和自定义光标适配器的 Android 奇怪行为

wpf - 如何使 WPF ListView 项目水平和垂直重复?

android - Android 中的 View.Frame 等价物

ios - Xamarin IOs,传递给 textStyle 参数的 UIFontMetrics.GetMetrics 方法的字符串值是什么?