在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方法:
- ListBox1.Items.Add('Text to add');
ListBox1.Items.Add('Text to add');
如果你想要創建不只一個簡單項,或是控件其他屬性,你可以先創建一個項目實例,然後將它添加到List Box中。
下面的代碼添加項目到ListBox中,如下圖所示:
- 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: