Skip to content

Commit

Permalink
Merge pull request #7355 from hansemannn/TIMOB-7735
Browse files Browse the repository at this point in the history
[TIMOB-7735] Support UITableView insert style
  • Loading branch information
pec1985 committed Oct 28, 2015
2 parents 2ab5574 + ce7bf38 commit 9bdba29
Show file tree
Hide file tree
Showing 3 changed files with 72 additions and 33 deletions.
10 changes: 10 additions & 0 deletions apidoc/Titanium/UI/ListItem.yml
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,16 @@ properties:
platforms: [iphone, ipad]
accessors: false

- name: canInsert
summary: Specifies if the item can be inserted by a user initiated action.
description: |
For more information see the "Editing Support" section of <Titanium.UI.ListView>.
type: Boolean
default: false
since: 5.2.0
platforms: [iphone, ipad]
accessors: false

- name: canMove
summary: Specifies if the item can be reordered within the list view by a user initiated action.
description: |
Expand Down
20 changes: 14 additions & 6 deletions apidoc/Titanium/UI/ListView.yml
Original file line number Diff line number Diff line change
Expand Up @@ -161,24 +161,32 @@ description: |
#### Editing Support (iOS)
Editing a list view through user initiated actions is supported through the following properties.
Editing a ListView through user initiated actions is supported through the following properties.
- **ListItem properties**:
- [canEdit](Titanium.UI.ListItem.canEdit) - When this is set to true, it allows the item to be deleted
from the List View through a user initiated action. The item can only be deleted when the List View is
from the ListView through a user initiated action. The item can only be deleted when the ListView is
in editing mode. The ListView can enter 'editing' mode either by explicitly setting the [editing](Titanium.UI.ListView.editing)
property to true, or by swiping accross an item whose `canEdit` property is set to true. When the user
deletes the item, a [delete](Titanium.UI.ListView.delete) event is fired.
- [editActions](Titanium.UI.ListItem.editActions) - When [canEdit](Titanium.UI.ListItem.canEdit) is set to true, the default behavior
is to allow the item to be deleted. This behavior can be overridden with on iOS 8 and above by using the [editActions](Titanium.UI.ListItem.editActions)
property of the listItem. When this property is defined, the user is instead presented with the options as defined by the [title](RowActionType.title) property.
In this scenario the listview does not fire a [delete](Titanium.UI.ListView.delete) event. Instead the [rowAction](Titanium.UI.ListView.rowAction) event is fired
and the developer is free to update the listview as required. This is supported on Titanium SDK 4.1.0 and later on the iOS platform.
property of the item. When this property is defined, the user is instead presented with the options as defined by the [title](RowActionType.title) property.
In this scenario the ListView does not fire a [delete](Titanium.UI.ListView.delete) event. Instead the [rowAction](Titanium.UI.ListView.rowAction) event is fired
and the developer is free to update the ListView as required. This is supported on Titanium SDK 4.1.0 and later on the iOS platform.
- [canInsert](Titanium.UI.ListItem.canInsert) - When this is set to true, it allows the item to insert a new item to the
ListView through a user initiated action. A new item can only be inserted when the ListView is
in editing mode. The ListView can enter 'editing' mode by explicitly setting the [editing](Titanium.UI.ListView.editing)
property to true. When the user clicks on the '+' sign of the item, an [insert](Titanium.UI.ListView.insert) event is fired.
Note: A new item is not inserted automatically when clicking on the '+' sign to let you decide the way to insert a new item.
You can use any inserting method of [Ti.UI.ListSection](Titanium.UI.ListSection), for example [insertItemsAt](Titanium.UI.ListSection.insertItemsAt)
and [appendItemsAt](Titanium.UI.ListSection.appendItemsAt).
- [canMove](Titanium.UI.ListItem.canMove) - When this item is set to true, it allows the item to be moved
to a different location within the List View. The item can only be moved when the List View is put in
to a different location within the ListView. The item can only be moved when the ListView is put in
editing mode by explicitly setting the [editing](Titanium.UI.ListView.editing) property to true. When the
user moves an item, a [move](Titanium.UI.ListView.move) event is fired.
Expand Down
75 changes: 48 additions & 27 deletions iphone/Classes/TiUIListView.m
Original file line number Diff line number Diff line change
Expand Up @@ -929,6 +929,13 @@ -(BOOL)canEditRowAtIndexPath:(NSIndexPath *)indexPath
return [TiUtils boolValue:editValue def:NO];
}

-(BOOL)canInsertRowAtIndexPath:(NSIndexPath *)indexPath
{
id insertValue = [self valueWithKey:@"canInsert" atIndexPath:indexPath];
//canInsert if undefined is false
return [TiUtils boolValue:insertValue def:NO];
}


-(BOOL)canMoveRowAtIndexPath:(NSIndexPath *)indexPath
{
Expand Down Expand Up @@ -1002,9 +1009,10 @@ - (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)
return NO;
}

