commitEditingStyle with grouped table problems
I'm having a hard time wrapping my head around this. I'm loading up an NSMutable array from a plist file and populating a grouped table with the data. The table has 3 sections, section 0 is not editable but the other two are - I have that limitation working. My issue is when the user chooses to delete a row in the two editable sections. I believe it's because I have two keys for each entry - one for it's name and the other for it's url.
Here's a sample of my plist. the Title key is used for the section names. Then there's a key for 'Rows' - which is the text that shows in each cell and then 'url' which, when the row is selected, loads the url in a webview. I know that I want to grab the section and the row that the user selects and then delete both the 'Row' and 'url' for that index. Any help would be greatly appreciated.
Here's my plist:
<array>
<dict>
<key>Title</key>
<string>Query</string>
<key>Rows</key>
<array>
<string>Non-editable String 1</string>
<string>Non-editable String 2</string>
<string>Non-editable String 3</string>
<string>Non-editable String 4</string>
</array>
</dict>
<dict>
<key>Title</key>
<string>Resources</string>
<key>Rows</key>
<array>
<string>Website Name 1</string>
<string>Website Name 2</string>
<string>Website Name 3</string>
<string>Website Name 4</string>
<string>Website Name 5</string>
</array>
<key>url</key>
<array>
<string>http://website1.com</string>
<string>http://website2.com</string>
<string>http://website3.com</string>
<string>http://website4.com</string>
<string>http://website5.com</string>
</array>
</dict>
<dict>
<key>Title</key>
<string>Monitoring</string>
<key>Rows</key>
<array>
<string>Website Name 6</string>
<string>Website Name 7</string>
<string>Website Name 8</string>
</array>
<key>url</key>
<array>
<string>http://website6.com</string>
<string>http://web开发者_开发知识库site7.com</string>
<string>http://website8.com</string>
</array>
</dict>
This limits editing to the last two sections
-(UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath {
if (indexPath.section == 0)
return UITableViewCellEditingStyleNone;
else
return UITableViewCellEditingStyleDelete;
}
This is (the non-working code) for committing the delete (tableData is the name of my mutable array). My app bombs at:
[[self.tableData objectAtIndex:indexPath.section] removeObjectAtIndex:indexPath.row];
But guessing the deleteRowsAtIndexPaths is wrong too. Code:
-(void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
if (editingStyle == UITableViewCellEditingStyleDelete) {
[self.tableView beginUpdates];
[[self.tableData objectAtIndex:indexPath.section] removeObjectAtIndex:indexPath.row];
[self.tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath.row] withRowAnimation:UITableViewRowAnimationFade];
[self.tableView endUpdates];
[self.tableView reloadData];
}
---UPDATE---
If I do it this way and set section and row to NSUInteger, when I log it, the correct section and row are logged. However, I'm crashing at
[[self.tableData objectAtIndex:section] removeObjectAtIndex:row];
In my console, I'm getting this error: Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSCFDictionary removeObjectAtIndex:]: unrecognized selector sent to instance 0x4b43b20'
-(void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
if (editingStyle == UITableViewCellEditingStyleDelete) {
NSUInteger row = [indexPath row];
NSLog(@"NSUInteger Row: %d", row);
NSUInteger section = [indexPath section];
NSLog(@"NSUInteger Section: %d", section);
[self.tableView beginUpdates];
[[self.tableData objectAtIndex:section] removeObjectAtIndex:row];
[self.tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:[NSIndexPath indexPathForRow:row inSection:section]] withRowAnimation:UITableViewRowAnimationFade];
For your plist, the tableData
variable is an NSMutableArray
that contains 3 objects each of which is an NSMutableDictionary
(despite the fact that the docs say it should be an NSDictionary--immutable).
The second and third dictionaries contain three keys: Title, Rows, and url
The values of the Rows and url keys are NSMutableArrays
(again despite the docs saying they should be NSArrays--immutable).
Regardless, after reading the plist, the objects that you want to modify must one way or another be mutable either by default or by you explicitly calling mutableCopy
.
In the commitEditingStyle
method, you need to call removeObjectAtIndex
on the arrays and not the section dictionary. So first get a reference to the Rows and url arrays (you must be doing something similar in cellForRowAtIndexPath
to display the values).
-(void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
if (editingStyle == UITableViewCellEditingStyleDelete)
{
NSMutableDictionary *sectionDict = [tableData objectAtIndex:indexPath.section];
NSMutableArray *sectionRows = [sectionDict objectForKey:@"Rows"];
NSMutableArray *sectionUrls = [sectionDict objectForKey:@"url"];
[sectionRows removeObjectAtIndex:indexPath.row];
[sectionUrls removeObjectAtIndex:indexPath.row];
//don't need beginUpdates (we're making only one delete call)
//tableView beginUpdates];
[tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath]
withRowAnimation:UITableViewRowAnimationFade];
//don't need endUpdates (since we're not doing beginUpdates)
//[tableView endUpdates];
//don't need reloadData since we're calling deleteRowsAtIndexPaths
//(or call reloadData instead of deleteRowsAtIndexPaths)
//[tableView reloadData];
}
}
精彩评论