Delphi FMX компонент ghosting премахва подкомпоненти

Създадох FMX компонент, който е масив от бутони. Това има просто свойство за преброяване на колони и редове. Всеки от бутоните в масива сам по себе си е компонент и когато броят на редовете или колоните се промени, аз изтривам всички бутони и създавам отново необходимия брой.

Проблемът, който имам, е, че след премахване на компонентите те продължават да се появяват като призрак на екрана. Например броят на колоните и редовете по подразбиране е 10, но за телефонния формат променям това по време на проектиране на 5 x 5. Ето резултата...

Проблем с компонента на масива от бутони

Сигурен съм, че това е свързано със свойството „Съхранено“, но съм сигурен, че се опитах да включа и изключа това на бутоните и масива без разлика. Изходен код на компонента (опитах се да премахна излишния код):

type
  TLFArrayButton = class(TStyledControl)

  TLFButtonArray = class(TPanel)
  private
    { Private declarations }
    FButtons: Array[0..9, 0..9] of TLFArrayButton;
    Sections: Array[0..3] of TRectangle;
    SectionsText: Array[0..3] of TText;
    FRowCount: integer;
    FColCount: integer;
    FFourUp: boolean;
    FOnArrayButtonClick: TLFButtonArrayButtonClick;
    procedure SetColCount(const Value: integer);
    procedure SetRowCount(const Value: integer);
    procedure CheckButtonArraySize;
    procedure PositionButtons;
    procedure FreeButtons;
    function GetButtonsText(Col, Row: Integer): String;
    procedure SetButtonsText(Col, Row: Integer; const Value: String);
    function GetButtonEnabled(Col, Row: Integer): Boolean;
    procedure SetButtonEnabled(Col, Row: Integer; const Value: Boolean);
    procedure SetFourUp(const Value: boolean);
    procedure OnButtonClick(Sender: TObject);

    procedure OnButtonMouseDown(Sender: TObject; Button: TMouseButton; Shift: TShiftState; X, Y: Single);
    procedure SetupSectionHeadders;
    function GetButton(id: integer): TLFArrayButton;
    function GetSectionHeadding(idx: integer): string;
    procedure SetSectionHeadding(idx: integer; const Value: string);
    procedure SetOnArrayButtonClick(const Value: TLFButtonArrayButtonClick);
  protected
    { Protected declarations }
    function GetStyleObject: TFmxObject; override;
    procedure Resize; override;
    procedure ApplyTriggers; virtual;
    procedure DoButtonClick(id: integer); virtual;
  public
    { Public declarations }
    constructor Create(AOwner: TComponent); override;
    destructor Destroy; override;
    property Button[id: integer]: TLFArrayButton read GetButton;
    property ButtonEnabled[Col, Row: Integer]: Boolean read GetButtonEnabled write SetButtonEnabled;
    property ButtonsText[Col, Row: Integer]: String read GetButtonsText write SetButtonsText;
    property SectionHeadding[idx: integer]: string read GetSectionHeadding write SetSectionHeadding;
  published
    { Published declarations }
    property FourUp: boolean read FFourUp write SetFourUp stored true default false;
    property ColCount: integer read FColCount write SetColCount stored true default 10;
    property RowCount: integer read FRowCount write SetRowCount stored true default 10;
    property OnArrayButtonClick: TLFButtonArrayButtonClick read FOnArrayButtonClick write SetOnArrayButtonClick;
 end;

procedure Register;

implementation

{$R *.res}

procedure Register;
begin
  RegisterComponents('LightFactoryFMX', [TLFButtonArray]);
  RegisterComponents('LightFactoryFMX', [TLFArrayButton]);
end;

{ TLFButtonArray }

procedure TLFButtonArray.CheckButtonArraySize;
var
  r, c, i: Integer;
begin
  FreeButtons;
  InvalidateRect(RectF(0,0,width,height));
  i := 1;
  for r := 0 to FRowCount-1 do
  begin
    for c := 0 to FColCount-1 do
    begin
      FButtons[r,c] := TLFArrayButton.Create(Self);
      FButtons[r,c].Parent := Self;
      FButtons[r,c].Stored := false;
      FButtons[r,c].Locked := true;
      FButtons[r,c].ShowID := true;
      FButtons[r,c].ID := i;
      FButtons[r,c].OnClick := OnButtonClick;
      inc(i);
    end;
  end;
end;