if ([self canEditRowAtIndexPath:indexPath]) {
if ([self canEditRowAtIndexPath:indexPath] || [self canInsertRowAtIndexPath:indexPath]) {
return YES;
}

if (editing) {
return [self canMoveRowAtIndexPath:indexPath];
}
Expand All @@ -1013,32 +1021,14 @@ - (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)

- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
TiUIListSectionProxy* theSection = [[self.listViewProxy sectionForIndex:indexPath.section] retain];

if (editingStyle == UITableViewCellEditingStyleDelete) {
TiUIListSectionProxy* theSection = [[self.listViewProxy sectionForIndex:indexPath.section] retain];
NSDictionary *theItem = [[theSection itemAtIndex:indexPath.row] retain];

//Delete Data
[theSection deleteItemAtIndex:indexPath.row];

//Fire the delete Event if required
NSString *eventName = @"delete";
if ([self.proxy _hasListeners:eventName]) {

NSMutableDictionary *eventObject = [[NSMutableDictionary alloc] initWithObjectsAndKeys:
theSection, @"section",
NUMINTEGER(indexPath.section), @"sectionIndex",
NUMINTEGER(indexPath.row), @"itemIndex",
nil];
id propertiesValue = [theItem objectForKey:@"properties"];
NSDictionary *properties = ([propertiesValue isKindOfClass:[NSDictionary class]]) ? propertiesValue : nil;
id itemId = [properties objectForKey:@"itemId"];
if (itemId != nil) {
[eventObject setObject:itemId forKey:@"itemId"];
}
[self.proxy fireEvent:eventName withObject:eventObject withSource:self.proxy propagate:NO reportSuccess:NO errorCode:0 message:nil];
[eventObject release];
}
[theItem release];
[self fireEditEventWithName:@"delete" andSection:theSection atIndexPath:(NSIndexPath*)indexPath];

BOOL emptySection = NO;

Expand Down Expand Up @@ -1112,20 +1102,27 @@ - (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEd

}
[tableView endUpdates];
[theSection release];
} else if(editingStyle == UITableViewCellEditingStyleInsert) {
[self fireEditEventWithName:@"insert" andSection:theSection atIndexPath:(NSIndexPath*)indexPath];
}
[theSection release];
}

#pragma mark - Editing Support Delegate Methods.

- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath
{
//No support for insert style yet
if ([self canEditRowAtIndexPath:indexPath]) {
if ([self canEditRowAtIndexPath:indexPath] == YES && [self canInsertRowAtIndexPath:indexPath] == YES) {
DebugLog(@"[WARN] The row at sectionIndex=%i and itemIndex=%i has both 'canEdit' and 'canInsert'. Please use either 'canEdit' for deleting or 'canInsert' for inserting a row.", indexPath.section, indexPath.row);
}

if ([self canEditRowAtIndexPath:indexPath] == YES) {
return UITableViewCellEditingStyleDelete;
} else {
return UITableViewCellEditingStyleNone;
} else if([self canInsertRowAtIndexPath:indexPath] == YES) {
return UITableViewCellEditingStyleInsert;
}

return UITableViewCellEditingStyleNone;
}

- (NSArray *)tableView:(UITableView *)tableView editActionsForRowAtIndexPath:(NSIndexPath *)indexPath
Expand Down Expand Up @@ -2000,6 +1997,30 @@ -(void)initSearchController:(id)sender
}
}

-(void)fireEditEventWithName:(NSString*)name andSection:(TiUIListSectionProxy*)section atIndexPath:(NSIndexPath*)indexPath
{
NSDictionary *theItem = [[section itemAtIndex:indexPath.row] retain];

//Fire the delete Event if required
if ([self.proxy _hasListeners:name]) {

NSMutableDictionary *eventObject = [[NSMutableDictionary alloc] initWithObjectsAndKeys:
section, @"section",
NUMINTEGER(indexPath.section), @"sectionIndex",
NUMINTEGER(indexPath.row), @"itemIndex",
nil];
id propertiesValue = [theItem objectForKey:@"properties"];
NSDictionary *properties = ([propertiesValue isKindOfClass:[NSDictionary class]]) ? propertiesValue : nil;
id itemId = [properties objectForKey:@"itemId"];
if (itemId != nil) {
[eventObject setObject:itemId forKey:@"itemId"];
}
[self.proxy fireEvent:name withObject:eventObject withSource:self.proxy propagate:NO reportSuccess:NO errorCode:0 message:nil];
[eventObject release];
}
[theItem release];
}

#pragma mark - UITapGestureRecognizer

- (BOOL)gestureRecognizerShouldBegin:(UIGestureRecognizer *)gestureRecognizer
Expand Down

0 comments on commit 9bdba29

Please sign in to comment.