Hi,
I amd creating TableView's on the fly in tabs and I want to be able to delete the item from within the click.
I am using the following code. Most of the time this works but occasionally I get an exception on doing the item release. I have tried using TV.Items.Delete(Item.Index) but I get an exception every time I do. I have checked that all variables are correct.
I an assigning the Item to the TagObject as I can't currently find any other way of getting access to the Item from withing the bitmap onclick event.
How should I delete an item from within the click?
Thanks,
Ken
procedure TMainForm.gGenericItemCustomize(Sender: TObject;
AItem: TTMSFMXTableViewItem; AItemShape: TTMSFMXTableViewItemShape;
AItemControlShape: TControl);
var
SB:TSpinBox;
L,R:TRectangle;
Bmp:TTMSFMXBitmap;
begin
R:=AItemShape.ShapeRightRectangle;
if R.ControlsCount=0 then
begin
R.Width:=70;
SB:=TSpinBox.Create(R);
SB.Parent:=R;
SB.Min:=1;
SB.Max:=99;
SB.OnChange:=gGenericSpinBoxChange;
SB.OnChangeTracking:=gGenericSpinBoxChange;
SB.Tag:=AItem.Index;
SB.Align:=tAlignLayout.Client;
SB.Value:=AItem.DataValue;
L:=AItemShape.ShapeLeftRectangle;
L.Width:=44;
Bmp:=AItemShape.ShapeLeftRectangleImage;
Bmp.Align:=TAlignLayout.Client;
Bmp.TagObject:=AItem;
Bmp.OnClick:=gGenericDoneButtonClick;
end;
end;
procedure TMainForm.gGenericDoneButtonClick(Sender: TObject);
var
ListID:Integer;
TV:TTMSFMXTableView;
Item:TTMSFMXTableViewItem;
Query:TUniQuery;
CompName:String;
begin
CompName:='ShoppingListTV'+IntToStr(TabShoppingLists.ActiveTab.Tag);
TV:=TabShoppingLists.ActiveTab.FindComponent(CompName) as TTMSFMXTableView;
if Assigned(TV) then
begin
Item:=TTMSFMXTableViewItem(TTMSFMXBitmap(Sender).TagObject);
if Assigned(Item) then
begin
ListID:=Item.Tag;
Query:=TUniQuery.Create(Nil);
Query.Connection:=UniConnection;
Query.SQL.Text:='update Lists set Current=0 where ID='+IntToStr(ListID);
Query.ExecSQL;
Query.Free;
TV.BeginUpdate;
Item.Release;
// TV.Items.Delete(Item.Index);
TV.EndUpdate;
end;
end;
end;
You can call Item.Free, there is no need for a TV.BeginUpdate/TV.EndUpdate.
Kind Regards,
Pieter
Pieter,
Thanks but without a TV.BeginUpdate/TV.EndUpdate the TableView is not refreshed.
Ken
Also Item.Free doesn't work from here.
If you are targetting a mobile platform, a free will be ignored.
Please try a DisposeOf instead, without the beginupdate/endupdate.
Thanks. It is a mobile app and DisposeOf without the beginupdate/endupdate is working but I am now totally confused about when to use free, disposeof and release!
This is because ARC is used on mobile applications. You can read the following article:
Thanks for the info.