I have tried to implement a ListView using OwnerData = true and an OnData procedure on Cocoa.
The data is only showing when setting Items.Count in the OnCreate procedure of the program, not when trying to add new items with setting Items.Count during run-time.
I have attached an example project and here is the code:
unit Unit1;
{$mode objfpc}{$H+}
interface
uses
Classes, SysUtils, FileUtil, Forms, Controls, Graphics, Dialogs, ComCtrls,
StdCtrls;
type
{ TForm1 }
TForm1 = class(TForm)
lv_def: TListView;
lv_virtual: TListView;
bt_add_item_lv_def: TButton;
bt_add_item_lv_virtual: TButton;
procedure FormCreate(Sender: TObject);
procedure lv_virtualData(Sender: TObject; Item: TListItem);
procedure bt_add_item_lv_defClick(Sender: TObject);
procedure bt_add_item_lv_virtualClick(Sender: TObject);
private
{ private declarations }
public
{ public declarations }
procedure AddItemDef;
end;
var
Form1: TForm1;
implementation
{$R *.lfm}
{ TForm1 }
procedure TForm1.FormCreate(Sender: TObject);
begin
// set items for default listview - works on Cocoa [items are displayed when starting the program]
self.AddItemDef;
self.AddItemDef;
self.AddItemDef;
// set items for virtual listview - works on Cocoa [items are displayed when starting the program]
lv_virtual.Items.Count := 3;
end;
procedure TForm1.bt_add_item_lv_defClick(Sender: TObject);
begin
// add item to default listview - works on Cocoa [items are displayed after clicking the button]
self.AddItemDef;
end;
procedure TForm1.bt_add_item_lv_virtualClick(Sender: TObject);
begin
// add item to virtual listview - NOT WORKING on Cocoa [new items are not displayed after clicking the button]
lv_virtual.Items.Count := lv_virtual.Items.Count + 1;
self.Caption := IntToStr(lv_virtual.Items.Count); // shows the correct item count
//lv_virtual.Invalidate; - no effect
//lv_virtual.Refresh; - no effect
//lv_virtual.Repaint; - no effect
//lv_virtual.Update; - no effect
end;
procedure TForm1.lv_virtualData(Sender: TObject; Item: TListItem);
begin
// fetch virtual listview data
Item.Caption := 'Cap'+inttostr(item.Index);
Item.SubItems.Add('Sub1');
Item.SubItems.Add('Sub2');
end;
procedure TForm1.AddItemDef;
var
AItem: TListItem;
begin
// add item to default listview - works on Cocoa
AItem := lv_def.Items.Add;
AItem.Caption := 'Cap'+inttostr(lv_def.Items.Count);
AItem.SubItems.Add('Sub1');
AItem.SubItems.Add('Sub2');
end;
end.
At startup, the ListView has 3 columns, at run-time, you can add new items with the Add-button.
On Windows, it is working in both situations; on Cocoa the Add-button procedure is not working. I think there is some procedure for the refresh missing. I have tried using .Invalidate, .Refresh, .Repaint and .Update but the items are not displayed.
Does someone have an idea?