您的位置:首页 > 编程语言 > Delphi

Delphi for iOS开发指南(9):在iOS应用程序中使用ListBox组件来显示TableView

2013-09-03 09:19 741 查看
在FireMonkey iOS应用程序中使用ListBox组件来显示TableView

 

在iOS平台上,FireMonkey使用FMX.ListBox.TListBox组件来显示一个iOS Style的TableView,像这种ListBox:







这篇教程描述了在你的FireMonkey iOS应用程序中为TableView建立项目列表的基本步骤。

 

 

在ListBox组件上创建项目列表

 

1.        选择File>New>FireMonkey Mobile Application-Delphi>BlankApplication。

2.        在Tool Palette选择TListBox组件,然后将它拖放到FireMonkey Mobile Form Designer。要找到TListBox,在ToolPalette的搜索框中输入部分字符(例如“tlist”)。



3.选中在Mobile FormDesigner上的TListBox组件,在Object Inspector里为Align属性设置为alClient:



4.在FireMonkey MobileForm Designer上,右击TListBox组件,然后选择Items Editor:



5.在Items Designer上,多点几次AddItem按钮来给ListBox添加一些项目:



6.关闭项目列表设计器。现在你可以在TListBox组件上找到你刚才添加的ListBox项目列表。例如:



 

 

 

 

 

添加一个Header

 

你可以使用下面的步骤来在TListBox组件上定义一个Header:

1.在FireMonkey MobileForm Designer上,右击TListBox组件,然后选择Add Item>TListBoxHeader:





2.在Tool Palette上,选择TLabel组件并将它拖放到你刚才添加的TListBoxHeader上方:



3.在Object Inspector里,更改TLabel组件的属性如下:



 

 

添加分组Header/Footer到列表中

 

你可以在TListBox上面加上一个分组Header和分组Footer:



1.  在FireMonkeyMobile Form Desginer上,右击TListBox组件,然后选择Items Editor。

2.  在ItemDesigner上,从下拉列表中选择TListBoxGroupHeader,然后选择Add Item:



3.  在下拉列表中选择TListBoxGroupFooter,然后选择Add Item。

4.  选中在项目列表中的ListBoxGroupHeader1,然后点击几次Up按钮直到这个项目成为列表中最上面那个:



5.关闭对话框。现在在TListBox组件上有一个分组Header和一个分组Footer。

 

 

显示列表项为分隔分组项

 

在ListBox上的项目可以显示成一个Plain列表或一个Grouped列表。由GroupingKind属性和StyleLookup属性所控制的,如下图所示:



你可以在Object Inspector中为你的TListBox组件选择一个Style。

 

 

 

添加一个复选框或其他辅助项给ListBox项

 

TListBox里的每个项通过ItemData.Accessory属性来使用一个辅助项,例如复选标记。下面的图片显示了你可以赋给ItemData.Accessory的值:



当Form Designer中的ListBox列表项选中的时候,你可以在Object Inspector中选择Accessory属性的值。



 

 

给ListBox项添加图标

 

每个在ListBox组件中的项目都包含一个Bitmap数据,做为图标,通过ItemData.Bitmap属性:



当ListBoxItem在Form Designer上被选中的时候你就可以在Object Inspector中选择Bitmap属性了。

 

 

 

 

给项目添加详信息

 

你可以给在ListBox组件上的每个列表项添加附件文本信息。

在ItemData.Detail属性中指定附加文本,然后通过StyleLookup属性来选择详细文本的位置,如下表格所示:



 

 

 

在代码中添加列表项给ListBox

 

要在ListBox中添加普通的项,你可以简单的调用Items.Add方法:

 

[delphi]
view plaincopyprint?

ListBox1.Items.Add('Text to add');  

ListBox1.Items.Add('Text to add');

 

 

 

如果你想要创建不只一个简单项,或是控件其他属性,你可以先创建一个项目实例,然后将它添加到List Box中。

下面的代码添加项目到ListBox中,如下图所示:



 

 

[delphi]
view plaincopyprint?

procedure TForm40.FormCreate(Sender: TObject);  
var  
  c: Char;  
  i: Integer;  
  Buffer: String;  
  ListBoxItem : TListBoxItem;  
  ListBoxGroupHeader : TListBoxGroupHeader;  
begin  
  ListBox1.BeginUpdate;  
  for c := 'a' to 'z' do  
  begin  
    // Add header ('A' to 'Z') to the List
  
    ListBoxGroupHeader := TListBoxGroupHeader.Create(ListBox1);  
    ListBoxGroupHeader.Text := UpperCase(c);  
    ListBox1.AddObject(ListBoxGroupHeader);  
   
    // Add items ('a', 'aa', 'aaa', 'b', 'bb', 'bbb', 'c', ...) to the list
  
    for i := 1 to 3 do  
    begin  
      // StringOfChar returns a string with a specified number of repeating characters.
  
      Buffer := StringOfChar(c, i);  
      // Simply add item   
      // ListBox1.Items.Add(Buffer);
  
   
      // or, you can add items by creating an instance of TListBoxItem by yourself
  
      ListBoxItem := TListBoxItem.Create(ListBox1);  
      ListBoxItem.Text := Buffer;  
      // (aNone=0, aMore=1, aDetail=2, aCheckmark=3)
  
      ListBoxItem.ItemData.Accessory := TListBoxItemData.TAccessory(i);  
      ListBox1.AddObject(ListBoxItem);  
    end;  
  end;  
  ListBox1.EndUpdate;  
end;  

procedure TForm40.FormCreate(Sender: TObject);
var
c: Char;
i: Integer;
Buffer: String;
ListBoxItem : TListBoxItem;
ListBoxGroupHeader : TListBoxGroupHeader;
begin
ListBox1.BeginUpdate;
for c := 'a' to 'z' do
begin
// Add header ('A' to 'Z') to the List
ListBoxGroupHeader := TListBoxGroupHeader.Create(ListBox1);
ListBoxGroupHeader.Text := UpperCase(c);
ListBox1.AddObject(ListBoxGroupHeader);

// Add items ('a', 'aa', 'aaa', 'b', 'bb', 'bbb', 'c', ...) to the list
for i := 1 to 3 do
begin
// StringOfChar returns a string with a specified number of repeating characters.
Buffer := StringOfChar(c, i);
// Simply add item
// ListBox1.Items.Add(Buffer);

// or, you can add items by creating an instance of TListBoxItem by yourself
ListBoxItem := TListBoxItem.Create(ListBox1);
ListBoxItem.Text := Buffer;
// (aNone=0, aMore=1, aDetail=2, aCheckmark=3)
ListBoxItem.ItemData.Accessory := TListBoxItemData.TAccessory(i);
ListBox1.AddObject(ListBoxItem);
end;
end;
ListBox1.EndUpdate;
end;


 

 

 

添加搜索框

 

你可以添加搜索框到ListBox中,使用搜索框,用户可以方便从很长的列表中定位到一个选项,如下图所示:





要在ListBox组件中添加搜索框,右击TListBox组件,从弹出菜单中选择Add Item>TSearchBox:



内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  delphi ios开发
相关文章推荐