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

时间:2017-09-01 12:46:37

标签: ios listview xamarin xamarin.forms

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

enter image description here

我在谈论灰色背景的文字提示:

  

启用个人热点以分享...

我可以添加到ListViewTableView的所有标准ViewCells,并且它们都会使用分隔符和白色背景进行渲染。

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

1 个答案:

答案 0 :(得分:2)

即使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;
        }
    }
}
相关问题