Skip to content

Commit

Permalink
[TIMOB-7735] Support UITableView insert style
Browse files Browse the repository at this point in the history
  • Loading branch information
hansemannn committed Oct 27, 2015
1 parent c1686a4 commit 567e29a
Showing 1 changed file with 48 additions and 27 deletions.
75 changes: 48 additions & 27 deletions iphone/Classes/TiUIListView.m
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 567e29a

Please sign in to comment.