constructor TLFButtonArray.Create(AOwner: TComponent);
begin
  inherited;
  Stored := false;
  StyleLookup := 'panelstyle';
  FFourUp := false;
  FColCount := 10;
  FRowCount := 10;
  CheckButtonArraySize;
  PositionButtons;
end;

procedure TLFButtonArray.FreeButtons;
var
  r, c: Integer;
begin
  for r := 0 to FRowCount-1 do
  begin
    for c := 0 to FColCount-1 do
    begin
      if assigned(FButtons[r, c]) then
      begin
        FButtons[r, c].Free;
        FButtons[r, c] := NIL;
      end;
    end;
  end;
end;

procedure TLFButtonArray.PositionButtons;
var
  r, c, s, b, loc_col, loc_row, num_per_sec: Integer;
  x, y, dw, dh: single;
  tmpBtn: TLFArrayButton;
begin
  if FFourUp then
  begin
    loc_col := ColCount div 2;
    loc_row := RowCount div 2;
    num_per_sec := loc_col * loc_row;
    //
    dw := (Width - INTER_SECTION_SPACING - ((FColCount+1) * BUTTON_SPACING)) / FColCount;
    dh := (Height - INTER_SECTION_SPACING - (Sections[0].Height * 2) - ((FRowCount+1) * BUTTON_SPACING)) / FRowCount;
    x := BUTTON_SPACING;
    y := BUTTON_SPACING;

    for s := 0 to 3 do
    begin
      x := Sections[s].Position.X;
      y := Sections[s].Position.Y + Sections[s].Height + BUTTON_SPACING;
      for b := 1 to num_per_sec do
      begin
        tmpBtn := GetButton((s*num_per_sec) + b);
        if assigned(tmpBtn) then
        begin
          tmpBtn.Position.X := x;
          tmpBtn.Position.Y := y;
          tmpBtn.Width := dw;
          tmpBtn.Height := dh;
          x := x + dw + BUTTON_SPACING;
          if x > (Sections[s].Position.X + Sections[s].Width) then
          begin
            x := Sections[s].Position.X;
            y := y + dh + BUTTON_SPACING;
          end;
        end;
      end;
    end;
  end
  else
  begin
    dw := (Width - ((FColCount+1) * BUTTON_SPACING)) / FColCount;
    dh := (Height - ((FRowCount+1) * BUTTON_SPACING)) / FRowCount;
    x := BUTTON_SPACING;
    y := BUTTON_SPACING;
    for r := 0 to FRowCount-1 do
    begin
      for c := 0 to FColCount-1 do
      begin
        FButtons[r,c].Position.X := x;
        FButtons[r,c].Position.Y := y;
        FButtons[r,c].Width := dw;
        FButtons[r,c].Height := dh;
        x := x + dw + BUTTON_SPACING;
      end;
      x := BUTTON_SPACING;
      y := y + dh + BUTTON_SPACING;
    end;
  end;
end;

procedure TLFButtonArray.Resize;
begin
  inherited;
  if FourUp then
    SetupSectionHeadders;
  PositionButtons;
end;

destructor TLFButtonArray.Destroy;
begin
  FreeButtons;
  inherited;
end;

procedure TLFButtonArray.SetColCount(const Value: integer);
begin
  if FColCount <> Value then
  begin
    if (Value > 0) and (Value < 11) then
    begin
      FColCount := Value;
      CheckButtonArraySize;
      PositionButtons;
    end;
  end;
end;

procedure TLFButtonArray.SetRowCount(const Value: integer);
begin
  if FRowCount <> Value then
  begin
    if (Value > 0) and (Value < 11) then
    begin
      FRowCount := Value;
      CheckButtonArraySize;
      PositionButtons;
    end;
  end;
end;

person Martin    schedule 27.08.2014    source източник
comment
Свързани stackoverflow.com/q/27818697/960757.   -  person TLama    schedule 14.01.2015


Отговори (1)


Формата все още препраща към бутоните поради ARC (автоматично преброяване на препратки) в Delphi за iOS/Android. Следователно FButtons[r, c].Free; трябва да бъде FButtons[r, c].DisposeOf;.

person Sebastian Z    schedule 27.08.2014
comment
Задните на ARC. Винаги трябва да имате предвид кой всички препраща към вашия клас/компонент. Защо. Тъй като извикването на SomeComponent.Free вече не извиква класа на деструктора, а само задава променливата SomeComponent на нула. След това деструкторът на клас/компонент се извиква само ако броят на препратките за този клас/компонент е 0. - person SilverWarior; 27.08.2014